From 38682b68da28a24ee15a7669a7b65b878abb44ea Mon Sep 17 00:00:00 2001 From: Mohammed Abad Date: Tue, 16 Apr 2024 19:01:02 +0100 Subject: [PATCH] created the entry service --- .../src/app/core/services/entry.service.ts | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/frontend/src/app/core/services/entry.service.ts b/frontend/src/app/core/services/entry.service.ts index dbc6c27..6683009 100644 --- a/frontend/src/app/core/services/entry.service.ts +++ b/frontend/src/app/core/services/entry.service.ts @@ -10,4 +10,77 @@ import { EntryRating } from '../../shared/models/entryRating.model'; providedIn: 'root', }) export class EntryService { + private backend_apiUrl = 'http://localhost:4000/api/v1'; + 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 + updateEntryById(entry: Entry) { + return this.http.post(`${this.backend_apiUrl}/${this.apiAction}`, { + entry, + }); + } + + // update entry + refrechEntryById(entryId: number) { + return this.http.put( + `${this.backend_apiUrl}/${this.apiAction}/${entryId}`, + [null] + ); + } + + // 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; + } }