reworked the library service

This commit is contained in:
X4Lo committed 2024-08-30 14:24:58 +01:00
1 parent 6a5ce2cc5e
commit 4c318fb569
1 file changed
+131 -49
+131 -49
View File
@@ -11,7 +11,7 @@ import { environment } from '../../../environments/environment.prod';
providedIn: 'root', providedIn: 'root',
}) })
export class LibraryService implements OnInit { export class LibraryService implements OnInit {
private libraryEntriesSubject = new ReplaySubject<Entry[]>(1); private entries: Entry[] = [];
private entriesLoaded = false; private entriesLoaded = false;
private backend_apiUrl = environment.API_URL; private backend_apiUrl = environment.API_URL;
@@ -20,19 +20,28 @@ export class LibraryService implements OnInit {
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {}
ngOnInit() { ngOnInit() {
this.fetchAllEntries(true); this.fetchAllEntries();
} }
public getAllEntries(forceFetch: boolean = false): Observable<Entry[]> { getAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
if (this.entriesLoaded && !forceFetch) { if (!forceFetch && this.entriesLoaded) {
return this.libraryEntriesSubject.asObservable(); return of(this.entries);
} else { } else {
return this.fetchAllEntries(forceFetch); return this.fetchAllEntries();
} }
} }
public refrechEntries() { // public getAllEntries2(forceFetch: boolean = false): ReplaySubject<Entry[]> {
return this.fetchAllEntries(true); // if (this.entriesLoaded && !forceFetch) {
// return this.libraryEntriesSubject;
// } else {
// this.fetchAllEntries(forceFetch);
// return this.libraryEntriesSubject;
// }
// }
refreshEntries(): Observable<Entry[]> {
return this.fetchAllEntries();
} }
fetchData( fetchData(
@@ -49,74 +58,147 @@ export class LibraryService implements OnInit {
return this.http.get<{ results: any[] }>(endPoint, { params: httpParams }); return this.http.get<{ results: any[] }>(endPoint, { params: httpParams });
} }
private fetchAllEntries(forceFetch: boolean = false): Observable<Entry[]> { // private fetchAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
const endPoint = `${this.backend_apiUrl}/${this.apiAction}`; // const endPoint = `${this.backend_apiUrl}/${this.apiAction}`;
// If data is already loaded and not force fetching, immediately return the cached observable. // // If data is already loaded and not force fetching, immediately return the cached observable.
if (this.entriesLoaded && !forceFetch) { // if (this.entriesLoaded && !forceFetch) {
return this.libraryEntriesSubject.asObservable(); // return this.libraryEntriesSubject.asObservable();
} else { // } else {
// Data needs to be (re)fetched. // // Data needs to be (re)fetched.
return this.fetchData(endPoint).pipe( // 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<Entry[]> {
return this.http
.get<Entry[]>(`${this.backend_apiUrl}/${this.apiAction}`)
.pipe(
tap((entries) => { tap((entries) => {
// Cache the latest entries and mark as loaded. this.entries = entries;
entries.forEach((entry: Entry) => (entry.isInLibrary = true)); this.entries.forEach((entry: Entry) => (entry.isInLibrary = true));
this.libraryEntriesSubject.next(entries);
this.entriesLoaded = true; this.entriesLoaded = true;
}), }),
catchError((error) => { catchError((error) => {
console.error('Failed to fetch entries:', 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')); return throwError(() => new Error('Failed to fetch entries'));
}) })
); );
} }
}
// addEntry(tmdb_id: string, is_tvserie: boolean): Observable<Entry | null> {
// 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<Entry>(
// `${this.backend_apiUrl}/${this.apiAction}`,
// {
// tmdb_id: tmdb_id,
// is_tvserie: is_tvserie,
// }
// );
// }
// })
// );
// }
addEntry(tmdb_id: string, is_tvserie: boolean): Observable<Entry | null> { addEntry(tmdb_id: string, is_tvserie: boolean): Observable<Entry | null> {
return this.getAllEntries().pipe( const existingEntry = this.entries.find(
switchMap((entries) => { (entry) => entry.entryId?.tmdb_id === tmdb_id
const existingEntry = entries.find((entry) => { );
if (entry.entryId?.tmdb_id) entry.entryId.tmdb_id == tmdb_id;
});
if (existingEntry) { if (existingEntry) {
return of(existingEntry); return of(existingEntry);
} else { } else {
return this.http.post<Entry>( return this.http
`${this.backend_apiUrl}/${this.apiAction}`, .post<Entry>(`${this.backend_apiUrl}/${this.apiAction}`, {
{
tmdb_id: tmdb_id, tmdb_id: tmdb_id,
is_tvserie: is_tvserie, is_tvserie: is_tvserie,
} })
.pipe(
tap((newEntry) => this.entries.push(newEntry)) // Add new entry to the list
); );
} }
}
// deleteEntry(id: number): Observable<any> {
// // 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<any> {
return this.http
.delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`)
.pipe(
tap(() => {
this.entries = this.entries.filter(
(entry) => entry.entryId.id !== id
);
}) })
); );
} }
deleteEntry(id: number): Observable<any> { // isInLibrary(tmdb_id: string): Observable<Entry | null> {
// Directly attempt to delete the entry without fetching all entries first. // return this.getAllEntries().pipe(
return this.http.delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`); // 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;
// });
isInLibrary(tmdb_id: string): Observable<Entry | null> { // if (existingEntry) {
return this.getAllEntries().pipe( // return of(existingEntry);
switchMap((entries) => { // } else {
if (entries.length == 0) return of(null); // return of(null);
const existingEntry = entries.find((entry) => { // }
// console.log(entry.entryId.tmdb_id, tmdb_id); // })
return entry.entryId.tmdb_id == tmdb_id; // );
}); // }
public isInLibrary(tmdb_id: string): Observable<Entry | null> {
if (existingEntry) { const existingEntry = this.entries.find(
return of(existingEntry); (entry) => entry.entryId.tmdb_id == tmdb_id
} else {
return of(null);
}
})
); );
return of(existingEntry || null);
} }
// isInLibrary2(tmdb_id: string): Entry | null {
// const entries = this.getAllEntries2();
// if (entries.length == 0) return 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 { private mapMovieToEntry(movie: any): Entry {
const entryId = { const entryId = {
tmdb_id: movie.entryId.tmdb_id, tmdb_id: movie.entryId.tmdb_id,