Search in sources :

Example 1 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData in project ezyfox-examples by tvd12.

the class BookServiceTest method addBookFailedDueToBookRepository.

@Test
public void addBookFailedDueToBookRepository() {
    // given
    final AddBookData addBookData = randomAddBookData();
    final Book book = new Book(RandomUtil.randomLong(), addBookData.getCategoryId(), addBookData.getAuthorId(), addBookData.getBookName(), addBookData.getPrice(), addBookData.getReleaseDate(), addBookData.getReleaseTime(), LocalDateTime.now(), LocalDateTime.now());
    when(bookRepository.findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId())).thenReturn(book);
    // when
    final Throwable throwable = assertThrows(() -> sut.addBook(addBookData));
    // then
    assertTrue(throwable instanceof DuplicatedBookException);
    verify(bookRepository, times(1)).findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId());
    validateMockitoUsage();
}
Also used : DuplicatedBookException(com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException) Book(com.tvd12.ezydata.example.jpa.entity.Book) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) Test(org.junit.jupiter.api.Test)

Example 2 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData in project ezyfox-examples by tvd12.

the class BookController method addBook.

@DoPost("/book/add")
public BookResponse addBook(@RequestBody AddBookRequest request) {
    bookValidator.validate(request);
    final AddBookData addBookData = requestToDataConverter.toData(request);
    final BookData bookData = bookService.addBook(addBookData);
    return dataToResponseConverter.toResponse(bookData);
}
Also used : AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) BookData(com.tvd12.ezydata.example.jpa.data.BookData) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData)

Example 3 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData in project ezyfox-examples by tvd12.

the class DataToEntityConverter method toEntity.

public Book toEntity(AddBookData data) {
    Book book = new Book();
    book.setCategoryId(data.getCategoryId());
    book.setAuthorId(data.getAuthorId());
    book.setName(data.getBookName());
    book.setPrice(data.getPrice());
    book.setReleaseDate(data.getReleaseDate());
    book.setReleaseTime(data.getReleaseTime());
    book.setCreatedTime(LocalDateTime.now());
    book.setUpdatedTime(LocalDateTime.now());
    return book;
}
Also used : Book(com.tvd12.ezydata.example.jpa.entity.Book)

Example 4 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData in project ezyfox-examples by tvd12.

the class BookService method addBooks.

public void addBooks(int count) {
    List<Book> books = new ArrayList<>();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int i = 0; i < count; ++i) {
        AddBookData data = AddBookData.builder().bookName(String.format("%03d", random.nextInt(1000))).authorId((long) random.nextInt(0, Integer.MAX_VALUE)).categoryId((long) random.nextInt(0, Integer.MAX_VALUE)).price(BigDecimal.valueOf(random.nextInt(0, Integer.MAX_VALUE))).releaseDate(LocalDate.now()).releaseTime(LocalDateTime.now()).build();
        books.add(dataToEntityConverter.toEntity(data));
    }
    bookRepository.save(books);
}
Also used : Book(com.tvd12.ezydata.example.jpa.entity.Book) ArrayList(java.util.ArrayList) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 5 with AddBookData

use of com.tvd12.ezydata.example.jpa.data.AddBookData 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

Book (com.tvd12.ezydata.example.jpa.entity.Book)5 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)4 BookData (com.tvd12.ezydata.example.jpa.data.BookData)2 Author (com.tvd12.ezydata.example.jpa.entity.Author)2 Category (com.tvd12.ezydata.example.jpa.entity.Category)2 DuplicatedBookException (com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException)2 Test (org.junit.jupiter.api.Test)2 InvalidAuthorIdException (com.tvd12.ezydata.example.jpa.exception.InvalidAuthorIdException)1 InvalidCategoryIdException (com.tvd12.ezydata.example.jpa.exception.InvalidCategoryIdException)1 ArrayList (java.util.ArrayList)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1