Search in sources :

Example 11 with Book

use of com.tvd12.ezydata.example.redis.entity.Book in project java-examples by tvd12.

the class MyApplicationStartup method main.

public static void main(String[] args) {
    ApplicationContext appContext = SpringApplication.run(MyApplicationStartup.class, args);
    BookService bookService = appContext.getBean(BookService.class);
    bookService.saveBook(new Book(1, "youngmonkeys.org"));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) Book(com.tvd12.example.spring_boot.entity.Book) BookService(com.tvd12.example.spring_boot.service.BookService)

Example 12 with Book

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

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

Example 14 with Book

use of com.tvd12.ezydata.example.redis.entity.Book in project ezyfox-examples by tvd12.

the class BookServiceTest method addBookSuccess.

@Test
public void addBookSuccess() {
    // given
    final AddBookData addBookData = randomAddBookData();
    when(bookRepository.findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId())).thenReturn(null);
    final Author author = new Author(RandomUtil.randomLong(), RandomUtil.randomShortAlphabetString());
    when(authorRepository.findById(addBookData.getAuthorId())).thenReturn(author);
    final Category category = new Category(RandomUtil.randomLong(), RandomUtil.randomShortAlphabetString());
    when(categoryRepository.findById(addBookData.getCategoryId())).thenReturn(category);
    final Book book = new Book(0L, addBookData.getCategoryId(), addBookData.getAuthorId(), addBookData.getBookName(), addBookData.getPrice(), addBookData.getReleaseDate(), addBookData.getReleaseTime(), LocalDateTime.now(), LocalDateTime.now());
    when(dataToEntityConverter.toEntity(addBookData)).thenReturn(book);
    doAnswer(it -> {
        Book inputBook = (Book) it.getArguments()[0];
        inputBook.setId(RandomUtil.randomLong());
        return null;
    }).when(bookRepository).save(book);
    BookData bookData = BookData.builder().id(book.getId()).name(book.getName()).author(AuthorData.builder().id(author.getId()).name(author.getName()).build()).category(CategoryData.builder().id(category.getId()).name(category.getName()).build()).price(addBookData.getPrice()).releaseDate(addBookData.getReleaseDate()).releaseTime(addBookData.getReleaseTime()).build();
    when(entityToDataConverter.toData(book, author, category)).thenReturn(bookData);
    // when
    BookData actual = sut.addBook(addBookData);
    // then
    assertThat(actual).isEqualsTo(bookData);
    verify(bookRepository, times(1)).findByNameAndAuthorId(addBookData.getBookName(), addBookData.getAuthorId());
    verify(authorRepository, times(1)).findById(addBookData.getAuthorId());
    verify(categoryRepository, times(1)).findById(addBookData.getCategoryId());
    verify(bookRepository, times(1)).save(book);
    verify(dataToEntityConverter, times(1)).toEntity(addBookData);
    verify(entityToDataConverter, times(1)).toData(book, author, category);
    validateMockitoUsage();
}
Also used : Category(com.tvd12.ezydata.example.jpa.entity.Category) Book(com.tvd12.ezydata.example.jpa.entity.Book) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) Author(com.tvd12.ezydata.example.jpa.entity.Author) BookData(com.tvd12.ezydata.example.jpa.data.BookData) AddBookData(com.tvd12.ezydata.example.jpa.data.AddBookData) Test(org.junit.jupiter.api.Test)

Example 15 with Book

use of com.tvd12.ezydata.example.redis.entity.Book in project ezyfox-examples by tvd12.

the class EzyFoxBeanExample method main.

public static void main(String[] args) {
    final EzyBeanContext beanContext = EzyBeanContext.builder().scan("com.tvd12.ezyfox.example.bean").build();
    final BookController bookController = (BookController) beanContext.getBean(BookController.class);
    bookController.saveBook(new Book(1L, "EzyFox in action"));
}
Also used : EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) Book(com.tvd12.ezyfox.example.bean.entity.Book) BookController(com.tvd12.ezyfox.example.bean.controller.BookController)

Aggregations

Book (com.tvd12.ezydata.example.jpa.entity.Book)6 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)4 Author (com.tvd12.ezydata.example.jpa.entity.Author)3 Category (com.tvd12.ezydata.example.jpa.entity.Category)3 Author (com.tvd12.example.spring_boot_redis.entity.Author)2 Book (com.tvd12.example.spring_boot_redis.entity.Book)2 Category (com.tvd12.example.spring_boot_redis.entity.Category)2 BookData (com.tvd12.ezydata.example.jpa.data.BookData)2 DuplicatedBookException (com.tvd12.ezydata.example.jpa.exception.DuplicatedBookException)2 Author (com.tvd12.ezydata.example.mongo.entity.Author)2 Book (com.tvd12.ezydata.example.mongo.entity.Book)2 Category (com.tvd12.ezydata.example.mongo.entity.Category)2 Author (com.tvd12.ezydata.example.redis.entity.Author)2 Category (com.tvd12.ezydata.example.redis.entity.Category)2 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)2 HttpNotFoundException (com.tvd12.ezyhttp.core.exception.HttpNotFoundException)2 lombok.val (lombok.val)2 Test (org.junit.jupiter.api.Test)2 Book (com.tvd12.example.spring_boot.entity.Book)1 BookService (com.tvd12.example.spring_boot.service.BookService)1