Search in sources :

Example 1 with Category

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

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

use of com.tvd12.ezydata.example.mongo.entity.Category in project ezyfox-examples by tvd12.

the class BookService method getBook.

public BookData getBook(Long bookId) {
    Book book = bookRepository.findById(bookId);
    if (book == null) {
        throw new BookNotFoundException("not found book with id: " + bookId);
    }
    final Author author = authorRepository.findById(book.getAuthorId());
    final Category category = categoryRepository.findById(book.getCategoryId());
    return entityToDataConverter.toData(book, author, category);
}
Also used : Category(com.tvd12.ezydata.example.jpa.entity.Category) Book(com.tvd12.ezydata.example.jpa.entity.Book) BookNotFoundException(com.tvd12.ezydata.example.jpa.exception.BookNotFoundException) Author(com.tvd12.ezydata.example.jpa.entity.Author)

Example 4 with Category

use of com.tvd12.ezydata.example.mongo.entity.Category in project ezyfox-examples by tvd12.

the class CategoryService method saveCategory.

public CategoryData saveCategory(AddCategoryData data) {
    final Category existedCategory = categoryRepository.findByName(data.getCategoryName());
    if (existedCategory != null) {
        throw new DuplicatedCategoryException("category named: " + data.getCategoryName() + " existed");
    }
    final Category entity = dataToEntityConverter.toEntity(data);
    categoryRepository.save(entity);
    return entityToDataConverter.toData(entity);
}
Also used : DuplicatedCategoryException(com.tvd12.ezydata.example.jpa.exception.DuplicatedCategoryException) Category(com.tvd12.ezydata.example.jpa.entity.Category)

Example 5 with Category

use of com.tvd12.ezydata.example.mongo.entity.Category in project ezyfox-examples by tvd12.

the class BookController method getBook.

@DoGet("/books/{bookId}")
public BookResponse getBook(@PathVariable Long bookId) {
    Book book = bookRepository.findById(bookId);
    if (book == null) {
        throw new HttpNotFoundException("not found book with id: " + bookId);
    }
    Author author = authorRepository.findById(book.getAuthorId());
    Category category = categoryRepository.findById(book.getCategoryId());
    return entityToResponseConverter.toBookResponse(book, author, category);
}
Also used : HttpNotFoundException(com.tvd12.ezyhttp.core.exception.HttpNotFoundException) Category(com.tvd12.ezydata.example.mongo.entity.Category) Book(com.tvd12.ezydata.example.mongo.entity.Book) Author(com.tvd12.ezydata.example.mongo.entity.Author)

Aggregations

Category (com.tvd12.ezydata.example.jpa.entity.Category)5 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)4 Category (com.tvd12.example.spring_boot_redis.entity.Category)3 Author (com.tvd12.ezydata.example.jpa.entity.Author)3 Book (com.tvd12.ezydata.example.jpa.entity.Book)3 Category (com.tvd12.ezydata.example.mongo.entity.Category)3 Category (com.tvd12.ezydata.example.redis.entity.Category)3 Author (com.tvd12.example.spring_boot_redis.entity.Author)2 Book (com.tvd12.example.spring_boot_redis.entity.Book)2 HttpBadRequestException (com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException)2 Author (com.tvd12.ezydata.example.mongo.entity.Author)2 Book (com.tvd12.ezydata.example.mongo.entity.Book)2 Author (com.tvd12.ezydata.example.redis.entity.Author)2 EzyRedisAtomicLong (com.tvd12.ezydata.redis.EzyRedisAtomicLong)2 HttpNotFoundException (com.tvd12.ezyhttp.core.exception.HttpNotFoundException)2 DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)2 lombok.val (lombok.val)2 BookNameAndAuthorId (com.tvd12.example.spring_boot_redis.entity.BookNameAndAuthorId)1 HttpNotFoundException (com.tvd12.example.spring_boot_redis.exception.HttpNotFoundException)1 RedisAtomicLong (com.tvd12.example.spring_boot_redis.repository.RedisAtomicLong)1