refactor(books): add /api/v1/books/ids select-all endpoint (#1899)
This commit is contained in:
@@ -118,6 +118,21 @@ public class BookController {
|
||||
return ResponseEntity.ok(bookFacetService.getFacets(facet, facetLogic, query));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get matching book ids", description = "Returns every book id matching the given sort, facet, facet_logic, and query parameters, in sort order. For select-all over the current filters.")
|
||||
@ApiResponse(responseCode = "200", description = "Matching book ids returned successfully")
|
||||
@GetMapping("/ids")
|
||||
public ResponseEntity<List<Long>> getBookIds(
|
||||
@Parameter(description = "Comma-separated sort keys; prefix a key with '-' for descending")
|
||||
@RequestParam(required = false) String sort,
|
||||
@Parameter(description = "Facet selection in key:value form; repeatable")
|
||||
@RequestParam(required = false) List<String> facet,
|
||||
@Parameter(description = "How facet values combine within a group: and, or, or not")
|
||||
@RequestParam(name = "facet_logic", required = false) String facetLogic,
|
||||
@Parameter(description = "Free-text search")
|
||||
@RequestParam(required = false) String query) {
|
||||
return ResponseEntity.ok(bookBrowseService.findAllIds(sort, facet, facetLogic, query));
|
||||
}
|
||||
|
||||
@Operation(summary = "Get a book by ID", description = "Retrieve details of a specific book by its ID.")
|
||||
@ApiResponses({
|
||||
@ApiResponse(responseCode = "200", description = "Book details returned successfully"),
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package org.booklore.service.browse;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.booklore.browse.BrowsePage;
|
||||
import org.booklore.browse.CursorCodec;
|
||||
@@ -48,6 +54,9 @@ public class BookBrowseService {
|
||||
private final CursorCodec cursorCodec;
|
||||
private final LinksBuilder linksBuilder;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public BrowsePage<Book> browse(String sort, List<String> facet, String facetLogicParam, String query, String cursor, Pageable pageable) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
Long userId = user.getId();
|
||||
@@ -92,6 +101,29 @@ public class BookBrowseService {
|
||||
return BrowsePage.of(page.getContent(), offset, limit, page.getTotalElements(), currentCursor, links);
|
||||
}
|
||||
|
||||
public List<Long> findAllIds(String sort, List<String> facet, String facetLogicParam, String query) {
|
||||
BookLoreUser user = authenticationService.getAuthenticatedUser();
|
||||
Long userId = user.getId();
|
||||
boolean isAdmin = user.getPermissions().isAdmin();
|
||||
|
||||
Map<String, List<String>> facets = BookFilterSpecifications.parseFacets(facet);
|
||||
FacetLogic facetLogic = FacetLogic.from(facetLogicParam);
|
||||
List<SortTerm> sortTerms = SortParser.parse(sort, sortRegistry.registry().keys());
|
||||
Specification<BookEntity> filter = filterSpecifications.base(query, facets, facetLogic, userId, isAdmin, BookFilterSpecifications.libraryIds(user), null);
|
||||
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
|
||||
Root<BookEntity> root = cq.from(BookEntity.class);
|
||||
cq.select(root.get("id"));
|
||||
Predicate predicate = filter.toPredicate(root, cq, cb);
|
||||
if (predicate != null) {
|
||||
cq.where(predicate);
|
||||
}
|
||||
cq.orderBy(sortRegistry.registry().toOrders(sortTerms, root, cq, cb, userId));
|
||||
|
||||
return entityManager.createQuery(cq).getResultList();
|
||||
}
|
||||
|
||||
public BrowsePage<Book> wrapLegacy(Page<Book> page, Pageable pageable) {
|
||||
long offset = pageable.getOffset();
|
||||
int limit = pageable.getPageSize();
|
||||
|
||||
Reference in New Issue
Block a user