Shelf implementation complete

This commit is contained in:
aditya.chandel
2024-12-25 00:53:33 -07:00
parent f3427a4564
commit ebcd6cd908
4 changed files with 42 additions and 16 deletions

View File

@@ -221,26 +221,23 @@ public class BooksService {
List<Shelf> shelvesToUnassign = shelfRepository.findAllById(shelfIdsToUnassign);
for (Book book : books) {
for (Shelf shelf : shelvesToUnassign) {
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
shelfOpt.ifPresent(value -> book.getShelves().remove(value));
shelf.getBooks().remove(book);
shelfRepository.save(shelf);
}
for (Shelf shelf : shelvesToAssign) {
Optional<Shelf> shelfOpt = book.getShelves().stream().filter(s -> s.getId().equals(shelf.getId())).findAny();
if (shelfOpt.isEmpty()) {
book.getShelves().removeIf(shelf -> shelfIdsToUnassign.contains(shelf.getId()));
shelvesToUnassign.forEach(shelf -> shelf.getBooks().remove(book));
shelvesToAssign.forEach(shelf -> {
if (!book.getShelves().contains(shelf)) {
book.getShelves().add(shelf);
}
Optional<Book> bookOpt = shelf.getBooks().stream().filter(b -> b.getId().equals(book.getId())).findAny();
if (bookOpt.isEmpty()) {
if (!shelf.getBooks().contains(book)) {
shelf.getBooks().add(book);
}
shelfRepository.save(shelf);
}
});
bookRepository.save(book);
shelfRepository.saveAll(shelvesToAssign);
}
return books.stream().map(BookTransformer::convertToBookDTO).toList();
return books.stream().map(BookTransformer::convertToBookDTO).collect(Collectors.toList());
}
}

View File

@@ -12,9 +12,12 @@
</div>
<div *ngIf="isAnyBookSelected()">
<p-button icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
<div class="button-container">
<p-button *ngIf="(entityType$ | async) === 'Library'" icon="pi pi-ellipsis-v" label="Options" severity="primary" [rounded]="true" [outlined]="true" (click)="menu.toggle($event)"></p-button>
<p-button *ngIf="(entityType$ | async) === 'Shelf'" icon="pi pi-minus-circle" label="Unshelf" severity="primary" [rounded]="true" [outlined]="true" (click)="unshelfBooks()"></p-button>
<p-button icon="pi pi-times" label="Deselect" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button>
</div>
<p-menu #menu [model]="items" [popup]="true" appendTo="body"></p-menu>
<p-button icon="pi pi-times" label="Deselect All" severity="warning" [rounded]="true" [outlined]="true" (click)="deselectAllBooks()"></p-button>
</div>
<div class="library-header-right">

View File

@@ -55,3 +55,8 @@
display: flex;
justify-content: space-around;
}
.button-container {
display: flex;
gap: 10px; /* Adds 10px space between buttons */
}

View File

@@ -25,6 +25,7 @@ export class BooksBrowserComponent implements OnInit {
items: MenuItem[] | undefined;
selectedBooks: Set<number> = new Set();
book: Book | undefined;
entity: Library | Shelf | null = null;
constructor(
private activatedRoute: ActivatedRoute,
@@ -88,6 +89,11 @@ export class BooksBrowserComponent implements OnInit {
return of(null);
})
);
this.entity$.subscribe(entity => {
this.entity = entity; // Store resolved value in local variable
});
}
handleBookSelect(book: Book, selected: boolean): void {
@@ -142,4 +148,19 @@ export class BooksBrowserComponent implements OnInit {
},
});
}
unshelfBooks() {
if (this.entity) {
this.bookService.assignShelvesToBook(this.selectedBooks, new Set(), new Set([this.entity.id])).subscribe(
() => {
this.messageService.add({severity: 'info', summary: 'Success', detail: 'Book\'s shelves updated'});
this.selectedBooks = new Set<number>();
},
(error) => {
this.messageService.add({severity: 'error', summary: 'Error', detail: 'Failed to update book\'s shelves'});
}
);
}
}
}