Instead of Entity[] use EntityState
This commit is contained in:
@@ -1,39 +1,60 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Observable, of, tap} from 'rxjs';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {catchError, map} from 'rxjs/operators';
|
||||
import {Library} from '../model/library.model';
|
||||
import {BookService} from './book.service';
|
||||
import {BookWithNeighborsDTO} from '../model/book.model';
|
||||
import {SortOption} from '../model/sort.model';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, of, tap } from 'rxjs';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { catchError, map } from 'rxjs/operators';
|
||||
import { Library } from '../model/library.model';
|
||||
import { BookService } from './book.service';
|
||||
import { BookWithNeighborsDTO } from '../model/book.model';
|
||||
import { SortOption } from '../model/sort.model';
|
||||
import { LibraryState } from '../model/state/library-state.model';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LibraryService {
|
||||
|
||||
private readonly url = 'http://localhost:8080/v1/library';
|
||||
private libraries = new BehaviorSubject<Library[]>([]);
|
||||
libraries$ = this.libraries.asObservable();
|
||||
|
||||
private libraryStateSubject = new BehaviorSubject<LibraryState>({
|
||||
libraries: null,
|
||||
loaded: false,
|
||||
error: null,
|
||||
});
|
||||
libraryState$ = this.libraryStateSubject.asObservable();
|
||||
|
||||
constructor(private http: HttpClient, private bookService: BookService) {
|
||||
this.loadLibraries();
|
||||
}
|
||||
|
||||
private loadLibraries(): void {
|
||||
this.http.get<Library[]>(this.url).pipe(
|
||||
catchError(error => {
|
||||
this.libraryStateSubject.next({
|
||||
libraries: null,
|
||||
loaded: true,
|
||||
error: error.message,
|
||||
});
|
||||
return of([]);
|
||||
})
|
||||
).subscribe((libraries) => {
|
||||
this.libraries.next(libraries);
|
||||
).subscribe(libraries => {
|
||||
this.libraryStateSubject.next({
|
||||
libraries,
|
||||
loaded: true,
|
||||
error: null,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
createLibrary(newLibrary: Library): Observable<Library> {
|
||||
return this.http.post<Library>(this.url, newLibrary).pipe(
|
||||
map((createdLibrary) => {
|
||||
this.libraries.next([...this.libraries.value, createdLibrary]);
|
||||
map(createdLibrary => {
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
const updatedLibraries = currentState.libraries ? [...currentState.libraries, createdLibrary] : [createdLibrary];
|
||||
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
|
||||
return createdLibrary;
|
||||
}),
|
||||
catchError((error) => {
|
||||
console.error('Error creating library:', error);
|
||||
catchError(error => {
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
this.libraryStateSubject.next({ ...currentState, error: error.message });
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
@@ -43,30 +64,41 @@ export class LibraryService {
|
||||
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);
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
const updatedLibraries = currentState.libraries?.filter(library => library.id !== libraryId) || [];
|
||||
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
|
||||
}),
|
||||
catchError(error => {
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
this.libraryStateSubject.next({ ...currentState, error: error.message });
|
||||
return of();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
refreshLibrary(libraryId: number): Observable<void> {
|
||||
return this.http.put<void>(`${this.url}/${libraryId}/refresh`, {});
|
||||
return this.http.put<void>(`${this.url}/${libraryId}/refresh`, {}).pipe(
|
||||
catchError(error => {
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
this.libraryStateSubject.next({ ...currentState, error: error.message });
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
updateSort(libraryId: number, sort: SortOption): Observable<Library> {
|
||||
return this.http.put<Library>(`${this.url}/${libraryId}/sort`, sort).pipe(
|
||||
map(updatedLibrary => {
|
||||
const updatedShelves = this.libraries.value.map(library =>
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
const updatedLibraries = currentState.libraries?.map(library =>
|
||||
library.id === libraryId ? updatedLibrary : library
|
||||
);
|
||||
this.libraries.next(updatedShelves);
|
||||
) || [];
|
||||
this.libraryStateSubject.next({ ...currentState, libraries: updatedLibraries });
|
||||
return updatedLibrary;
|
||||
}),
|
||||
catchError(error => {
|
||||
console.error('Error updating library sort:', error);
|
||||
const currentState = this.libraryStateSubject.value;
|
||||
this.libraryStateSubject.next({ ...currentState, error: error.message });
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
@@ -77,9 +109,8 @@ export class LibraryService {
|
||||
}
|
||||
|
||||
getBookCount(libraryId: number): Observable<number> {
|
||||
return this.bookService.books$.pipe(
|
||||
map(books => books.filter(book => book.libraryId === libraryId).length)
|
||||
return this.bookService.bookState$.pipe(
|
||||
map(state => (state.books || []).filter(book => book.libraryId === libraryId).length)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user