fix likes filtration

This commit is contained in:
Zakharov_Rostislav 2024-06-13 20:01:02 +04:00
parent a64bb9104e
commit 38823a4479
4 changed files with 49 additions and 15 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
SpringApp/data.mv.db
SpringApp/library/data.mv.db
SpringApp/library/data.trace.db
SpringApp/data.trace.db

View File

@ -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() {

View File

@ -16,9 +16,26 @@ public interface BookRepository extends
PagingAndSortingRepository<BookEntity, Long> {
Optional<BookEntity> 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<BookEntity> findAll(long userId, Pageable pageable);
List<BookEntity> findByTypeId(long typeId);
Page<BookEntity> 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<BookEntity> 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<BookEntity> 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<BookEntity> 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<BookEntity> 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<BookEntity> findByAuthorIdAndTypeId(Long authorId, Long typeId, long userId, Pageable pageable);
@Query(
"select count(*) as number " +

View File

@ -62,26 +62,16 @@ public class BookService {
@Transactional(readOnly = true)
public Page<BookEntity> getAll(long typeId, long authorId, long userId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
Page<BookEntity> 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<BookEntity> 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)