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:
+10
-2
@@ -800,8 +800,16 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
||||
case EntityType.LIBRARY:
|
||||
return this.fetchBooks(book => book.libraryId === entityId);
|
||||
case EntityType.SHELF:
|
||||
return this.fetchBooks(book =>
|
||||
book.shelves?.some(shelf => shelf.id === entityId) ?? false
|
||||
return this.shelfService.getBooksOnShelf(entityId).pipe(
|
||||
map(books => {
|
||||
const sortedBooks = this.sortService.applySort(books, this.bookSorter.selectedSort!);
|
||||
return {
|
||||
books: sortedBooks,
|
||||
loaded: true,
|
||||
error: null
|
||||
};
|
||||
}),
|
||||
switchMap(bookState => this.applyBookFilters(bookState))
|
||||
);
|
||||
case EntityType.MAGIC_SHELF:
|
||||
return this.fetchBooksMagicShelfBooks(entityId);
|
||||
|
||||
+13
-2
@@ -3,7 +3,7 @@ import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {Book} from '../../model/book.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {ShelfService} from '../../service/shelf.service';
|
||||
import {finalize, Observable} from 'rxjs';
|
||||
import {combineLatest, finalize, Observable} from 'rxjs';
|
||||
import {BookService} from '../../service/book.service';
|
||||
import {map, tap} from 'rxjs/operators';
|
||||
import {Shelf} from '../../model/shelf.model';
|
||||
@@ -14,6 +14,7 @@ import {Checkbox} from 'primeng/checkbox';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {BookDialogHelperService} from '../book-browser/book-dialog-helper.service';
|
||||
import {LoadingService} from '../../../../core/services/loading.service';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||
import {IconSelection} from '../../../../shared/service/icon-picker.service';
|
||||
|
||||
@@ -39,8 +40,18 @@ export class ShelfAssignerComponent implements OnInit {
|
||||
private bookService = inject(BookService);
|
||||
private bookDialogHelper = inject(BookDialogHelperService);
|
||||
private loadingService = inject(LoadingService);
|
||||
private userService = inject(UserService);
|
||||
|
||||
shelfState$: Observable<ShelfState> = combineLatest([
|
||||
this.shelfService.shelfState$,
|
||||
this.userService.userState$
|
||||
]).pipe(
|
||||
map(([state, userState]) => ({
|
||||
...state,
|
||||
shelves: state.shelves?.filter(s => s.userId === userState.user?.id) || []
|
||||
}))
|
||||
);
|
||||
|
||||
shelfState$: Observable<ShelfState> = this.shelfService.shelfState$;
|
||||
book: Book = this.dynamicDialogConfig.data.book;
|
||||
selectedShelves: Shelf[] = [];
|
||||
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
|
||||
|
||||
+15
@@ -88,6 +88,21 @@
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (isAdmin) {
|
||||
<div class="gradient-divider"></div>
|
||||
|
||||
<div class="form-group highlight-group">
|
||||
<label class="form-label">
|
||||
<i class="pi pi-globe label-icon"></i>
|
||||
Visibility
|
||||
</label>
|
||||
<div class="input-wrapper" style="display: flex; align-items: center; gap: 0.5rem;">
|
||||
<p-checkbox [(ngModel)]="isPublic" [binary]="true" inputId="publicShelf"></p-checkbox>
|
||||
<label for="publicShelf" style="cursor: pointer">Make this shelf public (read-only for others)</label>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="dialog-footer">
|
||||
|
||||
+9
-2
@@ -9,6 +9,8 @@ import {Button} from 'primeng/button';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {CheckboxModule} from 'primeng/checkbox';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shelf-creator',
|
||||
@@ -19,7 +21,8 @@ import {IconDisplayComponent} from '../../../../shared/components/icon-display/i
|
||||
Button,
|
||||
InputText,
|
||||
Tooltip,
|
||||
IconDisplayComponent
|
||||
IconDisplayComponent,
|
||||
CheckboxModule
|
||||
],
|
||||
styleUrl: './shelf-creator.component.scss',
|
||||
})
|
||||
@@ -28,9 +31,12 @@ export class ShelfCreatorComponent {
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
private messageService = inject(MessageService);
|
||||
private iconPickerService = inject(IconPickerService);
|
||||
private userService = inject(UserService);
|
||||
|
||||
shelfName: string = '';
|
||||
selectedIcon: IconSelection | null = null;
|
||||
isPublic: boolean = false;
|
||||
isAdmin: boolean = this.userService.getCurrentUser()?.permissions.admin ?? false;
|
||||
|
||||
openIconPicker(): void {
|
||||
this.iconPickerService.open().subscribe(icon => {
|
||||
@@ -55,7 +61,8 @@ export class ShelfCreatorComponent {
|
||||
const newShelf: Partial<Shelf> = {
|
||||
name: this.shelfName,
|
||||
icon: iconValue,
|
||||
iconType: iconType
|
||||
iconType: iconType,
|
||||
publicShelf: this.isPublic
|
||||
};
|
||||
|
||||
this.shelfService.createShelf(newShelf as Shelf).subscribe({
|
||||
|
||||
+10
@@ -47,6 +47,16 @@
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (isAdmin) {
|
||||
<div class="shelf-row">
|
||||
<label class="label">Visibility:</label>
|
||||
<div class="visibility-section" style="display: flex; align-items: center; gap: 0.5rem;">
|
||||
<p-checkbox [(ngModel)]="isPublic" [binary]="true" inputId="publicShelf"></p-checkbox>
|
||||
<label for="publicShelf" style="cursor: pointer; user-select: none;">Public Shelf</label>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
+10
-2
@@ -9,6 +9,8 @@ import {Shelf} from '../../model/shelf.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {IconPickerService, IconSelection} from '../../../../shared/service/icon-picker.service';
|
||||
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||
import {CheckboxModule} from 'primeng/checkbox';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-shelf-edit-dialog',
|
||||
@@ -17,7 +19,8 @@ import {IconDisplayComponent} from '../../../../shared/components/icon-display/i
|
||||
InputText,
|
||||
ReactiveFormsModule,
|
||||
FormsModule,
|
||||
IconDisplayComponent
|
||||
IconDisplayComponent,
|
||||
CheckboxModule
|
||||
],
|
||||
templateUrl: './shelf-edit-dialog.component.html',
|
||||
standalone: true,
|
||||
@@ -30,16 +33,20 @@ export class ShelfEditDialogComponent implements OnInit {
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
private messageService = inject(MessageService);
|
||||
private iconPickerService = inject(IconPickerService);
|
||||
private userService = inject(UserService);
|
||||
|
||||
shelfName: string = '';
|
||||
selectedIcon: IconSelection | null = null;
|
||||
shelf!: Shelf | undefined;
|
||||
isPublic: boolean = false;
|
||||
isAdmin: boolean = this.userService.getCurrentUser()?.permissions.admin ?? false;
|
||||
|
||||
ngOnInit(): void {
|
||||
const shelfId = this.dynamicDialogConfig?.data.shelfId;
|
||||
this.shelf = this.shelfService.getShelfById(shelfId);
|
||||
if (this.shelf) {
|
||||
this.shelfName = this.shelf.name;
|
||||
this.isPublic = this.shelf.publicShelf ?? false;
|
||||
if (this.shelf.iconType === 'PRIME_NG') {
|
||||
this.selectedIcon = {type: 'PRIME_NG', value: `pi pi-${this.shelf.icon}`};
|
||||
} else {
|
||||
@@ -67,7 +74,8 @@ export class ShelfEditDialogComponent implements OnInit {
|
||||
const shelf: Shelf = {
|
||||
name: this.shelfName,
|
||||
icon: iconValue,
|
||||
iconType: iconType
|
||||
iconType: iconType,
|
||||
publicShelf: this.isPublic
|
||||
};
|
||||
|
||||
this.shelfService.updateShelf(shelf, this.shelf?.id).subscribe({
|
||||
|
||||
@@ -6,4 +6,7 @@ export interface Shelf {
|
||||
icon: string;
|
||||
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
|
||||
sort?: SortOption;
|
||||
publicShelf?: boolean;
|
||||
userId?: number;
|
||||
bookCount?: number;
|
||||
}
|
||||
|
||||
@@ -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 =>
|
||||
|
||||
Reference in New Issue
Block a user