diff --git a/frontend/src/app/core/services/library.service.ts b/frontend/src/app/core/services/library.service.ts index b34033e..bb669ab 100644 --- a/frontend/src/app/core/services/library.service.ts +++ b/frontend/src/app/core/services/library.service.ts @@ -11,7 +11,7 @@ import { environment } from '../../../environments/environment.prod'; providedIn: 'root', }) export class LibraryService implements OnInit { - private libraryEntriesSubject = new ReplaySubject(1); + private entries: Entry[] = []; private entriesLoaded = false; private backend_apiUrl = environment.API_URL; @@ -20,19 +20,28 @@ export class LibraryService implements OnInit { constructor(private http: HttpClient) {} ngOnInit() { - this.fetchAllEntries(true); + this.fetchAllEntries(); } - public getAllEntries(forceFetch: boolean = false): Observable { - if (this.entriesLoaded && !forceFetch) { - return this.libraryEntriesSubject.asObservable(); + getAllEntries(forceFetch: boolean = false): Observable { + if (!forceFetch && this.entriesLoaded) { + return of(this.entries); } else { - return this.fetchAllEntries(forceFetch); + return this.fetchAllEntries(); } } - public refrechEntries() { - return this.fetchAllEntries(true); + // public getAllEntries2(forceFetch: boolean = false): ReplaySubject { + // if (this.entriesLoaded && !forceFetch) { + // return this.libraryEntriesSubject; + // } else { + // this.fetchAllEntries(forceFetch); + // return this.libraryEntriesSubject; + // } + // } + + refreshEntries(): Observable { + return this.fetchAllEntries(); } fetchData( @@ -49,73 +58,146 @@ export class LibraryService implements OnInit { return this.http.get<{ results: any[] }>(endPoint, { params: httpParams }); } - private fetchAllEntries(forceFetch: boolean = false): Observable { - const endPoint = `${this.backend_apiUrl}/${this.apiAction}`; + // private fetchAllEntries(forceFetch: boolean = false): Observable { + // const endPoint = `${this.backend_apiUrl}/${this.apiAction}`; - // If data is already loaded and not force fetching, immediately return the cached observable. - if (this.entriesLoaded && !forceFetch) { - return this.libraryEntriesSubject.asObservable(); - } else { - // Data needs to be (re)fetched. - return this.fetchData(endPoint).pipe( + // // If data is already loaded and not force fetching, immediately return the cached observable. + // if (this.entriesLoaded && !forceFetch) { + // return this.libraryEntriesSubject.asObservable(); + // } else { + // // Data needs to be (re)fetched. + // return this.fetchData(endPoint).pipe( + // tap((entries) => { + // // Cache the latest entries and mark as loaded. + // entries.forEach((entry: Entry) => (entry.isInLibrary = true)); + // this.libraryEntriesSubject.next(entries); + // this.entriesLoaded = true; + // }), + // catchError((error) => { + // console.error('Failed to fetch entries:', error); + // this.entriesLoaded = false; // Allow for a retry on subsequent calls. + // return throwError(() => new Error('Failed to fetch entries')); + // }) + // ); + // } + // } + fetchAllEntries(): Observable { + return this.http + .get(`${this.backend_apiUrl}/${this.apiAction}`) + .pipe( tap((entries) => { - // Cache the latest entries and mark as loaded. - entries.forEach((entry: Entry) => (entry.isInLibrary = true)); - this.libraryEntriesSubject.next(entries); + this.entries = entries; + this.entries.forEach((entry: Entry) => (entry.isInLibrary = true)); this.entriesLoaded = true; }), catchError((error) => { console.error('Failed to fetch entries:', error); - this.entriesLoaded = false; // Allow for a retry on subsequent calls. return throwError(() => new Error('Failed to fetch entries')); }) ); + } + + // addEntry(tmdb_id: string, is_tvserie: boolean): Observable { + // return this.getAllEntries().pipe( + // switchMap((entries) => { + // const existingEntry = entries.find((entry) => { + // if (entry.entryId?.tmdb_id) entry.entryId.tmdb_id == tmdb_id; + // }); + // if (existingEntry) { + // return of(existingEntry); + // } else { + // return this.http.post( + // `${this.backend_apiUrl}/${this.apiAction}`, + // { + // tmdb_id: tmdb_id, + // is_tvserie: is_tvserie, + // } + // ); + // } + // }) + // ); + // } + addEntry(tmdb_id: string, is_tvserie: boolean): Observable { + const existingEntry = this.entries.find( + (entry) => entry.entryId?.tmdb_id === tmdb_id + ); + if (existingEntry) { + return of(existingEntry); + } else { + return this.http + .post(`${this.backend_apiUrl}/${this.apiAction}`, { + tmdb_id: tmdb_id, + is_tvserie: is_tvserie, + }) + .pipe( + tap((newEntry) => this.entries.push(newEntry)) // Add new entry to the list + ); } } - addEntry(tmdb_id: string, is_tvserie: boolean): Observable { - return this.getAllEntries().pipe( - switchMap((entries) => { - const existingEntry = entries.find((entry) => { - if (entry.entryId?.tmdb_id) entry.entryId.tmdb_id == tmdb_id; - }); - if (existingEntry) { - return of(existingEntry); - } else { - return this.http.post( - `${this.backend_apiUrl}/${this.apiAction}`, - { - tmdb_id: tmdb_id, - is_tvserie: is_tvserie, - } + // deleteEntry(id: number): Observable { + // // Directly attempt to delete the entry without fetching all entries first. + // return this.http.delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`); + // } + public deleteEntry(id: number): Observable { + return this.http + .delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`) + .pipe( + tap(() => { + this.entries = this.entries.filter( + (entry) => entry.entryId.id !== id ); - } - }) + }) + ); + } + + // isInLibrary(tmdb_id: string): Observable { + // return this.getAllEntries().pipe( + // switchMap((entries) => { + // if (entries.length == 0) return of(null); + // const existingEntry = entries.find((entry) => { + // // console.log(entry.entryId.tmdb_id, tmdb_id); + // return entry.entryId.tmdb_id == tmdb_id; + // }); + + // if (existingEntry) { + // return of(existingEntry); + // } else { + // return of(null); + // } + // }) + // ); + // } + public isInLibrary(tmdb_id: string): Observable { + const existingEntry = this.entries.find( + (entry) => entry.entryId.tmdb_id == tmdb_id ); + return of(existingEntry || null); } - deleteEntry(id: number): Observable { - // Directly attempt to delete the entry without fetching all entries first. - return this.http.delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`); - } + // isInLibrary2(tmdb_id: string): Entry | null { + // const entries = this.getAllEntries2(); - isInLibrary(tmdb_id: string): Observable { - return this.getAllEntries().pipe( - switchMap((entries) => { - if (entries.length == 0) return of(null); - const existingEntry = entries.find((entry) => { - // console.log(entry.entryId.tmdb_id, tmdb_id); - return entry.entryId.tmdb_id == tmdb_id; - }); + // if (entries.length == 0) return null; - if (existingEntry) { - return of(existingEntry); - } else { - return of(null); - } - }) - ); - } + // entries + // .forEach((entry) => {}) + + // .pipe( + // switchMap((entries) => { + // const existingEntry = entries.find((entry) => { + // // console.log(entry.entryId.tmdb_id, tmdb_id); + // return entry.entryId.tmdb_id == tmdb_id; + // }); + + // if (existingEntry) { + // return of(existingEntry); + // } else { + // return of(null); + // } + // }) + // ); + // } private mapMovieToEntry(movie: any): Entry { const entryId = {