import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable, map } from 'rxjs'; 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'; import { Media } from '../../shared/models/media.model'; import { LibraryService } from './library.service'; @Injectable({ providedIn: 'root', }) export class EntryService { private backend_apiUrl = environment.API_URL; private apiAction = 'entries'; constructor( private http: HttpClient, private libraryService: LibraryService ) {} // get entries getEntries(): Observable { return this.http .get(`${this.backend_apiUrl}/${this.apiAction}`) .pipe(map((response) => response.map((e: any) => this.mapToEntry(e)))); } // get entry by id getEntryById(entryId: number) { return this.http.get( `${this.backend_apiUrl}/${this.apiAction}/${entryId}` ); } // update entry updateEntry(entryId: number, entryData: any) { return this.http.put( `${this.backend_apiUrl}/${this.apiAction}/${entryId}`, { entryData: entryData, } ); } addEntryMedia(entryId: number, data: FormData): Observable { return this.http.post( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media`, data ); } selectEntryMedia(entryId: number, mediaId: number): Observable { return this.http.put( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media/${mediaId}`, {} ); } deleteEntryMedia(entryId: number, mediaId: number): Observable { return this.http.delete( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/media/${mediaId}` ); } // update entry refrechEntryById(entryId: number) { return this.http.put( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/refresh`, {} ); } syncSeries() { return this.http.get( `${this.backend_apiUrl}/${this.apiAction}/sync-series` ); } fetchEntryCollectionById(entryId: number) { return this.http.put( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/fetch-collection`, {} ); } // delete entry removeEntryById(entryId: string) { return this.http.delete( `${this.backend_apiUrl}/${this.apiAction}/${entryId}` ); } // mapper private mapToEntry(data: any): Entry { const entryId = { tmdb_id: data.entryId.tmdb_id, } as EntryId; const entryRating = { tmdb_rating: data.entryRating.tmdb_rating, } as EntryRating; var is_tvseries = data.release_date == null ? true : false; var release_date = data.release_date || data.first_air_date; return { entryId: entryId, entryRating: entryRating, title: data.title, original_title: data.original_title, release_date: release_date, storyline: data.storyline, watchlist: data.storyline, favorit: false, hidden: false, is_tvseries: is_tvseries, status: data.status, poster_path: data.poster_path, } as Entry; } }