Search in sources :

Example 1 with InvalidAuthorIdException

use of com.tvd12.ezydata.example.jpa.exception.InvalidAuthorIdException in project ezyfox-examples by tvd12.

the class BookService method addBook.

public BookData addBook(AddBookData data) {
    Book existedBook = bookRepository.findByNameAndAuthorId(data.getBookName(), data.getAuthorId());
    if (existedBook != null) {
        throw new DuplicatedBookException("author: " + data.getAuthorId() + " has already registered book: " + data.getBookName());
    }
    final Author author = authorRepository.findById(data.getAuthorId());
    if (author == null) {
        throw new InvalidAuthorIdException("author: " + data.getAuthorId() + " not found");
    }
    final Category category = categoryRepository.findById(data.getCategoryId());
    if (category == null) {
        throw new InvalidCategoryIdException("category: " + data.getCategoryId() + " not found");
    }
    final Book book = dataToEntityConverter.toEntity(data);
    bookRepository.save(book);
    return entityToDataConverter.toData(book, author, category);
}
Also used : DuplicatedBookException(com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException) Category(com.tvd12.ezydata.example.jpa.entity.Category) InvalidCategoryIdException(com.tvd12.ezydata.example.jpa.exception.InvalidCategoryIdException) Book(com.tvd12.ezydata.example.jpa.entity.Book) InvalidAuthorIdException(com.tvd12.ezydata.example.jpa.exception.InvalidAuthorIdException) Author(com.tvd12.ezydata.example.jpa.entity.Author)

Aggregations

Author (com.tvd12.ezydata.example.jpa.entity.Author)1 Book (com.tvd12.ezydata.example.jpa.entity.Book)1 Category (com.tvd12.ezydata.example.jpa.entity.Category)1 DuplicatedBookException (com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException)1 InvalidAuthorIdException (com.tvd12.ezydata.example.jpa.exception.InvalidAuthorIdException)1 InvalidCategoryIdException (com.tvd12.ezydata.example.jpa.exception.InvalidCategoryIdException)1