Search in sources :

Example 1 with BookNameAndAuthorId

use of com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId 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 BookNameAndAuthorId

use of com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId 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)

Aggregations

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 Category (com.tvd12.example.spring_boot_redis.entity.Category)1 HttpBadRequestException (com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException)1 Author (com.tvd12.ezydata.example.redis.entity.Author)1 BookNameAndAuthorId (com.tvd12.ezydata.example.redis.entity.BookNameAndAuthorId)1 Category (com.tvd12.ezydata.example.redis.entity.Category)1 EzyRedisAtomicLong (com.tvd12.ezydata.redis.EzyRedisAtomicLong)1 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)1 lombok.val (lombok.val)1