fix(filter): update filter options to use CountedOption for read statuses and file types (#448)
* fix(filter): update filter options to use CountedOption for read statuses and file types * fix(specification): validate file types and read statuses, throwing APIException for invalid values
This commit is contained in:
@@ -10,8 +10,8 @@ import java.util.List;
|
||||
public record AppFilterOptions(
|
||||
List<CountedOption> authors,
|
||||
List<LanguageOption> languages,
|
||||
List<String> readStatuses,
|
||||
List<String> fileTypes,
|
||||
List<CountedOption> readStatuses,
|
||||
List<CountedOption> fileTypes,
|
||||
List<CountedOption> categories,
|
||||
List<CountedOption> publishers,
|
||||
List<CountedOption> series,
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.booklore.app.dto;
|
||||
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -13,9 +11,9 @@ public record BookListRequest(
|
||||
String dir,
|
||||
Long libraryId,
|
||||
Long shelfId,
|
||||
ReadStatus status,
|
||||
@Size(max = 20) List<String> status,
|
||||
String search,
|
||||
BookFileType fileType,
|
||||
@Size(max = 20) List<String> fileType,
|
||||
Integer minRating,
|
||||
Integer maxRating,
|
||||
@Size(max = 20) List<String> authors,
|
||||
|
||||
@@ -362,7 +362,7 @@ public class AppBookService {
|
||||
.authors(Collections.emptyList())
|
||||
.languages(Collections.emptyList())
|
||||
.fileTypes(Collections.emptyList())
|
||||
.readStatuses(getReadStatusOptions())
|
||||
.readStatuses(Collections.emptyList())
|
||||
.categories(Collections.emptyList())
|
||||
.publishers(Collections.emptyList())
|
||||
.series(Collections.emptyList())
|
||||
@@ -409,15 +409,20 @@ public class AppBookService {
|
||||
c.count()))
|
||||
.toList();
|
||||
|
||||
String fileTypeQuery = "SELECT DISTINCT bf.bookType FROM BookEntity b"
|
||||
String fileTypeQuery = "SELECT bf.bookType, COUNT(DISTINCT b.id) FROM BookEntity b"
|
||||
+ " JOIN b.bookFiles bf"
|
||||
+ " WHERE (b.deleted IS NULL OR b.deleted = false)"
|
||||
+ " AND b.bookFiles IS NOT EMPTY"
|
||||
+ " AND bf.isBookFormat = true"
|
||||
+ scopeClause;
|
||||
var ftQ = entityManager.createQuery(fileTypeQuery, BookFileType.class);
|
||||
+ scopeClause
|
||||
+ " GROUP BY bf.bookType ORDER BY COUNT(DISTINCT b.id) DESC";
|
||||
var ftQ = entityManager.createQuery(fileTypeQuery, Tuple.class);
|
||||
setFilterQueryParams(ftQ, accessibleLibraryIds, libraryId, shelfId, magicBookIds);
|
||||
List<String> fileTypes = ftQ.getResultList().stream().map(Enum::name).sorted().toList();
|
||||
List<AppFilterOptions.CountedOption> fileTypes = ftQ.getResultList().stream()
|
||||
.map(t -> new AppFilterOptions.CountedOption(
|
||||
t.get(0, BookFileType.class).name(),
|
||||
t.get(1, Long.class)))
|
||||
.toList();
|
||||
|
||||
List<AppFilterOptions.CountedOption> categories = queryCountedOptions(
|
||||
"c.name", "JOIN b.metadata m JOIN m.categories c", "",
|
||||
@@ -446,11 +451,14 @@ public class AppBookService {
|
||||
"AND m.narrator IS NOT NULL AND m.narrator <> ''",
|
||||
scopeClause, accessibleLibraryIds, libraryId, shelfId, magicBookIds);
|
||||
|
||||
List<AppFilterOptions.CountedOption> readStatuses = queryReadStatusCounts(
|
||||
userId, scopeClause, accessibleLibraryIds, libraryId, shelfId, magicBookIds);
|
||||
|
||||
AppFilterOptions result = AppFilterOptions.builder()
|
||||
.authors(authors)
|
||||
.languages(languages)
|
||||
.fileTypes(fileTypes)
|
||||
.readStatuses(getReadStatusOptions())
|
||||
.readStatuses(readStatuses)
|
||||
.categories(categories)
|
||||
.publishers(publishers)
|
||||
.series(seriesOptions)
|
||||
@@ -514,10 +522,26 @@ public class AppBookService {
|
||||
.getResultList());
|
||||
}
|
||||
|
||||
private List<String> getReadStatusOptions() {
|
||||
return Arrays.stream(ReadStatus.values())
|
||||
.filter(s -> s != ReadStatus.UNSET)
|
||||
.map(Enum::name)
|
||||
private List<AppFilterOptions.CountedOption> queryReadStatusCounts(
|
||||
Long userId, String scopeClause, Set<Long> accessibleLibraryIds,
|
||||
Long libraryId, Long shelfId, Set<Long> magicBookIds) {
|
||||
String jpql = "SELECT ubp.readStatus, COUNT(DISTINCT ubp.book.id) FROM UserBookProgressEntity ubp"
|
||||
+ " WHERE ubp.user.id = :userId"
|
||||
+ " AND ubp.readStatus <> org.booklore.model.enums.ReadStatus.UNSET"
|
||||
+ " 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"
|
||||
+ scopeClause
|
||||
+ " )"
|
||||
+ " GROUP BY ubp.readStatus ORDER BY COUNT(DISTINCT ubp.book.id) DESC";
|
||||
var q = entityManager.createQuery(jpql, Tuple.class);
|
||||
q.setParameter("userId", userId);
|
||||
setFilterQueryParams(q, accessibleLibraryIds, libraryId, shelfId, magicBookIds);
|
||||
return q.getResultList().stream()
|
||||
.map(t -> new AppFilterOptions.CountedOption(
|
||||
t.get(0, ReadStatus.class).name(),
|
||||
t.get(1, Long.class)))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -624,16 +648,22 @@ public class AppBookService {
|
||||
specs.add(AppBookSpecification.inShelf(req.shelfId()));
|
||||
}
|
||||
|
||||
if (req.status() != null) {
|
||||
specs.add(AppBookSpecification.withReadStatus(req.status(), userId));
|
||||
if (req.status() != null && !req.status().isEmpty()) {
|
||||
List<String> cleaned = BookListRequest.cleanValues(req.status());
|
||||
if (!cleaned.isEmpty()) {
|
||||
specs.add(AppBookSpecification.withReadStatuses(cleaned, userId, req.effectiveFilterMode()));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.search() != null && !req.search().trim().isEmpty()) {
|
||||
specs.add(AppBookSpecification.searchText(req.search()));
|
||||
}
|
||||
|
||||
if (req.fileType() != null) {
|
||||
specs.add(AppBookSpecification.withFileType(req.fileType()));
|
||||
if (req.fileType() != null && !req.fileType().isEmpty()) {
|
||||
List<String> cleaned = BookListRequest.cleanValues(req.fileType());
|
||||
if (!cleaned.isEmpty()) {
|
||||
specs.add(AppBookSpecification.withFileTypes(cleaned, req.effectiveFilterMode()));
|
||||
}
|
||||
}
|
||||
|
||||
if (req.minRating() != null) {
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
package org.booklore.app.specification;
|
||||
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ReadStatus;
|
||||
import jakarta.persistence.criteria.*;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AppBookSpecification {
|
||||
|
||||
@@ -172,6 +175,100 @@ public class AppBookSpecification {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books by multiple file types with mode support.
|
||||
* OR = books with at least one file of ANY listed type
|
||||
* AND = books with files of ALL listed types
|
||||
* NOT = books with NONE of the listed file types
|
||||
*/
|
||||
public static Specification<BookEntity> withFileTypes(List<String> fileTypes, String mode) {
|
||||
return (root, query, cb) -> {
|
||||
List<String> unknown = new ArrayList<>();
|
||||
List<BookFileType> parsed = fileTypes.stream()
|
||||
.filter(s -> s != null && !s.isBlank())
|
||||
.map(s -> {
|
||||
String trimmed = s.trim().toUpperCase();
|
||||
try {
|
||||
return BookFileType.valueOf(trimmed);
|
||||
} catch (IllegalArgumentException e) {
|
||||
unknown.add(s.trim());
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
if (!unknown.isEmpty()) {
|
||||
throw new APIException("Invalid fileType values: " + unknown + ". Valid values: " + List.of(BookFileType.values()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (parsed.isEmpty()) return cb.conjunction();
|
||||
|
||||
if ("and".equals(mode)) {
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
for (BookFileType ft : parsed) {
|
||||
Subquery<Long> sub = query.subquery(Long.class);
|
||||
Root<BookFileEntity> bfRoot = sub.from(BookFileEntity.class);
|
||||
sub.select(bfRoot.get("book").get("id"))
|
||||
.where(cb.equal(bfRoot.get("bookType"), ft));
|
||||
predicates.add(root.get("id").in(sub));
|
||||
}
|
||||
return cb.and(predicates.toArray(new Predicate[0]));
|
||||
}
|
||||
|
||||
Subquery<Long> sub = query.subquery(Long.class);
|
||||
Root<BookFileEntity> bfRoot = sub.from(BookFileEntity.class);
|
||||
sub.select(bfRoot.get("book").get("id"))
|
||||
.where(bfRoot.get("bookType").in(parsed));
|
||||
|
||||
if ("not".equals(mode)) {
|
||||
return cb.not(root.get("id").in(sub));
|
||||
}
|
||||
return root.get("id").in(sub);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books by multiple read statuses with mode support (per-user).
|
||||
* OR = books with ANY of the listed read statuses
|
||||
* AND = impossible for a single-value field, treated as OR
|
||||
* NOT = books with NONE of the listed read statuses
|
||||
*/
|
||||
public static Specification<BookEntity> withReadStatuses(List<String> statuses, Long userId, String mode) {
|
||||
return (root, query, cb) -> {
|
||||
if (userId == null) return cb.conjunction();
|
||||
List<String> unknown = new ArrayList<>();
|
||||
List<ReadStatus> parsed = statuses.stream()
|
||||
.filter(s -> s != null && !s.isBlank())
|
||||
.map(s -> {
|
||||
String trimmed = s.trim().toUpperCase();
|
||||
try {
|
||||
return ReadStatus.valueOf(trimmed);
|
||||
} catch (IllegalArgumentException e) {
|
||||
unknown.add(s.trim());
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
if (!unknown.isEmpty()) {
|
||||
throw new APIException("Invalid status values: " + unknown + ". Valid values: " + List.of(ReadStatus.values()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (parsed.isEmpty()) return cb.conjunction();
|
||||
|
||||
Subquery<Long> sub = query.subquery(Long.class);
|
||||
Root<UserBookProgressEntity> progressRoot = sub.from(UserBookProgressEntity.class);
|
||||
sub.select(progressRoot.get("book").get("id"))
|
||||
.where(
|
||||
cb.equal(progressRoot.get("user").get("id"), userId),
|
||||
progressRoot.get("readStatus").in(parsed)
|
||||
);
|
||||
|
||||
if ("not".equals(mode)) {
|
||||
return cb.not(root.get("id").in(sub));
|
||||
}
|
||||
return root.get("id").in(sub);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter books where the user's personal rating is >= minRating.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user