fix(books): make "select all" actually select all not just the books loaded in (#533)

This commit is contained in:
brios
2026-04-22 17:27:38 -04:00
committed by Zach Yale
parent 1f5f99d958
commit 13f282fe72
8 changed files with 98 additions and 14 deletions
@@ -31,6 +31,18 @@ public class AppBookController {
return ResponseEntity.ok(mobileBookService.getBooks(request));
}
@Operation(
summary = "Get all book IDs matching filters",
description = "Return all book IDs that match the given filters without pagination. Useful for bulk selection.",
operationId = "appGetAllBookIds"
)
@GetMapping("/ids")
public ResponseEntity<List<Long>> getAllBookIds(
@ModelAttribute BookListRequest request) {
return ResponseEntity.ok(mobileBookService.getAllBookIds(request));
}
@Operation(
summary = "Get app book details",
description = "Retrieve detailed app-facing information for a single book.",
@@ -129,6 +129,33 @@ public class AppBookService {
return buildPageResponse(bookPage, userId, pageNum, pageSize);
}
public List<Long> getAllBookIds(BookListRequest req) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
Long userId = user.getId();
Set<Long> accessibleLibraryIds = getAccessibleLibraryIds(user);
Specification<BookEntity> spec = buildSpecification(accessibleLibraryIds, userId, req);
if (req.magicShelfId() != null) {
spec = spec.and(magicShelfBookService.toSpecification(userId, req.magicShelfId()));
}
if (Boolean.TRUE.equals(req.unshelved())) {
spec = spec.and(AppBookSpecification.unshelved());
}
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<BookEntity> root = cq.from(BookEntity.class);
cq.select(root.get("id"));
if (spec != null) {
cq.where(spec.toPredicate(root, cq, cb));
}
return entityManager.createQuery(cq).getResultList();
}
public AppBookDetail getBookDetail(Long bookId) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
Long userId = user.getId();
@@ -21,6 +21,11 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.enums.PermissionType;
import org.booklore.model.websocket.LogNotification;
import org.booklore.model.websocket.Topic;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
@@ -51,6 +56,7 @@ class BookCoverServiceTest {
@Mock private MetadataWriterFactory metadataWriterFactory;
@Mock private TransactionTemplate transactionTemplate;
@Mock private Executor taskExecutor;
@Mock private AuthenticationService authenticationService;
@InjectMocks
private BookCoverService service;
@@ -58,6 +64,7 @@ class BookCoverServiceTest {
@BeforeEach
void setUp() {
lenient().when(appProperties.isLocalStorage()).thenReturn(true);
lenient().when(authenticationService.getAuthenticatedUser()).thenReturn(BookLoreUser.builder().username("testuser").build());
}
private BookEntity buildBook(long id, boolean coverLocked) {
@@ -848,10 +848,20 @@ export class BookBrowserComponent implements AfterViewInit {
}
selectAllBooks(): void {
this.bookSelectionService.selectAll();
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
this.appBooksApi.fetchAllBookIds().pipe(take(1)).subscribe({
next: (allIds) => {
this.bookSelectionService.selectAll(allIds);
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
},
error: () => {
this.bookSelectionService.selectAll();
if (this.bookTableComponent) {
this.bookTableComponent.selectAllBooks();
}
}
});
}
deselectAllBooks(): void {
@@ -76,4 +76,13 @@ describe('BookSelectionService', () => {
expect(service.selectedBooks()).toEqual(new Set());
expect(service.selectedCount()).toBe(0);
});
it('selects all from provided book IDs when passed to selectAll', () => {
const books = [makeBook(1), makeBook(2)];
service.setCurrentBooks(books);
service.selectAll([10, 20, 30, 40, 50]);
expect(service.selectedBooks()).toEqual(new Set([10, 20, 30, 40, 50]));
expect(service.selectedCount()).toBe(5);
});
});
@@ -72,7 +72,12 @@ export class BookSelectionService {
}
}
selectAll(): void {
selectAll(allBookIds?: number[]): void {
if (allBookIds && allBookIds.length > 0) {
this._selectedBooks.set(new Set(allBookIds));
return;
}
if (!this.currentBooks || this.currentBooks.length === 0) return;
this._selectedBooks.update(current => {
@@ -1,6 +1,6 @@
import {computed, inject, Injectable, signal} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {lastValueFrom} from 'rxjs';
import {lastValueFrom, Observable} from 'rxjs';
import {injectInfiniteQuery, injectQuery, QueryClient} from '@tanstack/angular-query-experimental';
import {API_CONFIG} from '../../../core/config/api-config';
import {AuthService} from '../../../shared/service/auth.service';
@@ -143,6 +143,12 @@ export class AppBooksApiService {
this.booksQuery.fetchNextPage();
}
/** Fetch all book IDs matching the current filters (no pagination). */
fetchAllBookIds(): Observable<number[]> {
const params = this.buildFilterParams();
return this.http.get<number[]>(`${this.booksUrl}/ids`, {params});
}
/** Invalidate the books query to force a refresh from the server. */
invalidate(): void {
void this.queryClient.invalidateQueries({queryKey: ['app-books']});
@@ -150,15 +156,20 @@ export class AppBooksApiService {
}
private buildParams(page: number): HttpParams {
const filters = this._filters();
const sort = this._sort();
const search = this._search();
let params = new HttpParams()
return this.buildFilterParams()
.set('page', page.toString())
.set('size', PAGE_SIZE.toString())
.set('sort', sort.field)
.set('dir', sort.dir);
}
private buildFilterParams(): HttpParams {
const filters = this._filters();
const search = this._search();
let params = new HttpParams();
if (search) params = params.set('search', search);
if (filters.libraryId) params = params.set('libraryId', filters.libraryId.toString());
@@ -1,5 +1,5 @@
import {Subject} from 'rxjs';
import {TestBed} from '@angular/core/testing';
import {TestBed, ComponentFixture} from '@angular/core/testing';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {TranslocoService} from '@jsverse/transloco';
@@ -12,29 +12,32 @@ describe('LiveNotificationBoxComponent', () => {
let notifications$: Subject<LogNotification>;
let langChanges$: Subject<string>;
let component: LiveNotificationBoxComponent;
let fixture: ComponentFixture<LiveNotificationBoxComponent>;
beforeEach(() => {
notifications$ = new Subject<LogNotification>();
langChanges$ = new Subject<string>();
TestBed.configureTestingModule({
imports: [LiveNotificationBoxComponent],
providers: [
LiveNotificationBoxComponent,
{
provide: NotificationEventService,
useValue: {latestNotification$: notifications$},
useValue: {latestNotification$: notifications$.asObservable()},
},
{
provide: TranslocoService,
useValue: {
translate: (key: string) => `translated:${key}`,
langChanges$,
langChanges$: langChanges$.asObservable(),
},
},
],
});
component = TestBed.inject(LiveNotificationBoxComponent);
fixture = TestBed.createComponent(LiveNotificationBoxComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
afterEach(() => {