Search in sources :

Example 1 with HttpBadRequestException

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);
}
Also used : Category(com.tvd12.example.spring_boot_redis.entity.Category) Book(com.tvd12.example.spring_boot_redis.entity.Book) BookNameAndAuthorId(com.tvd12.example.spring_boot_redis.entity.BookNameAndAuthorId) HttpBadRequestException(com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException) Author(com.tvd12.example.spring_boot_redis.entity.Author)

Example 2 with HttpBadRequestException

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;
}
Also used : Category(com.tvd12.example.spring_boot_redis.entity.Category) RedisAtomicLong(com.tvd12.example.spring_boot_redis.repository.RedisAtomicLong) HttpBadRequestException(com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with HttpBadRequestException

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());
}
Also used : HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Test(org.testng.annotations.Test)

Example 4 with HttpBadRequestException

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);
}
Also used : HashMap(java.util.HashMap) Reference(com.fasterxml.jackson.databind.JsonMappingException.Reference) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) TryCatch(com.tvd12.ezyhttp.server.core.annotation.TryCatch)

Example 5 with HttpBadRequestException

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);
}
Also used : lombok.val(lombok.val) Category(com.tvd12.ezydata.example.mongo.entity.Category) Book(com.tvd12.ezydata.example.mongo.entity.Book) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) Author(com.tvd12.ezydata.example.mongo.entity.Author)

Aggregations

HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)7 Category (com.tvd12.example.spring_boot_redis.entity.Category)2 HttpBadRequestException (com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException)2 Category (com.tvd12.ezydata.example.mongo.entity.Category)2 Category (com.tvd12.ezydata.example.redis.entity.Category)2 EzyRedisAtomicLong (com.tvd12.ezydata.redis.EzyRedisAtomicLong)2 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)2 lombok.val (lombok.val)2 Reference (com.fasterxml.jackson.databind.JsonMappingException.Reference)1 Author (com.tvd12.example.spring_boot_redis.entity.Author)1 Book (com.tvd12.example.spring_boot_redis.entity.Book)1 BookNameAndAuthorId (com.tvd12.example.spring_boot_redis.entity.BookNameAndAuthorId)1 RedisAtomicLong (com.tvd12.example.spring_boot_redis.repository.RedisAtomicLong)1 Author (com.tvd12.ezydata.example.mongo.entity.Author)1 Book (com.tvd12.ezydata.example.mongo.entity.Book)1 Author (com.tvd12.ezydata.example.redis.entity.Author)1 BookNameAndAuthorId (com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId)1 EzyProcessor.processWithLogException (com.tvd12.ezyfox.util.EzyProcessor.processWithLogException)1 BodyDeserializer (com.tvd12.ezyhttp.core.codec.BodyDeserializer)1 DeserializeBodyException (com.tvd12.ezyhttp.core.exception.DeserializeBodyException)1