From aa9798b68a38ca8124d615c2fa22d81d38aa5892 Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Wed, 12 Jun 2024 20:01:02 +0400 Subject: [PATCH] it is working --- .../ip/library/controllers/books/BookDto.java | 20 ++++++++ .../library/controllers/books/BookEntity.java | 49 ++++++++++++++++--- .../controllers/books/BookService.java | 37 +++++++++++++- .../controllers/users/UserBookController.java | 5 +- .../main/resources/templates/book-search.html | 5 +- 5 files changed, 104 insertions(+), 12 deletions(-) diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookDto.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookDto.java index 3270a6c..2b0775e 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookDto.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookDto.java @@ -22,6 +22,10 @@ public class BookDto { private Long typeId; @NotNull private List authorsId; + @NotNull + private boolean isChosen = false; + @Min(0) + private int fansNumber = 0; public Long getId() { return id; @@ -70,4 +74,20 @@ public class BookDto { public void setAuthorsId(List authorsId) { this.authorsId = authorsId; } + + public boolean getIsChosen() { + return isChosen; + } + + public void setIsChosen(boolean isChosen) { + this.isChosen = isChosen; + } + + public int getFansNumber() { + return fansNumber; + } + + public void setFansNumber(int fansNumber) { + this.fansNumber = fansNumber; + } } diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java index b1c441b..1238207 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookEntity.java @@ -15,9 +15,11 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.OrderBy; import jakarta.persistence.Table; +import jakarta.persistence.Transient; import com.ip.library.controllers.authors.AuthorEntity; import com.ip.library.controllers.authors_books.AuthorsBooksEntity; +import com.ip.library.controllers.favorites.FavoriteEntity; import com.ip.library.controllers.types.TypeEntity; @Entity @@ -32,6 +34,13 @@ public class BookEntity extends BaseEntity { @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @OrderBy("id ASC") private Set authorsBooks = new HashSet<>(); + @OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OrderBy("id ASC") + private Set fans = new HashSet<>(); + @Transient + private boolean isChosen = false; + @Transient + private int fansNumber = 0; public BookEntity() { super(); @@ -42,14 +51,6 @@ public class BookEntity extends BaseEntity { this.type = type; } - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - public TypeEntity getType() { return type; } @@ -66,6 +67,38 @@ public class BookEntity extends BaseEntity { this.authorsBooks = authorsBooks; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean getIsChosen() { + return isChosen; + } + + public void setIsChosen(boolean isChosen) { + this.isChosen = isChosen; + } + + public int getFansNumber() { + return fansNumber; + } + + public void setFansNumber(int fansNumber) { + this.fansNumber = fansNumber; + } + + public Set getFans() { + return fans; + } + + public void setFans(Set fans) { + this.fans = fans; + } + public boolean addAuthor(AuthorEntity author) { AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this); return authorsBooks.add(entity); diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java index 8a8e3cd..8e10c73 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookService.java @@ -13,6 +13,7 @@ import com.ip.library.controllers.authors.AuthorService; import com.ip.library.controllers.authors_books.AuthorsBooksEntity; import com.ip.library.controllers.authors_books.AuthorsBooksId; import com.ip.library.controllers.authors_books.AuthorsBooksRepository; +import com.ip.library.controllers.users.UserRepository; import com.ip.library.core.error.NotFoundException; @Service @@ -20,14 +21,17 @@ public class BookService { private final AuthorsBooksRepository authorsBooksRepository; private final BookRepository bookRepository; private final AuthorService authorService; + private final UserRepository userRepository; public BookService( BookRepository bookRepository, AuthorService authorService, - AuthorsBooksRepository authorsBooksRepository) { + AuthorsBooksRepository authorsBooksRepository, + UserRepository userRepository) { this.bookRepository = bookRepository; this.authorService = authorService; this.authorsBooksRepository = authorsBooksRepository; + this.userRepository = userRepository; } private void checkNameUniqueness(String name){ @@ -55,6 +59,32 @@ public class BookService { return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest); } + @Transactional(readOnly = true) + public Page getAll(long typeId, long authorId, long userId, int page, int size) { + PageRequest pageRequest = PageRequest.of(page, size); + Page result = null; + if (typeId <= 0L && authorId <= 0L) { + result = bookRepository.findAll(pageRequest); + } + else if (authorId <= 0L){ + result = bookRepository.findByTypeId(typeId, pageRequest); + } + else if (typeId <= 0L) { + result = bookRepository.findByAuthorId(authorId, pageRequest); + } + else { + result = bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest); + } + List favorites = getUserFavorities(userId); + for (BookEntity book : result.getContent()) { + if (favorites.contains(book)) { + book.setIsChosen(true); + } + book.setFansNumber(book.getFans().size()); + } + return result; + } + @Transactional(readOnly = true) public List getAll(long typeId, long authorId) { if (typeId <= 0L && authorId <= 0L) @@ -122,6 +152,11 @@ public class BookService { return bookRepository.getBookSubscribersNumber(bookId); } + @Transactional(readOnly = true) + public List getUserFavorities (long userId) { + return userRepository.getUserFavorities(userId); + } + @Transactional(readOnly = true) public List getAuthors(long bookId) { return bookRepository.getAuthors(bookId); diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java b/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java index 48b25f9..ab59dd4 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/users/UserBookController.java @@ -99,9 +99,10 @@ public class UserBookController { @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, @RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId, @RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId, - Model model) { + Model model, + @AuthenticationPrincipal UserPrincipal principal) { final Map attributes = PageAttributesMapper.toAttributes( - bookService.getAll(typeId, authorId, page, Constants.DEFUALT_PAGE_SIZE), + bookService.getAll(typeId, authorId, principal.getId(), page, Constants.DEFUALT_PAGE_SIZE), this::toBookDto); model.addAllAttributes(attributes); model.addAttribute(TYPE_ATTRIBUTE, typeId); diff --git a/SpringApp/library/src/main/resources/templates/book-search.html b/SpringApp/library/src/main/resources/templates/book-search.html index bc3c41b..28f8241 100644 --- a/SpringApp/library/src/main/resources/templates/book-search.html +++ b/SpringApp/library/src/main/resources/templates/book-search.html @@ -42,6 +42,7 @@ Название Жанр Автор + Добавили в избранное @@ -51,10 +52,12 @@ +
- +