use of com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException 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.example.spring_boot_redis.exception.HttpBadRequestException in project java-examples by tvd12.
the class CategoryController method addCategory.
@PostMapping("/add")
public Category addCategory(@RequestBody AddCategoryRequest request) {
Long existedCategoryId = categoryIdByNameRepository.findById(request.getCategoryName()).orElse(null);
if (existedCategoryId != null) {
throw new HttpBadRequestException("category named: " + request.getCategoryName() + " existed");
}
Category category = new Category(idGentor.incrementAndGet("category"), request.getCategoryName());
categoryRepository.save(category);
categoryIdByNameRepository.put(category.getName(), category.getId());
return category;
}
use of com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException in project ezyhttp by youngmonkeys.
the class HttpBadRequestExceptionTest method test.
@Test
public void test() {
// given
int code = StatusCodes.BAD_REQUEST;
String data = "error";
// when
HttpBadRequestException sut = new HttpBadRequestException(data);
// then
Asserts.assertEquals(code, sut.getCode());
Asserts.assertEquals(data, sut.getData());
}
use of com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException in project ezyhttp by youngmonkeys.
the class GlobalExceptionHandler method handleException.
@TryCatch(InvalidFormatException.class)
public void handleException(RequestArguments args, HttpServletRequest request, HttpServletResponse response, InvalidFormatException e) {
e.printStackTrace();
Map<String, String> data = new HashMap<>();
for (Reference reference : e.getPath()) data.put(reference.getFieldName(), "invalid");
throw new HttpBadRequestException(data);
}
use of com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException 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