Search in sources :

Example 6 with HttpBadRequestException

use of com.tvd12.ezyhttp.core.exception.HttpBadRequestException 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;
}
Also used : Category(com.tvd12.ezydata.example.mongo.entity.Category) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Example 7 with HttpBadRequestException

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

Example 8 with HttpBadRequestException

use of com.tvd12.ezyhttp.core.exception.HttpBadRequestException 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;
}
Also used : Category(com.tvd12.ezydata.example.redis.entity.Category) EzyRedisAtomicLong(com.tvd12.ezydata.redis.EzyRedisAtomicLong) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Example 9 with HttpBadRequestException

use of com.tvd12.ezyhttp.core.exception.HttpBadRequestException in project ezyhttp by youngmonkeys.

the class AbstractRequestHandler method deserializeBody.

protected <T> T deserializeBody(BodyData bodyData, Class<T> type) throws IOException {
    String contentType = bodyData.getContentType();
    if (contentType == null) {
        throw new HttpBadRequestException("contentType is null");
    }
    BodyDeserializer deserializer = dataConverters.getBodyDeserializer(contentType);
    try {
        return deserializer.deserialize(bodyData, type);
    } catch (Exception e) {
        throw new DeserializeBodyException("can't deserialize body data to: " + type.getName(), e);
    }
}
Also used : BodyDeserializer(com.tvd12.ezyhttp.core.codec.BodyDeserializer) DeserializeBodyException(com.tvd12.ezyhttp.core.exception.DeserializeBodyException) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException) DeserializeHeaderException(com.tvd12.ezyhttp.core.exception.DeserializeHeaderException) IOException(java.io.IOException) DeserializePathVariableException(com.tvd12.ezyhttp.core.exception.DeserializePathVariableException) DeserializeBodyException(com.tvd12.ezyhttp.core.exception.DeserializeBodyException) DeserializeCookieException(com.tvd12.ezyhttp.core.exception.DeserializeCookieException) EzyProcessor.processWithLogException(com.tvd12.ezyfox.util.EzyProcessor.processWithLogException) DeserializeParameterException(com.tvd12.ezyhttp.core.exception.DeserializeParameterException) HttpBadRequestException(com.tvd12.ezyhttp.core.exception.HttpBadRequestException)

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