From 38823a4479ca73b79744901c8277daec436e4f3c Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Thu, 13 Jun 2024 20:01:02 +0400 Subject: [PATCH] fix likes filtration --- .gitignore | 1 + .../library/controllers/books/BookEntity.java | 8 +++- .../controllers/books/BookRepository.java | 37 +++++++++++++++++++ .../controllers/books/BookService.java | 18 ++------- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index a057160..28ebd4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ SpringApp/data.mv.db SpringApp/library/data.mv.db SpringApp/library/data.trace.db +SpringApp/data.trace.db 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 7d8af35..148cb2c 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 @@ -52,8 +52,14 @@ public class BookEntity extends BaseEntity { this.type = type; } - public BookEntity(boolean isChosen) { + public BookEntity(BookEntity book, boolean isChosen) { this.isChosen = isChosen; + this.name = book.name; + this.type = book.type; + this.authorsBooks =book.authorsBooks; + this.fans = book.fans; + this.fansNumber = book.fansNumber; + this.id = book.id; } public TypeEntity getType() { diff --git a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java index 598d312..2d3e387 100644 --- a/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java +++ b/SpringApp/library/src/main/java/com/ip/library/controllers/books/BookRepository.java @@ -16,9 +16,26 @@ public interface BookRepository extends PagingAndSortingRepository { Optional findByNameIgnoreCase(String name); + @Query( + "select new com.ip.library.controllers.books.BookEntity(b, " + + "case when f.id.bookId is null then false else true end) " + + "from BookEntity b left join FavoriteEntity f " + + "on b.id = f.id.bookId and f.user.id = ?1 " + + "order by b.id") + Page findAll(long userId, Pageable pageable); + List findByTypeId(long typeId); Page findByTypeId(long typeId, Pageable pageable); + + @Query( + "select new com.ip.library.controllers.books.BookEntity(b, " + + "case when f.id.bookId is null then false else true end) " + + "from BookEntity b left join FavoriteEntity f " + + "on b.id = f.id.bookId and f.user.id = ?2 " + + "where b.type.id = ?1 " + + "order by b.id") + Page findByTypeId(long typeId, long userId, Pageable pageable); @Query( "select ab.book " + @@ -34,6 +51,16 @@ public interface BookRepository extends "order by ab.book.id") Page findByAuthorId(Long authorId, Pageable pageable); + @Query( + "select new com.ip.library.controllers.books.BookEntity(b, " + + "case when f.id.bookId is null then false else true end) " + + "from BookEntity b " + + "left join FavoriteEntity f on b.id = f.id.bookId and f.user.id = ?2 " + + "left join AuthorsBooksEntity ab on b.id = ab.id.bookId " + + "where ab.id.authorId = ?1 " + + "order by b.id") + Page findByAuthorId(Long authorId, long userId, Pageable pageable); + @Query( "select ab.book " + "from AuthorsBooksEntity ab " + @@ -49,6 +76,16 @@ public interface BookRepository extends "ab.book.type.id = ?2 " + "order by ab.book.id") Page findByAuthorIdAndTypeId(Long authorId, Long typeId, Pageable pageable); + + @Query( + "select new com.ip.library.controllers.books.BookEntity(b, " + + "case when f.id.bookId is null then false else true end) " + + "from BookEntity b " + + "left join FavoriteEntity f on b.id = f.id.bookId and f.user.id = ?3 " + + "left join AuthorsBooksEntity ab on b.id = ab.id.bookId " + + "where ab.id.authorId = ?1 and b.type.id = ?2 " + + "order by b.id") + Page findByAuthorIdAndTypeId(Long authorId, Long typeId, long userId, Pageable pageable); @Query( "select count(*) as number " + 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 6986fc7..0070c5f 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 @@ -62,26 +62,16 @@ public class BookService { @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); + return bookRepository.findAll(userId, pageRequest); } else if (authorId <= 0L){ - result = bookRepository.findByTypeId(typeId, pageRequest); + return bookRepository.findByTypeId(typeId, userId, pageRequest); } else if (typeId <= 0L) { - result = bookRepository.findByAuthorId(authorId, pageRequest); + return bookRepository.findByAuthorId(authorId, userId, pageRequest); } - else { - result = bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest); - } - List favorites = getUserFavorities(userId); - for (BookEntity book : result.getContent()) { - if (favorites.contains(book)) { - book.setIsChosen(true); - } - } - return result; + return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, userId, pageRequest); } @Transactional(readOnly = true)