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
@@ -129,4 +129,14 @@ public class SecurityUtil {
|
|||||||
var user = getCurrentUser();
|
var user = getCurrentUser();
|
||||||
return user != null && user.getPermissions().isCanAccessTaskManager();
|
return user != null && user.getPermissions().isCanAccessTaskManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean canReadShelf(Long shelfId) {
|
||||||
|
var user = getCurrentUser();
|
||||||
|
if (user != null) {
|
||||||
|
return shelfRepository.findById(shelfId)
|
||||||
|
.map(shelf -> shelf.isPublic() || shelf.getUser().getId().equals(user.getId()))
|
||||||
|
.orElse(false);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -39,7 +39,7 @@ public class ShelfController {
|
|||||||
@ApiResponse(responseCode = "403", description = "Forbidden")
|
@ApiResponse(responseCode = "403", description = "Forbidden")
|
||||||
})
|
})
|
||||||
@GetMapping("/{shelfId}")
|
@GetMapping("/{shelfId}")
|
||||||
@PreAuthorize("@securityUtil.isShelfOwner(#shelfId)")
|
@PreAuthorize("@securityUtil.canReadShelf(#shelfId)")
|
||||||
public ResponseEntity<Shelf> getShelf(
|
public ResponseEntity<Shelf> getShelf(
|
||||||
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
|
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
|
||||||
return ResponseEntity.ok(shelfService.getShelf(shelfId));
|
return ResponseEntity.ok(shelfService.getShelf(shelfId));
|
||||||
@@ -76,7 +76,7 @@ public class ShelfController {
|
|||||||
@Operation(summary = "Get books on a shelf", description = "Retrieve all books assigned to a specific shelf.")
|
@Operation(summary = "Get books on a shelf", description = "Retrieve all books assigned to a specific shelf.")
|
||||||
@ApiResponse(responseCode = "200", description = "Books returned successfully")
|
@ApiResponse(responseCode = "200", description = "Books returned successfully")
|
||||||
@GetMapping("/{shelfId}/books")
|
@GetMapping("/{shelfId}/books")
|
||||||
@PreAuthorize("@securityUtil.isShelfOwner(#shelfId)")
|
@PreAuthorize("@securityUtil.canReadShelf(#shelfId)")
|
||||||
public ResponseEntity<List<Book>> getShelfBooks(
|
public ResponseEntity<List<Book>> getShelfBooks(
|
||||||
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
|
@Parameter(description = "ID of the shelf") @PathVariable Long shelfId) {
|
||||||
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
|
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
|
||||||
|
|||||||
@@ -9,5 +9,7 @@ import org.mapstruct.Mapping;
|
|||||||
public interface ShelfMapper {
|
public interface ShelfMapper {
|
||||||
|
|
||||||
@Mapping(source = "user.id", target = "userId")
|
@Mapping(source = "user.id", target = "userId")
|
||||||
|
@Mapping(source = "public", target = "publicShelf")
|
||||||
|
@Mapping(target = "bookCount", expression = "java(shelfEntity.getBookEntities() != null ? shelfEntity.getBookEntities().size() : 0)")
|
||||||
Shelf toShelf(ShelfEntity shelfEntity);
|
Shelf toShelf(ShelfEntity shelfEntity);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,4 +15,6 @@ public class Shelf {
|
|||||||
private IconType iconType;
|
private IconType iconType;
|
||||||
private Sort sort;
|
private Sort sort;
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
private boolean publicShelf;
|
||||||
|
private int bookCount;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -21,4 +21,6 @@ public class ShelfCreateRequest {
|
|||||||
|
|
||||||
@NotNull(message = "Shelf icon type must not be null.")
|
@NotNull(message = "Shelf icon type must not be null.")
|
||||||
private IconType iconType;
|
private IconType iconType;
|
||||||
|
|
||||||
|
private boolean publicShelf;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,10 @@ public class ShelfEntity {
|
|||||||
@Builder.Default
|
@Builder.Default
|
||||||
private IconType iconType = IconType.PRIME_NG;
|
private IconType iconType = IconType.PRIME_NG;
|
||||||
|
|
||||||
|
@Column(name = "is_public", nullable = false)
|
||||||
|
@Builder.Default
|
||||||
|
private boolean isPublic = false;
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.LAZY)
|
@ManyToMany(fetch = FetchType.LAZY)
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "book_shelf_mapping",
|
name = "book_shelf_mapping",
|
||||||
|
|||||||
@@ -16,5 +16,8 @@ public interface ShelfRepository extends JpaRepository<ShelfEntity, Long> {
|
|||||||
|
|
||||||
Optional<ShelfEntity> findByUserIdAndName(Long id, String name);
|
Optional<ShelfEntity> findByUserIdAndName(Long id, String name);
|
||||||
|
|
||||||
|
@org.springframework.data.jpa.repository.Query("SELECT s FROM ShelfEntity s WHERE s.user.id = :userId OR s.isPublic = true")
|
||||||
|
List<ShelfEntity> findByUserIdOrPublicShelfTrue(@org.springframework.data.repository.query.Param("userId") Long userId);
|
||||||
|
|
||||||
List<ShelfEntity> findByUserIdInAndName(List<Long> userIds, String name);
|
List<ShelfEntity> findByUserIdInAndName(List<Long> userIds, String name);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,10 +37,14 @@ public class ShelfService {
|
|||||||
if (shelfRepository.existsByUserIdAndName(userId, request.getName())) {
|
if (shelfRepository.existsByUserIdAndName(userId, request.getName())) {
|
||||||
throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName());
|
throw ApiError.SHELF_ALREADY_EXISTS.createException(request.getName());
|
||||||
}
|
}
|
||||||
|
if (request.isPublicShelf() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
|
||||||
|
throw new org.springframework.security.access.AccessDeniedException("Only admins can create public shelves");
|
||||||
|
}
|
||||||
ShelfEntity shelfEntity = ShelfEntity.builder()
|
ShelfEntity shelfEntity = ShelfEntity.builder()
|
||||||
.icon(request.getIcon())
|
.icon(request.getIcon())
|
||||||
.name(request.getName())
|
.name(request.getName())
|
||||||
.iconType(request.getIconType())
|
.iconType(request.getIconType())
|
||||||
|
.isPublic(request.isPublicShelf())
|
||||||
.user(fetchUserEntityById(userId))
|
.user(fetchUserEntityById(userId))
|
||||||
.build();
|
.build();
|
||||||
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
|
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
|
||||||
@@ -48,15 +52,19 @@ public class ShelfService {
|
|||||||
|
|
||||||
public Shelf updateShelf(Long id, ShelfCreateRequest request) {
|
public Shelf updateShelf(Long id, ShelfCreateRequest request) {
|
||||||
ShelfEntity shelfEntity = findShelfByIdOrThrow(id);
|
ShelfEntity shelfEntity = findShelfByIdOrThrow(id);
|
||||||
|
if (request.isPublicShelf() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
|
||||||
|
throw new org.springframework.security.access.AccessDeniedException("Only admins can create public shelves");
|
||||||
|
}
|
||||||
shelfEntity.setName(request.getName());
|
shelfEntity.setName(request.getName());
|
||||||
shelfEntity.setIcon(request.getIcon());
|
shelfEntity.setIcon(request.getIcon());
|
||||||
shelfEntity.setIconType(request.getIconType());
|
shelfEntity.setIconType(request.getIconType());
|
||||||
|
shelfEntity.setPublic(request.isPublicShelf());
|
||||||
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
|
return shelfMapper.toShelf(shelfRepository.save(shelfEntity));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Shelf> getShelves() {
|
public List<Shelf> getShelves() {
|
||||||
Long userId = getAuthenticatedUserId();
|
Long userId = getAuthenticatedUserId();
|
||||||
return shelfRepository.findByUserId(userId).stream()
|
return shelfRepository.findByUserIdOrPublicShelfTrue(userId).stream()
|
||||||
.map(shelfMapper::toShelf)
|
.map(shelfMapper::toShelf)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE shelf ADD COLUMN is_public BOOLEAN NOT NULL DEFAULT FALSE;
|
||||||
+10
-2
@@ -800,8 +800,16 @@ export class BookBrowserComponent implements OnInit, AfterViewInit {
|
|||||||
case EntityType.LIBRARY:
|
case EntityType.LIBRARY:
|
||||||
return this.fetchBooks(book => book.libraryId === entityId);
|
return this.fetchBooks(book => book.libraryId === entityId);
|
||||||
case EntityType.SHELF:
|
case EntityType.SHELF:
|
||||||
return this.fetchBooks(book =>
|
return this.shelfService.getBooksOnShelf(entityId).pipe(
|
||||||
book.shelves?.some(shelf => shelf.id === entityId) ?? false
|
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:
|
case EntityType.MAGIC_SHELF:
|
||||||
return this.fetchBooksMagicShelfBooks(entityId);
|
return this.fetchBooksMagicShelfBooks(entityId);
|
||||||
|
|||||||
+13
-2
@@ -3,7 +3,7 @@ import {DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
|
|||||||
import {Book} from '../../model/book.model';
|
import {Book} from '../../model/book.model';
|
||||||
import {MessageService} from 'primeng/api';
|
import {MessageService} from 'primeng/api';
|
||||||
import {ShelfService} from '../../service/shelf.service';
|
import {ShelfService} from '../../service/shelf.service';
|
||||||
import {finalize, Observable} from 'rxjs';
|
import {combineLatest, finalize, Observable} from 'rxjs';
|
||||||
import {BookService} from '../../service/book.service';
|
import {BookService} from '../../service/book.service';
|
||||||
import {map, tap} from 'rxjs/operators';
|
import {map, tap} from 'rxjs/operators';
|
||||||
import {Shelf} from '../../model/shelf.model';
|
import {Shelf} from '../../model/shelf.model';
|
||||||
@@ -14,6 +14,7 @@ import {Checkbox} from 'primeng/checkbox';
|
|||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {BookDialogHelperService} from '../book-browser/book-dialog-helper.service';
|
import {BookDialogHelperService} from '../book-browser/book-dialog-helper.service';
|
||||||
import {LoadingService} from '../../../../core/services/loading.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 {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||||
import {IconSelection} from '../../../../shared/service/icon-picker.service';
|
import {IconSelection} from '../../../../shared/service/icon-picker.service';
|
||||||
|
|
||||||
@@ -39,8 +40,18 @@ export class ShelfAssignerComponent implements OnInit {
|
|||||||
private bookService = inject(BookService);
|
private bookService = inject(BookService);
|
||||||
private bookDialogHelper = inject(BookDialogHelperService);
|
private bookDialogHelper = inject(BookDialogHelperService);
|
||||||
private loadingService = inject(LoadingService);
|
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;
|
book: Book = this.dynamicDialogConfig.data.book;
|
||||||
selectedShelves: Shelf[] = [];
|
selectedShelves: Shelf[] = [];
|
||||||
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
|
bookIds: Set<number> = this.dynamicDialogConfig.data.bookIds;
|
||||||
|
|||||||
+15
@@ -88,6 +88,21 @@
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
</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>
|
||||||
|
|
||||||
<div class="dialog-footer">
|
<div class="dialog-footer">
|
||||||
|
|||||||
+9
-2
@@ -9,6 +9,8 @@ import {Button} from 'primeng/button';
|
|||||||
import {InputText} from 'primeng/inputtext';
|
import {InputText} from 'primeng/inputtext';
|
||||||
import {Tooltip} from 'primeng/tooltip';
|
import {Tooltip} from 'primeng/tooltip';
|
||||||
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||||
|
import {UserService} from '../../../settings/user-management/user.service';
|
||||||
|
import {CheckboxModule} from 'primeng/checkbox';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-shelf-creator',
|
selector: 'app-shelf-creator',
|
||||||
@@ -19,7 +21,8 @@ import {IconDisplayComponent} from '../../../../shared/components/icon-display/i
|
|||||||
Button,
|
Button,
|
||||||
InputText,
|
InputText,
|
||||||
Tooltip,
|
Tooltip,
|
||||||
IconDisplayComponent
|
IconDisplayComponent,
|
||||||
|
CheckboxModule
|
||||||
],
|
],
|
||||||
styleUrl: './shelf-creator.component.scss',
|
styleUrl: './shelf-creator.component.scss',
|
||||||
})
|
})
|
||||||
@@ -28,9 +31,12 @@ export class ShelfCreatorComponent {
|
|||||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||||
private messageService = inject(MessageService);
|
private messageService = inject(MessageService);
|
||||||
private iconPickerService = inject(IconPickerService);
|
private iconPickerService = inject(IconPickerService);
|
||||||
|
private userService = inject(UserService);
|
||||||
|
|
||||||
shelfName: string = '';
|
shelfName: string = '';
|
||||||
selectedIcon: IconSelection | null = null;
|
selectedIcon: IconSelection | null = null;
|
||||||
|
isPublic: boolean = false;
|
||||||
|
isAdmin: boolean = this.userService.getCurrentUser()?.permissions.admin ?? false;
|
||||||
|
|
||||||
openIconPicker(): void {
|
openIconPicker(): void {
|
||||||
this.iconPickerService.open().subscribe(icon => {
|
this.iconPickerService.open().subscribe(icon => {
|
||||||
@@ -55,7 +61,8 @@ export class ShelfCreatorComponent {
|
|||||||
const newShelf: Partial<Shelf> = {
|
const newShelf: Partial<Shelf> = {
|
||||||
name: this.shelfName,
|
name: this.shelfName,
|
||||||
icon: iconValue,
|
icon: iconValue,
|
||||||
iconType: iconType
|
iconType: iconType,
|
||||||
|
publicShelf: this.isPublic
|
||||||
};
|
};
|
||||||
|
|
||||||
this.shelfService.createShelf(newShelf as Shelf).subscribe({
|
this.shelfService.createShelf(newShelf as Shelf).subscribe({
|
||||||
|
|||||||
+10
@@ -47,6 +47,16 @@
|
|||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -9,6 +9,8 @@ import {Shelf} from '../../model/shelf.model';
|
|||||||
import {MessageService} from 'primeng/api';
|
import {MessageService} from 'primeng/api';
|
||||||
import {IconPickerService, IconSelection} from '../../../../shared/service/icon-picker.service';
|
import {IconPickerService, IconSelection} from '../../../../shared/service/icon-picker.service';
|
||||||
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
import {IconDisplayComponent} from '../../../../shared/components/icon-display/icon-display.component';
|
||||||
|
import {CheckboxModule} from 'primeng/checkbox';
|
||||||
|
import {UserService} from '../../../settings/user-management/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-shelf-edit-dialog',
|
selector: 'app-shelf-edit-dialog',
|
||||||
@@ -17,7 +19,8 @@ import {IconDisplayComponent} from '../../../../shared/components/icon-display/i
|
|||||||
InputText,
|
InputText,
|
||||||
ReactiveFormsModule,
|
ReactiveFormsModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
IconDisplayComponent
|
IconDisplayComponent,
|
||||||
|
CheckboxModule
|
||||||
],
|
],
|
||||||
templateUrl: './shelf-edit-dialog.component.html',
|
templateUrl: './shelf-edit-dialog.component.html',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
@@ -30,16 +33,20 @@ export class ShelfEditDialogComponent implements OnInit {
|
|||||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||||
private messageService = inject(MessageService);
|
private messageService = inject(MessageService);
|
||||||
private iconPickerService = inject(IconPickerService);
|
private iconPickerService = inject(IconPickerService);
|
||||||
|
private userService = inject(UserService);
|
||||||
|
|
||||||
shelfName: string = '';
|
shelfName: string = '';
|
||||||
selectedIcon: IconSelection | null = null;
|
selectedIcon: IconSelection | null = null;
|
||||||
shelf!: Shelf | undefined;
|
shelf!: Shelf | undefined;
|
||||||
|
isPublic: boolean = false;
|
||||||
|
isAdmin: boolean = this.userService.getCurrentUser()?.permissions.admin ?? false;
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
const shelfId = this.dynamicDialogConfig?.data.shelfId;
|
const shelfId = this.dynamicDialogConfig?.data.shelfId;
|
||||||
this.shelf = this.shelfService.getShelfById(shelfId);
|
this.shelf = this.shelfService.getShelfById(shelfId);
|
||||||
if (this.shelf) {
|
if (this.shelf) {
|
||||||
this.shelfName = this.shelf.name;
|
this.shelfName = this.shelf.name;
|
||||||
|
this.isPublic = this.shelf.publicShelf ?? false;
|
||||||
if (this.shelf.iconType === 'PRIME_NG') {
|
if (this.shelf.iconType === 'PRIME_NG') {
|
||||||
this.selectedIcon = {type: 'PRIME_NG', value: `pi pi-${this.shelf.icon}`};
|
this.selectedIcon = {type: 'PRIME_NG', value: `pi pi-${this.shelf.icon}`};
|
||||||
} else {
|
} else {
|
||||||
@@ -67,7 +74,8 @@ export class ShelfEditDialogComponent implements OnInit {
|
|||||||
const shelf: Shelf = {
|
const shelf: Shelf = {
|
||||||
name: this.shelfName,
|
name: this.shelfName,
|
||||||
icon: iconValue,
|
icon: iconValue,
|
||||||
iconType: iconType
|
iconType: iconType,
|
||||||
|
publicShelf: this.isPublic
|
||||||
};
|
};
|
||||||
|
|
||||||
this.shelfService.updateShelf(shelf, this.shelf?.id).subscribe({
|
this.shelfService.updateShelf(shelf, this.shelf?.id).subscribe({
|
||||||
|
|||||||
@@ -6,4 +6,7 @@ export interface Shelf {
|
|||||||
icon: string;
|
icon: string;
|
||||||
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
|
iconType?: 'PRIME_NG' | 'CUSTOM_SVG';
|
||||||
sort?: SortOption;
|
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 [
|
return [
|
||||||
{
|
{
|
||||||
label: 'Options',
|
label: (isPublicShelf ? 'Public Shelf - ' : '') + (disableOptions ? 'Read only' : 'Options'),
|
||||||
items: [
|
items: [
|
||||||
{
|
{
|
||||||
label: 'Edit Shelf',
|
label: 'Edit Shelf',
|
||||||
icon: 'pi pi-pen-to-square',
|
icon: 'pi pi-pen-to-square',
|
||||||
|
disabled: disableOptions,
|
||||||
command: () => {
|
command: () => {
|
||||||
this.dialogLauncherService.openShelfEditDialog((entity?.id as number));
|
this.dialogLauncherService.openShelfEditDialog((entity?.id as number));
|
||||||
}
|
}
|
||||||
@@ -153,6 +159,7 @@ export class LibraryShelfMenuService {
|
|||||||
{
|
{
|
||||||
label: 'Delete Shelf',
|
label: 'Delete Shelf',
|
||||||
icon: 'pi pi-trash',
|
icon: 'pi pi-trash',
|
||||||
|
disabled: disableOptions,
|
||||||
command: () => {
|
command: () => {
|
||||||
this.confirmationService.confirm({
|
this.confirmationService.confirm({
|
||||||
message: `Are you sure you want to delete shelf: ${entity?.name}?`,
|
message: `Are you sure you want to delete shelf: ${entity?.name}?`,
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
import {inject, Injectable} from '@angular/core';
|
import {inject, Injectable} from '@angular/core';
|
||||||
import {HttpClient} from '@angular/common/http';
|
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 {tap, catchError, map, shareReplay, finalize} from 'rxjs/operators';
|
||||||
|
|
||||||
import {Shelf} from '../model/shelf.model';
|
import {Shelf} from '../model/shelf.model';
|
||||||
import {ShelfState} from '../model/state/shelf-state.model';
|
import {ShelfState} from '../model/state/shelf-state.model';
|
||||||
import {BookService} from './book.service';
|
import {BookService} from './book.service';
|
||||||
import {API_CONFIG} from '../../../core/config/api-config';
|
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'})
|
@Injectable({providedIn: 'root'})
|
||||||
export class ShelfService {
|
export class ShelfService {
|
||||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelves`;
|
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/shelves`;
|
||||||
private http = inject(HttpClient);
|
private http = inject(HttpClient);
|
||||||
private bookService = inject(BookService);
|
private bookService = inject(BookService);
|
||||||
|
private userService = inject(UserService);
|
||||||
|
|
||||||
private shelfStateSubject = new BehaviorSubject<ShelfState>({
|
private shelfStateSubject = new BehaviorSubject<ShelfState>({
|
||||||
shelves: null,
|
shelves: null,
|
||||||
@@ -101,13 +104,30 @@ export class ShelfService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getBookCount(shelfId: number): Observable<number> {
|
getBookCount(shelfId: number): Observable<number> {
|
||||||
return this.bookService.bookState$.pipe(
|
return combineLatest([
|
||||||
map(state =>
|
this.shelfState$,
|
||||||
(state.books || []).filter(b => b.shelves?.some(s => s.id === shelfId)).length
|
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> {
|
getUnshelvedBookCount(): Observable<number> {
|
||||||
return this.bookService.bookState$.pipe(
|
return this.bookService.bookState$.pipe(
|
||||||
map(state =>
|
map(state =>
|
||||||
|
|||||||
Reference in New Issue
Block a user