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/entryRating.model'; import { environment } from '../../../environments/environment.prod'; @Injectable({ providedIn: 'root', }) export class EntryService { private backend_apiUrl = environment.API_URL; private apiAction = 'entries'; constructor(private http: HttpClient) {} // 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: string) { return this.http.get( `${this.backend_apiUrl}/${this.apiAction}/${entryId}` ); } // update entry updateEntry(entry: Entry) { return this.http.put( `${this.backend_apiUrl}/${this.apiAction}/${entry.id}`, { favorit: entry.favorit, watchlist: entry.watchlist, hidden: entry.hidden, } ); } // update entry refrechEntryById(entryId: number) { return this.http.get( `${this.backend_apiUrl}/${this.apiAction}/${entryId}/refrech` ); } // 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; } }