Implement Public Shelves (#1969)

* Implement Public Shelves

Multiple changes to support shelves being marked as public.

- **Backend:**
    - Added 'isPublic' field the Shelves
    - Made it so that when looking for shelves it includes public ones
    - Allow admins to mark shelves are public
    - Added 'bookCount' to 'Shelf' so it shows for public shelves as
well
    - Added 'canReadShelf' to 'SecurityUtil' for checking read access to
public shelves.
    - Updated ShelfController to use this for getting shelf data

- **Frontend:**
    - Updated Shelf to include 'publicShelf', 'userId', and 'bookCount'
    - Updated components to allow admins to toggle shelf visibility.
    - Modified 'ShelfService' to:
        - Add 'getBooksOnShelf' method to fetch books from the API.
        - Update /getBookCount' to use 'Shelf.bookCount' for non-owners.
    - Restricted edit/delete options to shelf owners.

* Renumber Add_is_public_to_shelf.sql

Conflicted with another

---------

Co-authored-by: ACX <8075870+acx10@users.noreply.github.com>
This commit is contained in:
Chris Debenham
2026-01-15 21:19:05 -07:00
committed by GitHub
co-authored by ACX
parent c20b1972f8
commit 36d9f74864
18 changed files with 139 additions and 18 deletions
@@ -135,14 +135,20 @@ export class LibraryShelfMenuService {
];
}
initializeShelfMenuItems(entity: Shelf | null): MenuItem[] {
initializeShelfMenuItems(entity: any): MenuItem[] {
const user = this.userService.getCurrentUser();
const isOwner = entity?.userId === user?.id;
const isPublicShelf = entity?.publicShelf ?? false;
const disableOptions = !isOwner;
return [
{
label: 'Options',
label: (isPublicShelf ? 'Public Shelf - ' : '') + (disableOptions ? 'Read only' : 'Options'),
items: [
{
label: 'Edit Shelf',
icon: 'pi pi-pen-to-square',
disabled: disableOptions,
command: () => {
this.dialogLauncherService.openShelfEditDialog((entity?.id as number));
}
@@ -153,6 +159,7 @@ export class LibraryShelfMenuService {
{
label: 'Delete Shelf',
icon: 'pi pi-trash',
disabled: disableOptions,
command: () => {
this.confirmationService.confirm({
message: `Are you sure you want to delete shelf: ${entity?.name}?`,
@@ -1,18 +1,21 @@
import {inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {BehaviorSubject, combineLatest, Observable, of} from 'rxjs';
import {tap, catchError, map, shareReplay, finalize} from 'rxjs/operators';
import {Shelf} from '../model/shelf.model';
import {ShelfState} from '../model/state/shelf-state.model';
import {BookService} from './book.service';
import {API_CONFIG} from '../../../core/config/api-config';
import {Book} from '../model/book.model';
import {UserService} from '../../settings/user-management/user.service';
@Injectable({providedIn: 'root'})
export class ShelfService {
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelves`;
private http = inject(HttpClient);
private bookService = inject(BookService);
private userService = inject(UserService);
private shelfStateSubject = new BehaviorSubject<ShelfState>({
shelves: null,
@@ -101,13 +104,30 @@ export class ShelfService {
}
getBookCount(shelfId: number): Observable<number> {
return this.bookService.bookState$.pipe(
map(state =>
(state.books || []).filter(b => b.shelves?.some(s => s.id === shelfId)).length
)
return combineLatest([
this.shelfState$,
this.userService.userState$,
this.bookService.bookState$
]).pipe(
map(([shelfState, userState, bookState]) => {
const shelf = shelfState.shelves?.find(s => s.id === shelfId);
if (!shelf) return 0;
const isOwner = userState.user?.id === shelf.userId;
if (isOwner) {
return (bookState.books || []).filter(b => b.shelves?.some(s => s.id === shelfId)).length;
} else {
return shelf.bookCount || 0;
}
})
);
}
getBooksOnShelf(shelfId: number): Observable<Book[]> {
return this.http.get<Book[]>(`${this.url}/${shelfId}/books`);
}
getUnshelvedBookCount(): Observable<number> {
return this.bookService.bookState$.pipe(
map(state =>