Implement entity deletion
This commit is contained in:
@@ -43,7 +43,8 @@ export class BooksBrowserComponent implements OnInit {
|
||||
private shelfService: ShelfService,
|
||||
private dialogService: DialogService,
|
||||
private sortService: SortService
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.sortOptions = SortOptionsHelper.generateSortOptions();
|
||||
@@ -92,9 +93,11 @@ export class BooksBrowserComponent implements OnInit {
|
||||
private fetchEntity(libraryId: number, shelfId: number): Observable<Library | Shelf | null> {
|
||||
if (libraryId) {
|
||||
this.entityType = 'Library';
|
||||
this.initializeLibraryMenuItems();
|
||||
return this.fetchLibrary(libraryId);
|
||||
} else if (shelfId) {
|
||||
this.entityType = 'Shelf';
|
||||
this.initializeShelfMenuItems();
|
||||
return this.fetchShelf(shelfId);
|
||||
}
|
||||
return of(null);
|
||||
@@ -206,4 +209,88 @@ export class BooksBrowserComponent implements OnInit {
|
||||
];
|
||||
}
|
||||
|
||||
private initializeLibraryMenuItems(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.confirmationService.confirm({
|
||||
message: 'Sure you want to delete ' + this.entity?.name + "?",
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
this.libraryService.deleteLibrary(this.entity?.id!).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'Library was deleted'
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete library',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
private initializeShelfMenuItems(): void {
|
||||
this.items = [
|
||||
{
|
||||
icon: 'pi pi-trash',
|
||||
tooltipOptions: {
|
||||
tooltipLabel: 'Delete',
|
||||
tooltipPosition: 'top'
|
||||
},
|
||||
command: () => {
|
||||
this.confirmationService.confirm({
|
||||
message: 'Sure you want to delete ' + this.entity?.name + "?",
|
||||
header: 'Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptIcon: 'none',
|
||||
rejectIcon: 'none',
|
||||
rejectButtonStyleClass: 'p-button-text',
|
||||
accept: () => {
|
||||
this.shelfService.deleteShelf(this.entity?.id!).subscribe({
|
||||
complete: () => {
|
||||
this.router.navigate(['/']);
|
||||
this.messageService.add({
|
||||
severity: 'info',
|
||||
summary: 'Success',
|
||||
detail: 'Shelf was deleted'
|
||||
});
|
||||
},
|
||||
error: () => {
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Failed',
|
||||
detail: 'Failed to delete shelf',
|
||||
life: 3000
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,6 +54,16 @@ export class BookService {
|
||||
this.books.next(filteredBooks);
|
||||
}
|
||||
|
||||
removeBooksFromShelf(shelfId: number): void {
|
||||
const books = this.books.value.filter(book => {
|
||||
if(book.shelves) {
|
||||
book.shelves = book.shelves.filter(shelf => shelf.id !== shelfId);
|
||||
}
|
||||
return book;
|
||||
})
|
||||
this.books.next(books);
|
||||
}
|
||||
|
||||
getBookById(bookId: number): Observable<Book | undefined> {
|
||||
return this.books$.pipe(
|
||||
map(books => books.find(book => book.id === bookId))
|
||||
|
||||
@@ -39,15 +39,15 @@ export class LibraryService {
|
||||
);
|
||||
}
|
||||
|
||||
deleteLibrary(libraryId: number) {
|
||||
return this.http.delete(`${this.url}/${libraryId}`).pipe(
|
||||
deleteLibrary(libraryId: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.url}/${libraryId}`).pipe(
|
||||
tap(() => {
|
||||
this.bookService.removeBooksByLibraryId(libraryId);
|
||||
const updatedLibraries = this.libraries.value.filter(library => library.id !== libraryId);
|
||||
this.libraries.next(updatedLibraries);
|
||||
}),
|
||||
catchError(error => {
|
||||
return of(null);
|
||||
return of();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||
import {BehaviorSubject, Observable, of, tap} from 'rxjs';
|
||||
import {catchError, map} from 'rxjs/operators';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Shelf} from '../model/shelf.model';
|
||||
import {SortOption} from '../model/sort.model';
|
||||
import {BookService} from "./book.service";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -14,7 +15,7 @@ export class ShelfService {
|
||||
private shelves = new BehaviorSubject<Shelf[]>([]);
|
||||
shelves$ = this.shelves.asObservable();
|
||||
|
||||
constructor(private http: HttpClient) {
|
||||
constructor(private http: HttpClient, private bookService: BookService) {
|
||||
this.http.get<Shelf[]>(this.url).pipe(
|
||||
catchError(error => {
|
||||
return of([]);
|
||||
@@ -53,4 +54,16 @@ export class ShelfService {
|
||||
);
|
||||
}
|
||||
|
||||
deleteShelf(shelfId: number) {
|
||||
return this.http.delete<void>(`${this.url}/${shelfId}`).pipe(
|
||||
tap(() => {
|
||||
this.bookService.removeBooksByLibraryId(shelfId);
|
||||
let shelves = this.shelves.value.filter(shelf => shelf.id !== shelfId);
|
||||
this.shelves.next(shelves);
|
||||
}),
|
||||
catchError(error => {
|
||||
return of();
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user