use of com.tvd12.ezydata.example.jpa.entity.Category 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);
}
use of com.tvd12.ezydata.example.jpa.entity.Category in project ezyfox-examples by tvd12.
the class CategoryController method addCategory.
@DoPost("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
Category existedCategory = categoryRepository.findByName(request.getCategoryName());
if (existedCategory != null) {
throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
}
Category category = new Category(maxIdRepository.incrementAndGet("category"), request.getCategoryName());
categoryRepository.save(category);
return category;
}
use of com.tvd12.ezydata.example.jpa.entity.Category 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.ezydata.example.jpa.entity.Category 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.ezydata.example.jpa.entity.Category in project ezyfox-examples by tvd12.
the class CategoryController method addCategory.
@DoPost("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
Long existedCategoryId = categoryIdByNameMap.get(request.getCategoryName());
if (existedCategoryId != null) {
throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
}
Category category = new Category(idGentor.incrementAndGet(), request.getCategoryName());
categoryMap.put(category.getId(), category);
categoryIdByNameMap.put(category.getName(), category.getId());
return category;
}
Aggregations