Instead of Entity[] use EntityState

This commit is contained in:
aditya.chandel
2024-12-28 23:06:54 -07:00
parent da153d6c04
commit a5e321319a
17 changed files with 398 additions and 251 deletions
@@ -1,26 +1,43 @@
import {Injectable} from '@angular/core';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {catchError, map, switchMap} from 'rxjs/operators';
import {Book, BookMetadata, BookSetting} from '../model/book.model';
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { catchError, map, switchMap } from 'rxjs/operators';
import { Book, BookMetadata, BookSetting } from '../model/book.model';
import { BookState } from '../model/state/book-state.model';
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class BookService {
private readonly url = 'http://localhost:8080/v1/book';
private books = new BehaviorSubject<Book[]>([]);
books$ = this.books.asObservable();
private bookStateSubject = new BehaviorSubject<BookState>({
books: null,
loaded: false,
error: null,
});
bookState$ = this.bookStateSubject.asObservable();
constructor(private http: HttpClient) {
this.loadBooks();
}
private loadBooks(): void {
this.http.get<Book[]>(this.url).pipe(
catchError(error => {
console.error('Error loading books:', error);
return of([]);
this.bookStateSubject.next({
books: null,
loaded: true,
error: error.message,
});
return of(null);
})
).subscribe((libraries) => {
this.books.next(libraries);
).subscribe(books => {
this.bookStateSubject.next({
books: books || [],
loaded: true,
error: null,
});
});
}
@@ -31,42 +48,46 @@ export class BookService {
shelvesToUnassign: Array.from(shelvesToUnassign),
};
return this.http.post<Book[]>(`${this.url}/assign-shelves`, requestPayload).pipe(
map((updatedBooks) => {
const currentBooks = this.books.value;
map(updatedBooks => {
const currentState = this.bookStateSubject.value;
const currentBooks = currentState.books || [];
updatedBooks.forEach(updatedBook => {
const bookIndex = currentBooks.findIndex(b => b.id === updatedBook.id);
if (bookIndex !== -1) {
currentBooks[bookIndex] = updatedBook;
const index = currentBooks.findIndex(b => b.id === updatedBook.id);
if (index !== -1) {
currentBooks[index] = updatedBook;
}
});
this.books.next([...currentBooks]);
this.bookStateSubject.next({ ...currentState, books: [...currentBooks] });
return updatedBooks;
}),
catchError(error => {
console.error('Error assigning shelves to books:', error);
const currentState = this.bookStateSubject.value;
this.bookStateSubject.next({ ...currentState, error: error.message });
throw error;
})
);
}
removeBooksByLibraryId(libraryId: number): void {
const filteredBooks = this.books.value.filter(book => book.libraryId !== libraryId);
this.books.next(filteredBooks);
const currentState = this.bookStateSubject.value;
const currentBooks = currentState.books || [];
const filteredBooks = currentBooks.filter(book => book.libraryId !== libraryId);
this.bookStateSubject.next({ ...currentState, books: 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);
const currentState = this.bookStateSubject.value;
const currentBooks = currentState.books || [];
const updatedBooks = currentBooks.map(book => ({
...book,
shelves: book.shelves?.filter(shelf => shelf.id !== shelfId),
}));
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
}
getBookById(bookId: number): Observable<Book | undefined> {
return this.books$.pipe(
map(books => books.find(book => book.id === bookId))
return this.bookState$.pipe(
map(state => state.books?.find(book => book.id === bookId))
);
}
@@ -75,19 +96,17 @@ export class BookService {
}
updateViewerSetting(viewerSetting: any, bookId: number): Observable<void> {
const url = `${this.url}/${bookId}/viewer-setting`;
return this.http.put<void>(url, viewerSetting);
return this.http.put<void>(`${this.url}/${bookId}/viewer-setting`, viewerSetting);
}
updateLastReadTime(bookId: number): Observable<Book> {
return this.http.put<Book>(`${this.url}/${bookId}/update-last-read`, {}).pipe(
switchMap(updatedBook => {
console.log(updatedBook);
const currentBooks = this.books.getValue();
const updatedBooks = currentBooks.map(book =>
book.id === updatedBook.id ? {...book, lastReadTime: updatedBook.lastReadTime} : book
const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(book =>
book.id === updatedBook.id ? { ...book, lastReadTime: updatedBook.lastReadTime } : book
);
this.books.next(updatedBooks);
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
return [updatedBook];
})
);
@@ -97,9 +116,7 @@ export class BookService {
const url = `/pdf-viewer/book/${book.id}`;
window.open(url, '_blank');
this.updateLastReadTime(book.id).subscribe({
complete: () => {
},
error: (err) => console.error('Failed to update last read time', err),
error: err => console.error('Failed to update last read time', err),
});
}
@@ -107,10 +124,13 @@ export class BookService {
if (query.length < 2) {
return of([]);
}
const filteredBooks = this.books.value.filter(book =>
book.metadata?.title?.toLowerCase().includes(query.toLowerCase()) || false
return this.bookState$.pipe(
map(state =>
(state.books || []).filter(book =>
book.metadata?.title?.toLowerCase().includes(query.toLowerCase())
)
)
);
return of(filteredBooks);
}
getBookDataUrl(bookId: number): string {
@@ -130,45 +150,39 @@ export class BookService {
}
setBookMetadata(googleBookId: string, bookId: number): Observable<Book> {
const requestBody = {googleBookId: googleBookId};
const requestBody = { googleBookId };
return this.http.put<Book>(`${this.url}/${bookId}/set-metadata`, requestBody).pipe(
map(book => {
const updatedBooks = this.books.getValue().map(existingBook =>
const currentState = this.bookStateSubject.value;
const updatedBooks = (currentState.books || []).map(existingBook =>
existingBook.id === book.id ? book : existingBook
);
this.books.next(updatedBooks);
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
return book;
}),
catchError(error => {
console.error('Error assigning shelves to books:', error);
const currentState = this.bookStateSubject.value;
this.bookStateSubject.next({ ...currentState, error: error.message });
throw error;
})
);
}
handleNewlyCreatedBook(book: Book): void {
const currentBooks = this.books.getValue();
const bookIndex = currentBooks.findIndex(existingBook => existingBook.id === book.id);
const updatedBooks = [...currentBooks];
const currentState = this.bookStateSubject.value;
const updatedBooks = currentState.books ? [...currentState.books] : [];
const bookIndex = updatedBooks.findIndex(existingBook => existingBook.id === book.id);
if (bookIndex > -1) {
updatedBooks[bookIndex] = book;
} else {
updatedBooks.push(book);
}
this.books.next(updatedBooks);
this.bookStateSubject.next({ ...currentState, books: updatedBooks });
}
handleRemovedBookIds(removedBookIds: Set<number>) {
console.log(removedBookIds);
if (!(removedBookIds instanceof Set)) {
console.error("removedBookIds should be a Set");
return; // Or throw an error if preferred
}
const currentBooks = this.books.getValue();
let filteredBooks = currentBooks.filter(currentBook => !removedBookIds.has(currentBook.id));
this.books.next(filteredBooks);
handleRemovedBookIds(removedBookIds: Set<number>): void {
const currentState = this.bookStateSubject.value;
const filteredBooks = (currentState.books || []).filter(book => !removedBookIds.has(book.id));
this.bookStateSubject.next({ ...currentState, books: filteredBooks });
}
}
@@ -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)
);
}
}
@@ -1,38 +1,58 @@
import {Injectable} from '@angular/core';
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";
import { Injectable } from '@angular/core';
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';
import { ShelfState } from '../model/state/shelf-state.model';
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class ShelfService {
private readonly url = 'http://localhost:8080/v1/shelf';
private shelves = new BehaviorSubject<Shelf[]>([]);
shelves$ = this.shelves.asObservable();
private shelfStateSubject = new BehaviorSubject<ShelfState>({
shelves: null,
loaded: false,
error: null,
});
shelfState$ = this.shelfStateSubject.asObservable();
constructor(private http: HttpClient, private bookService: BookService) {
this.loadShelves();
}
private loadShelves(): void {
this.http.get<Shelf[]>(this.url).pipe(
catchError(error => {
this.shelfStateSubject.next({
shelves: null,
loaded: true,
error: error.message,
});
return of([]);
})
).subscribe((shelves) => {
this.shelves.next(shelves);
).subscribe(shelves => {
this.shelfStateSubject.next({
shelves,
loaded: true,
error: null,
});
});
}
createShelf(shelf: Shelf): Observable<Shelf> {
return this.http.post<Shelf>(this.url, shelf).pipe(
map(newShelf => {
this.shelves.next([...this.shelves.value, newShelf])
const currentState = this.shelfStateSubject.value;
const updatedShelves = currentState.shelves ? [...currentState.shelves, newShelf] : [newShelf];
this.shelfStateSubject.next({ ...currentState, shelves: updatedShelves });
return newShelf;
}),
catchError(error => {
console.error('Error creating shelf:', error);
const currentState = this.shelfStateSubject.value;
this.shelfStateSubject.next({ ...currentState, error: error.message });
throw error;
})
);
@@ -41,35 +61,42 @@ export class ShelfService {
updateSort(shelfId: number, sort: SortOption): Observable<Shelf> {
return this.http.put<Shelf>(`${this.url}/${shelfId}/sort`, sort).pipe(
map(updatedShelf => {
const updatedShelves = this.shelves.value.map(shelf =>
const currentState = this.shelfStateSubject.value;
const updatedShelves = currentState.shelves?.map(shelf =>
shelf.id === shelfId ? updatedShelf : shelf
);
this.shelves.next(updatedShelves);
) || [];
this.shelfStateSubject.next({ ...currentState, shelves: updatedShelves });
return updatedShelf;
}),
catchError(error => {
console.error('Error updating shelf sort:', error);
const currentState = this.shelfStateSubject.value;
this.shelfStateSubject.next({ ...currentState, error: error.message });
throw error;
})
);
}
deleteShelf(shelfId: number) {
deleteShelf(shelfId: number): Observable<void> {
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);
this.bookService.removeBooksFromShelf(shelfId);
const currentState = this.shelfStateSubject.value;
const updatedShelves = currentState.shelves?.filter(shelf => shelf.id !== shelfId) || [];
this.shelfStateSubject.next({ ...currentState, shelves: updatedShelves });
}),
catchError(error => {
const currentState = this.shelfStateSubject.value;
this.shelfStateSubject.next({ ...currentState, error: error.message });
return of();
})
);
}
getBookCount(shelfId: number): Observable<number> {
return this.bookService.books$.pipe(
map(books => books.filter(book => book.shelves?.some(shelf => shelf.id === shelfId)).length)
return this.bookService.bookState$.pipe(
map(state =>
(state.books || []).filter(book => book.shelves?.some(shelf => shelf.id === shelfId)).length
)
);
}
}