Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save namila007/03e8bce213efec52bcbbedb5fe9fde82 to your computer and use it in GitHub Desktop.
Save namila007/03e8bce213efec52bcbbedb5fe9fde82 to your computer and use it in GitHub Desktop.
Author Service
@SuppressWarnings("unchecked")
@Service
@NoArgsConstructor
public class AuthorServiceImp extends AuthorService
{
@Autowired
private AuthorRepository authorRepository;
@Override
public Page<Author> getAll( Pageable pageable )
{
return authorRepository.findAll( pageable );
}
@Override
public Author add( Author o )
{
return authorRepository.save( o );
}
@Override
public Author update( Author o, int id )
{
Author author = checkIfIdIsPresentandReturnAuthor( id );
author.setName( o.getName() );
author.setAddress( o.getAddress() );
return authorRepository.save( author );
}
@Override
public Author getById( int id )
{
return checkIfIdIsPresentandReturnAuthor( id );
}
@Override
public Author deleteById( int id )
{
Author author = checkIfIdIsPresentandReturnAuthor( id );
authorRepository.deleteById( id );
return author;
}
@Override
public List<Book> getBooksById( int id )
{
return checkIfIdIsPresentandReturnAuthor( id ).getBookList();
}
private Author checkIfIdIsPresentandReturnAuthor( int id )
{
if ( !authorRepository.findById( id ).isPresent() )
throw new ResourceNotFoundException( " Author id = " + id + " not found" );
else
return authorRepository.findById( id ).get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment