From 218ebb4e0350dee0a7a9445f6cc3fac301ca5e9e Mon Sep 17 00:00:00 2001 From: Anton Isaiev Date: Mon, 4 May 2026 09:58:04 +0300 Subject: [PATCH] fix(books): include physical books in app book list query (#938) --- .../booklore/app/service/AppBookService.java | 22 +++++++++---------- .../app/service/AppSeriesService.java | 12 +++++----- .../specification/AppBookSpecification.java | 7 ++++++ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/backend/src/main/java/org/booklore/app/service/AppBookService.java b/backend/src/main/java/org/booklore/app/service/AppBookService.java index 5db3f0655..5b759f25e 100644 --- a/backend/src/main/java/org/booklore/app/service/AppBookService.java +++ b/backend/src/main/java/org/booklore/app/service/AppBookService.java @@ -247,7 +247,7 @@ public class AppBookService { Specification spec = AppBookSpecification.combine( AppBookSpecification.notDeleted(), - AppBookSpecification.hasDigitalFile(), + AppBookSpecification.hasDigitalFileOrIsPhysical(), AppBookSpecification.inLibraries(accessibleLibraryIds), AppBookSpecification.searchText(query) ); @@ -311,7 +311,7 @@ public class AppBookService { Specification spec = AppBookSpecification.combine( AppBookSpecification.notDeleted(), - AppBookSpecification.hasDigitalFile(), + AppBookSpecification.hasDigitalFileOrIsPhysical(), AppBookSpecification.inLibraries(accessibleLibraryIds), AppBookSpecification.addedWithinDays(30) ); @@ -404,7 +404,7 @@ public class AppBookService { List summaries = orderedBookIds.stream() .map(bookEntitiesById::get) .filter(Objects::nonNull) - .filter(BookEntity::hasFiles) + .filter(b -> b.hasFiles() || Boolean.TRUE.equals(b.getIsPhysical())) .map(bookEntity -> mobileBookMapper.toSummary(bookEntity, progressMap.get(bookEntity.getId()))) .toList(); @@ -573,7 +573,7 @@ public class AppBookService { "FROM UserBookProgressEntity ubp " + "WHERE ubp.user.id = :userId AND ubp.personalRating IS NOT NULL " + "AND ubp.book.id IN (SELECT b.id FROM BookEntity b WHERE (b.deleted IS NULL OR b.deleted = false) " + - "AND b.bookFiles IS NOT EMPTY " + + "AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true) " + scopeClause + ") " + "GROUP BY 1 ORDER BY 1 DESC"; var prQ = entityManager.createQuery(personalRatingQuery, Tuple.class); @@ -624,7 +624,7 @@ public class AppBookService { String creatorJpql = "SELECT cr.name, mapping.role, COUNT(DISTINCT b.id) FROM BookEntity b" + " JOIN b.metadata m JOIN m.comicMetadata cm JOIN cm.creatorMappings mapping JOIN mapping.creator cr" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " " + scopeClause + " GROUP BY cr.name, mapping.role ORDER BY COUNT(DISTINCT b.id) DESC"; var creatorQ = entityManager.createQuery(creatorJpql, Tuple.class); @@ -800,7 +800,7 @@ public class AppBookService { List> specs = new ArrayList<>(); specs.add(AppBookSpecification.notDeleted()); - specs.add(AppBookSpecification.hasDigitalFile()); + specs.add(AppBookSpecification.hasDigitalFileOrIsPhysical()); if (accessibleLibraryIds != null) { if (req.libraryId() != null && accessibleLibraryIds.contains(req.libraryId())) { @@ -1105,7 +1105,7 @@ public class AppBookService { private Specification buildBaseSpecification(Set accessibleLibraryIds, Long libraryId) { List> specs = new ArrayList<>(); specs.add(AppBookSpecification.notDeleted()); - specs.add(AppBookSpecification.hasDigitalFile()); + specs.add(AppBookSpecification.hasDigitalFileOrIsPhysical()); if (accessibleLibraryIds != null) { if (libraryId != null && !accessibleLibraryIds.contains(libraryId)) { @@ -1155,7 +1155,7 @@ public class AppBookService { String jpql = "SELECT " + selectExpr + ", COUNT(DISTINCT b.id) FROM BookEntity b" + " " + joins + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + (extraWhere.isEmpty() ? "" : " " + extraWhere) + " " + scopeClause + " GROUP BY " + selectExpr + " ORDER BY COUNT(DISTINCT b.id) DESC"; @@ -1208,7 +1208,7 @@ public class AppBookService { + " AND ubp.book.id IN (" + " SELECT b.id FROM BookEntity b" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " " + scopeClause + " )" + " GROUP BY ubp.readStatus ORDER BY COUNT(DISTINCT ubp.book.id) DESC"; @@ -1223,7 +1223,7 @@ public class AppBookService { String baseQuery = "SELECT COUNT(DISTINCT b.id) FROM BookEntity b" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " AND b.id NOT IN (" + " SELECT ubp.book.id FROM UserBookProgressEntity ubp WHERE ubp.user.id = :userId" + " )" @@ -1262,7 +1262,7 @@ public class AppBookService { String scopeClause, Set accessibleLibraryIds, Long libraryId, Long shelfId, Set magicBookIds) { String jpql = "SELECT " + caseExpr + ", COUNT(DISTINCT b.id) FROM BookEntity b " + joins + - " WHERE (b.deleted IS NULL OR b.deleted = false) AND b.bookFiles IS NOT EMPTY " + " WHERE (b.deleted IS NULL OR b.deleted = false) AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true) " + extraWhere + " " + scopeClause + " GROUP BY 1"; var q = entityManager.createQuery(jpql, Tuple.class); diff --git a/backend/src/main/java/org/booklore/app/service/AppSeriesService.java b/backend/src/main/java/org/booklore/app/service/AppSeriesService.java index 06baadee6..44c59fb6d 100644 --- a/backend/src/main/java/org/booklore/app/service/AppSeriesService.java +++ b/backend/src/main/java/org/booklore/app/service/AppSeriesService.java @@ -80,7 +80,7 @@ public class AppSeriesService { + " FROM BookEntity b JOIN b.metadata m" + " LEFT JOIN b.userBookProgress p ON p.user.id = :userId" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " AND m.seriesName IS NOT NULL" + libraryClause + searchClause @@ -103,7 +103,7 @@ public class AppSeriesService { String countQuery = "SELECT COUNT(DISTINCT m.seriesName) FROM BookEntity b JOIN b.metadata m" + (inProgressOnly ? " LEFT JOIN b.userBookProgress p ON p.user.id = :userId" : "") + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " AND m.seriesName IS NOT NULL" + libraryClause + searchClause; @@ -114,7 +114,7 @@ public class AppSeriesService { + "SELECT m.seriesName FROM BookEntity b JOIN b.metadata m" + " LEFT JOIN b.userBookProgress p ON p.user.id = :userId" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " AND m.seriesName IS NOT NULL" + libraryClause + searchClause @@ -125,7 +125,7 @@ public class AppSeriesService { String countAlt = "SELECT m.seriesName FROM BookEntity b JOIN b.metadata m" + " LEFT JOIN b.userBookProgress p ON p.user.id = :userId" + " WHERE (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + " AND m.seriesName IS NOT NULL" + libraryClause + searchClause @@ -178,7 +178,7 @@ public class AppSeriesService { + " JOIN FETCH b.metadata m" + " WHERE m.seriesName IN :seriesNames" + " AND (b.deleted IS NULL OR b.deleted = false)" - + " AND b.bookFiles IS NOT EMPTY" + + " AND (b.bookFiles IS NOT EMPTY OR b.isPhysical = true)" + libraryClause; var booksQ = entityManager.createQuery(booksQuery, BookEntity.class); @@ -362,7 +362,7 @@ public class AppSeriesService { private Specification buildSeriesBooksSpec(Set accessibleLibraryIds, Long libraryId, String seriesName) { List> specs = new ArrayList<>(); specs.add(AppBookSpecification.notDeleted()); - specs.add(AppBookSpecification.hasDigitalFile()); + specs.add(AppBookSpecification.hasDigitalFileOrIsPhysical()); specs.add(AppBookSpecification.inSeries(seriesName)); if (accessibleLibraryIds != null) { diff --git a/backend/src/main/java/org/booklore/app/specification/AppBookSpecification.java b/backend/src/main/java/org/booklore/app/specification/AppBookSpecification.java index b0aff9ddb..ea18d99ad 100644 --- a/backend/src/main/java/org/booklore/app/specification/AppBookSpecification.java +++ b/backend/src/main/java/org/booklore/app/specification/AppBookSpecification.java @@ -202,6 +202,13 @@ public class AppBookSpecification { return (root, query, cb) -> cb.isNotEmpty(root.get("bookFiles")); } + public static Specification hasDigitalFileOrIsPhysical() { + return (root, query, cb) -> cb.or( + cb.isNotEmpty(root.get("bookFiles")), + cb.equal(root.get("isPhysical"), true) + ); + } + public static Specification hasAudiobookFile() { return (root, query, cb) -> { Subquery subquery = query.subquery(Long.class);