fix(api): include physical books in file type facet (#2687)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.booklore.app.specification;
|
||||
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.exception.ApiError;
|
||||
import org.booklore.model.entity.*;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.model.enums.ComicCreatorRole;
|
||||
@@ -19,6 +20,8 @@ import java.util.function.Function;
|
||||
|
||||
public class AppBookSpecification {
|
||||
|
||||
private static final String PHYSICAL_FILE_TYPE = "PHYSICAL";
|
||||
|
||||
private AppBookSpecification() {
|
||||
}
|
||||
|
||||
@@ -240,11 +243,27 @@ 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
|
||||
* PHYSICAL matches the book's physical flag, alongside any digital file types.
|
||||
* OR = books matching ANY listed format
|
||||
* AND = books matching ALL listed formats
|
||||
* NOT = books matching NONE of the listed formats
|
||||
*/
|
||||
public static Specification<BookEntity> withFileTypes(List<String> fileTypes, String mode) {
|
||||
if (fileTypes.stream().anyMatch(s -> s != null && PHYSICAL_FILE_TYPE.equalsIgnoreCase(s.trim()))) {
|
||||
List<String> digitalTypes = fileTypes.stream()
|
||||
.filter(s -> s != null && !s.isBlank() && !PHYSICAL_FILE_TYPE.equalsIgnoreCase(s.trim()))
|
||||
.toList();
|
||||
Specification<BookEntity> physical = (root, query, cb) ->
|
||||
cb.isTrue(root.get("isPhysical"));
|
||||
if ("not".equals(mode)) {
|
||||
physical = Specification.not(physical);
|
||||
}
|
||||
if (digitalTypes.isEmpty()) {
|
||||
return physical;
|
||||
}
|
||||
Specification<BookEntity> digital = withFileTypes(digitalTypes, mode);
|
||||
return "and".equals(mode) || "not".equals(mode) ? physical.and(digital) : physical.or(digital);
|
||||
}
|
||||
return (root, query, cb) -> {
|
||||
List<String> unknown = new ArrayList<>();
|
||||
List<BookFileType> parsed = fileTypes.stream()
|
||||
@@ -261,7 +280,7 @@ public class AppBookSpecification {
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
if (!unknown.isEmpty()) {
|
||||
throw new APIException("Invalid fileType values: " + unknown + ". Valid values: " + List.of(BookFileType.values()), HttpStatus.BAD_REQUEST);
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Invalid fileType values: " + unknown + ". Valid values: " + List.of(BookFileType.values()) + ", " + PHYSICAL_FILE_TYPE);
|
||||
}
|
||||
if (parsed.isEmpty()) return cb.conjunction();
|
||||
|
||||
|
||||
@@ -33,9 +33,11 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -46,6 +48,11 @@ public class BookFacetService {
|
||||
private static final String FACET_PATH = "/api/v1/books/facets";
|
||||
private static final int MAX_VALUES = 100;
|
||||
|
||||
private static final FacetDef PHYSICAL_FILE_TYPE = new FacetDef("file_type", "File Type", (cb, root, userId) ->
|
||||
cb.<String>selectCase()
|
||||
.when(cb.isTrue(root.get("isPhysical")), "PHYSICAL")
|
||||
.otherwise(cb.nullLiteral(String.class)));
|
||||
|
||||
private static final List<FacetDef> FACETS = List.of(
|
||||
new FacetDef("author", "Authors", (cb, root, userId) -> metadata(root).join("authors", JoinType.LEFT).get("name")),
|
||||
new FacetDef("genre", "Genre", (cb, root, userId) -> metadata(root).join("categories", JoinType.LEFT).get("name")),
|
||||
@@ -128,7 +135,13 @@ public class BookFacetService {
|
||||
groups.add(sortGroup(preserved));
|
||||
for (FacetDef def : FACETS) {
|
||||
Specification<BookEntity> base = filterSpecifications.base(query, facets, facetLogic, userId, isAdmin, libraryIds, def.key());
|
||||
groups.add(toGroup(def, count(def, base, userId), facet, preserved));
|
||||
List<FacetCount> counts = count(def, base, userId);
|
||||
if ("file_type".equals(def.key())) {
|
||||
counts = Stream.concat(counts.stream(), count(PHYSICAL_FILE_TYPE, base, userId).stream())
|
||||
.sorted(Comparator.comparingLong(FacetCount::count).reversed().thenComparing(FacetCount::value))
|
||||
.toList();
|
||||
}
|
||||
groups.add(toGroup(def, counts, facet, preserved));
|
||||
}
|
||||
List<Link> links = List.of(Link.json(List.of("self"), href(FACET_PATH, preserved)));
|
||||
return new FacetGroupsResponse(links, groups);
|
||||
|
||||
Reference in New Issue
Block a user