use of com.tvd12.example.spring_boot.entity.Book in project ezyfox-examples by tvd12.
the class BookController method addBook.
@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
BookNameAndAuthorId bookNameAndAuthorId = new BookNameAndAuthorId(request.getBookName(), request.getAuthorId());
Long existedBookId = bookIdByNameAndAuthorIdMap.get(bookNameAndAuthorId);
if (existedBookId != null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " has already registered book: " + request.getBookName());
}
Author author = authorMap.get(request.getAuthorId());
if (author == null) {
throw new HttpBadRequestException("author: " + request.getAuthorId() + " not found");
}
Category category = categoryMap.get(request.getCategoryId());
if (category == null) {
throw new HttpBadRequestException("category: " + request.getCategoryId() + " not found");
}
val bookId = idGentor.incrementAndGet();
val book = requestToEntityConverter.toBookEntity(request, bookId);
bookMap.put(book.getId(), book);
bookIdByNameAndAuthorIdMap.put(bookNameAndAuthorId, bookId);
return entityToResponseConverter.toBookResponse(book, author, category);
}
use of com.tvd12.example.spring_boot.entity.Book in project ezyfox-examples by tvd12.
the class BookController method getBook.
@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
Book book = bookMap.get(bookId);
if (book == null) {
throw new HttpNotFoundException("not found book with id: " + bookId);
}
Author author = authorMap.get(book.getAuthorId());
Category category = categoryMap.get(book.getCategoryId());
return entityToResponseConverter.toBookResponse(book, author, category);
}
use of com.tvd12.example.spring_boot.entity.Book in project ezyfox-examples by tvd12.
the class BookController method addBook.
@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
bookValidator.validate(request);
final AddBookData addBookData = requestToDataConverter.toData(request);
final BookData bookData = bookService.addBook(addBookData);
return dataToResponseConverter.toResponse(bookData);
}
use of com.tvd12.example.spring_boot.entity.Book in project ezyfox-examples by tvd12.
the class DataToEntityConverter method toEntity.
public Book toEntity(AddBookData data) {
Book book = new Book();
book.setCategoryId(data.getCategoryId());
book.setAuthorId(data.getAuthorId());
book.setName(data.getBookName());
book.setPrice(data.getPrice());
book.setReleaseDate(data.getReleaseDate());
book.setReleaseTime(data.getReleaseTime());
book.setCreatedTime(LocalDateTime.now());
book.setUpdatedTime(LocalDateTime.now());
return book;
}
use of com.tvd12.example.spring_boot.entity.Book in project java-examples by tvd12.
the class BookController method getBook.
@GetMapping("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
Book book = bookRepository.findById(bookId).orElseThrow(() -> new HttpNotFoundException("not found book with id: " + bookId));
Author author = authorRepository.findById(book.getAuthorId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
Category category = categoryRepository.findById(book.getCategoryId()).orElseThrow(() -> new IllegalStateException("maybe someone has change redis"));
return entityToResponseConverter.toBookResponse(book, author, category);
}
Aggregations