refactor(books): add /api/v1/books/ids select-all endpoint (#1899)

This commit is contained in:
Zach
2026-07-24 10:48:06 +01:00
committed by GitHub
parent 9741c6d06e
commit 30d8760bd9
3 changed files with 85 additions and 0 deletions
@@ -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();
@@ -227,6 +227,44 @@ class BookBrowseServiceTest {
.hasMessageContaining("Unknown sort key");
}
@Test
void findAllIdsMatchesPageOrderingForSameFilters() {
for (int i = 0; i < 5; i++) {
book(String.format("Book%02d", i), List.of());
}
em.flush();
List<Long> pageIds = browse("title", null, null, null, 0, 20).content().stream().map(Book::getId).toList();
List<Long> allIds = browseService.findAllIds("title", null, null, null);
assertThat(allIds).isEqualTo(pageIds);
}
@Test
void findAllIdsAppliesFacets() {
Long horror = book("H", List.of("Horror")).getId();
book("R", List.of("Romance"));
em.flush();
assertThat(browseService.findAllIds(null, List.of("genre:Horror"), null, null)).containsExactly(horror);
}
@Test
void findAllIdsRespectsLibraryScoping() {
Long inLibrary = book("InLibrary", List.of()).getId();
LibraryEntity otherLibrary = LibraryEntity.builder().name("Other").icon("book").watch(false)
.formatPriority(List.of(BookFileType.EPUB)).build();
em.persist(otherLibrary);
LibraryPathEntity otherPath = LibraryPathEntity.builder().library(otherLibrary).path("/o").build();
em.persist(otherPath);
BookEntity outside = BookEntity.builder()
.library(otherLibrary).libraryPath(otherPath).addedOn(Instant.now()).deleted(false).build();
em.persist(outside);
em.persist(BookMetadataEntity.builder().book(outside).title("Outside").build());
em.flush();
assertThat(browseService.findAllIds(null, null, null, null)).containsExactly(inLibrary);
}
@Test
void wrapLegacyAddsCursorAndLinksToExistingPage() {
Book book = Book.builder().id(1L).build();