use of com.tvd12.ezyfox.example.bean.entity.Book in project java-examples by tvd12.
the class BookController method addBook.
@PostMapping("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
BookNameAndAuthorId bookNameAndAuthorId = new BookNameAndAuthorId(request.getBookName(), request.getAuthorId());
Long existedBookId = bookIdByNameAndAuthorIdRepository.findById(bookNameAndAuthorId).orElse(null);
if (existedBookId != null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
}
Author author = authorRepository.findById(request.getAuthorId()).orElseThrow(() -> new HttpBadRequestException("author: " + request.getAuthorId() + " not found"));
Category category = categoryRepository.findById(request.getCategoryId()).orElseThrow(() -> new HttpBadRequestException("category: " + request.getCategoryId() + " not found"));
Long bookId = idGentor.incrementAndGet("book");
Book book = requestToEntityConverter.toBookEntity(request, bookId);
bookRepository.save(book);
bookIdByNameAndAuthorIdRepository.put(bookNameAndAuthorId, bookId);
return entityToResponseConverter.toBookResponse(book, author, category);
}
use of com.tvd12.ezyfox.example.bean.entity.Book in project ezyfox-examples by tvd12.
the class BookService method getBook.
public BookData getBook(Long bookId) {
Book book = bookRepository.findById(bookId);
if (book == null) {
throw new BookNotFoundException("not found book with id: " + bookId);
}
final Author author = authorRepository.findById(book.getAuthorId());
final Category category = categoryRepository.findById(book.getCategoryId());
return entityToDataConverter.toData(book, author, category);
}
use of com.tvd12.ezyfox.example.bean.entity.Book in project ezyfox-examples by tvd12.
the class BookServiceTest method addBookFailedDueToBookRepository.
@Test
public void addBookFailedDueToBookRepository() {
// given
final AddBookData addBookData = randomAddBookData();
final Book book = new Book(RandomUtil.randomLong(), addBookData.getCategoryId(), addBookData.getAuthorId(), addBookData.getBookName(), addBookData.getPrice(), addBookData.getReleaseDate(), addBookData.getReleaseTime(), LocalDateTime.now(), LocalDateTime.now());
when(bookRepository.findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId())).thenReturn(book);
// when
final Throwable throwable = assertThrows(() -> sut.addBook(addBookData));
// then
assertTrue(throwable instanceof DuplicatedBookException);
verify(bookRepository, times(1)).findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId());
validateMockitoUsage();
}
use of com.tvd12.ezyfox.example.bean.entity.Book in project ezyfox-examples by tvd12.
the class BookController method getBook.
@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
Book book = bookRepository.findById(bookId);
if (book == null) {
throw new HttpNotFoundException("not found book with id: " + bookId);
}
Author author = authorRepository.findById(book.getAuthorId());
Category category = categoryRepository.findById(book.getCategoryId());
return entityToResponseConverter.toBookResponse(book, author, category);
}
use of com.tvd12.ezyfox.example.bean.entity.Book in project ezyfox-examples by tvd12.
the class BookController method addBook.
@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
Book existedBook = bookRepository.findByNameAndAuthorId(request.getBookName(), request.getAuthorId());
if (existedBook != null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
}
Author author = authorRepository.findById(request.getAuthorId());
if (author == null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " not found");
}
Category category = categoryRepository.findById(request.getCategoryId());
if (category == null) {
throw new HttpBadRequestException("category: " + request.getCategoryId() + " not found");
}
val bookId = maxIdRepository.incrementAndGet("book");
val book = requestToEntityConverter.toBookEntity(request, bookId);
bookRepository.save(book);
return entityToResponseConverter.toBookResponse(book, author, category);
}
Aggregations