This commit is contained in:
X4Lo committed 2025-01-14 17:56:02 +01:00
1 parent 289dc98042
commit 65177c7553
18 files changed
+156 -203

No files matched your search

@@ -6,6 +6,7 @@ import { EntryId } from '../../shared/models/entryId.model';
import { EntryRating } from '../../shared/models/entry-rating.model';
import { environment } from '../../../environments/environment.prod';
import { Media } from '../../shared/models/media.model';
import { LibraryService } from './library.service';
@Injectable({
providedIn: 'root',
@@ -14,7 +15,10 @@ export class EntryService {
private backend_apiUrl = environment.API_URL;
private apiAction = 'entries';
constructor(private http: HttpClient) {}
constructor(
private http: HttpClient,
private libraryService: LibraryService
) {}
// get entries
getEntries(): Observable<Entry[]> {
+95 -157
View File
@@ -1,52 +1,46 @@
import { Injectable, OnInit } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, ReplaySubject, throwError, map, of } from 'rxjs';
import { catchError, switchMap, tap } from 'rxjs/operators';
import { Observable, throwError, map, of, BehaviorSubject } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { Entry } from '../../shared/models/entry.model';
import { EntryId } from '../../shared/models/entryId.model';
import { EntryRating } from '../../shared/models/entry-rating.model';
import { environment } from '../../../environments/environment.prod';
@Injectable({
providedIn: 'root',
})
export class LibraryService implements OnInit {
private entries: Entry[] = [];
private entriesLoaded = false;
private entriesSubject: BehaviorSubject<Entry[]> = new BehaviorSubject<
Entry[]
>([]);
public entries$: Observable<Entry[]> = this.entriesSubject.asObservable();
// private oldEntries: Entry[] = [];
// private entriesLoaded = false;
private backend_apiUrl = environment.API_URL;
private apiAction = 'entries';
constructor(private http: HttpClient) {
this.fetchAllEntries().subscribe(() => {
console.log(`Library Service: Loaded ${this.entries.length} entry.`);
console.log(
`Library Service: Loaded ${
this.entriesSubject.getValue().length
} entry.`
);
});
}
ngOnInit() {}
getAllEntries(forceFetch: boolean = false): Observable<Entry[]> {
if (!forceFetch && this.entriesLoaded) {
return of(this.entries);
} else {
return this.fetchAllEntries();
newGetAllEntries(refresh: boolean = false): Observable<Entry[]> {
if (refresh) {
this.fetchAllEntries();
}
return this.entries$;
}
// 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(
private fetchData(
endPoint: string,
params?: { [param: string]: string | number }
): Observable<any> {
@@ -60,37 +54,14 @@ 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}`;
// // 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[]> {
private fetchAllEntries(): Observable<Entry[]> {
return this.http
.get<Entry[]>(`${this.backend_apiUrl}/${this.apiAction}`)
.pipe(
tap((entries) => {
this.entries = entries;
this.entries.forEach((entry: Entry) => (entry.isInLibrary = true));
this.entriesLoaded = true;
tap((result) => {
result.forEach((entry: Entry) => (entry.isInLibrary = true));
this.entriesSubject.next(result);
}),
catchError((error) => {
console.error('Failed to fetch entries:', error);
@@ -99,30 +70,17 @@ export class LibraryService implements OnInit {
);
}
// 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> {
const existingEntry = this.entries.find(
(entry) => entry.entryId?.tmdb_id === tmdb_id
);
refetchEntries(): Observable<Entry[]> {
return this.fetchAllEntries();
}
public addEntry(
tmdb_id: string,
is_tvserie: boolean
): Observable<Entry | null> {
const existingEntry = this.entriesSubject
.getValue()
.find((entry) => entry.entryId?.tmdb_id == tmdb_id);
if (existingEntry) {
return of(existingEntry);
} else {
@@ -132,105 +90,85 @@ export class LibraryService implements OnInit {
is_tvserie: is_tvserie,
})
.pipe(
tap((newEntry) => this.entries.push(newEntry)) // Add new entry to the list
tap((newEntry) => {
newEntry.isInLibrary = true;
const currentEntries = this.entriesSubject.getValue();
const updatedEntries = [...currentEntries, newEntry];
this.entriesSubject.next(updatedEntries);
}) // 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
const currentEntries = this.entriesSubject.getValue();
const updatedEntries = currentEntries.filter(
(entry) => entry.entryId.id != id
);
this.entriesSubject.next(updatedEntries);
})
);
}
// 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);
// }
// })
// );
// }
public isInLibrary(tmdb_id: string): Observable<Entry | null> {
console.log(`is in library called`);
const existingEntry = this.entries.find(
(entry) => entry.entryId.tmdb_id == tmdb_id
public setEntry(entry: Entry) {
const currentEntries = this.entriesSubject.getValue();
let updatedEntries = currentEntries.filter(
(e) => e.id != entry.id
);
return of(existingEntry || null);
updatedEntries.push(entry);
this.entriesSubject.next(updatedEntries);
}
// isInLibrary2(tmdb_id: string): Entry | null {
// const entries = this.getAllEntries2();
public isInLibrary(tmdb_id: string): Observable<Entry | null> {
// Since entries$ is an Observable, we need to use pipe and map to work with the data
return this.entries$.pipe(
map((entries: Entry[]) => {
const existingEntry = entries.find(
(entry) => entry.entryId.tmdb_id == tmdb_id
);
return existingEntry || null;
})
);
}
// if (entries.length == 0) return null;
// private mapMovieToEntry(movie: any): Entry {
// const entryId = {
// tmdb_id: movie.entryId.tmdb_id,
// } as EntryId;
// const entryRating = {
// tmdb_rating: movie.entryRating.tmdb_rating,
// } as EntryRating;
// entries
// .forEach((entry) => {})
// var is_tvseries = movie.release_date == null ? true : false;
// var release_date = movie.release_date || movie.first_air_date;
// .pipe(
// switchMap((entries) => {
// const existingEntry = entries.find((entry) => {
// // console.log(entry.entryId.tmdb_id, tmdb_id);
// return entry.entryId.tmdb_id == tmdb_id;
// });
// return {
// entryId: entryId,
// entryRating: entryRating,
// if (existingEntry) {
// return of(existingEntry);
// } else {
// return of(null);
// }
// })
// );
// title: movie.title,
// original_title: movie.original_title,
// release_date: release_date,
// storyline: movie.storyline,
// watchlist: movie.storyline,
// favorit: false,
// hidden: false,
// is_tvseries: is_tvseries,
// status: movie.status,
// poster_path: movie.poster_path,
// } as Entry;
// }
private mapMovieToEntry(movie: any): Entry {
const entryId = {
tmdb_id: movie.entryId.tmdb_id,
} as EntryId;
const entryRating = {
tmdb_rating: movie.entryRating.tmdb_rating,
} as EntryRating;
var is_tvseries = movie.release_date == null ? true : false;
var release_date = movie.release_date || movie.first_air_date;
return {
entryId: entryId,
entryRating: entryRating,
title: movie.title,
original_title: movie.original_title,
release_date: release_date,
storyline: movie.storyline,
watchlist: movie.storyline,
favorit: false,
hidden: false,
is_tvseries: is_tvseries,
status: movie.status,
poster_path: movie.poster_path,
} as Entry;
}
}
@@ -97,7 +97,7 @@ export class TmdbService {
getByKeywordMulti(keyword: string, page: number) {
var endPoint = `${this.tmdb_apiUrl}/search/multi?query=${keyword}&page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
return this.fetchdataNoCach(endPoint).pipe(
return this.fetchdata(endPoint).pipe(
switchMap((response) =>
from(response.results).pipe(
filter(
@@ -105,14 +105,6 @@ export class TmdbService {
entry.media_type === 'tv' || entry.media_type === 'movie'
),
map((entry: any) => this.adaptMovieToEntry(entry)),
switchMap((entry: any) =>
this.libraryService.isInLibrary(entry.entryId.tmdb_id).pipe(
map((inLibrary) => {
// console.log(inLibrary);
return inLibrary ? inLibrary : entry;
})
)
),
toArray(),
map((entries) => ({
entries: entries,