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',
})
export class LibraryService implements OnInit {
private libraryEntriesSubject = new ReplaySubject<Entry[]>(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<Entry[]> {
if (this.entriesLoaded && !forceFetch) {
return this.libraryEntriesSubject.asObservable();
getAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
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<Entry[]> {
// if (this.entriesLoaded && !forceFetch) {
// return this.libraryEntriesSubject;
// } else {
// this.fetchAllEntries(forceFetch);
// return this.libraryEntriesSubject;
// }
// }
refreshEntries(): Observable<Entry[]> {
return this.fetchAllEntries();
}
fetchData(
@@ -49,74 +58,147 @@ export class LibraryService implements OnInit {
return this.http.get<{ results: any[] }>(endPoint, { params: httpParams });
}
private fetchAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
const endPoint = `${this.backend_apiUrl}/${this.apiAction}`;
// private fetchAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
// 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<Entry[]> {
return this.http
.get<Entry[]>(`${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<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> {
return this.getAllEntries().pipe(
switchMap((entries) => {
const existingEntry = entries.find((entry) => {
if (entry.entryId?.tmdb_id) entry.entryId.tmdb_id == tmdb_id;
});
const existingEntry = this.entries.find(
(entry) => entry.entryId?.tmdb_id === tmdb_id
);
if (existingEntry) {
return of(existingEntry);
} else {
return this.http.post<Entry>(
`${this.backend_apiUrl}/${this.apiAction}`,
{
return this.http
.post<Entry>(`${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
);
}
}
// 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> {
// Directly attempt to delete the entry without fetching all entries first.
return this.http.delete(`${this.backend_apiUrl}/${this.apiAction}/${id}`);
}
// isInLibrary(tmdb_id: string): Observable<Entry | null> {
// 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;
// });
isInLibrary(tmdb_id: string): Observable<Entry | null> {
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);
}
})
// if (existingEntry) {
// return of(existingEntry);
// } else {
// return of(null);
// }
// })
// );
// }
public isInLibrary(tmdb_id: string): Observable<Entry | null> {
const existingEntry = this.entries.find(
(entry) => entry.entryId.tmdb_id == tmdb_id
);
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 {
const entryId = {
tmdb_id: movie.entryId.tmdb_id,