implemented global caching for tmdb service requests (except search)

This commit is contained in:
X4Lo committed 2024-04-05 03:05:26 +00:00
1 parent 00cbf216c4
commit c84ee2c5eb
3 files changed
+82 -57

No files matched your search

+47 -14
View File
@@ -1,6 +1,6 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, map } from 'rxjs';
import { Observable, map, of } from 'rxjs';
import { Entry } from '../../shared/models/entry.model';
import { EntryId } from '../../shared/models/entryId.model';
import { EntryRating } from '../../shared/models/entryRating.model';
@@ -9,21 +9,49 @@ import { EntryRating } from '../../shared/models/entryRating.model';
providedIn: 'root',
})
export class TmdbService {
cach: Record<string, Entry[]> = {};
constructor(private http: HttpClient) {}
// private apiUrl = environment.apiBaseUrl;
private tmdb_apiUrl = ' https://api.themoviedb.org/3';
private tmdb_apiUrl = 'https://api.themoviedb.org/3';
private tmdb_apiKey = '5323ef3dc430df54dcb9e58c37fd22bb';
fetchdata(endPoint: string): Observable<any> {
if (this.cach[endPoint]) {
return of({
exist: true,
results: this.cach[endPoint],
});
} else {
return this.http.get<{ results: any[] }>(endPoint);
}
}
fetchdataNoCach(endPoint: string): Observable<any> {
return this.http.get<{ results: any[] }>(endPoint);
}
getEntriesUsingEndPoint(endPoint: string): Observable<Entry[]> {
return this.fetchdata(endPoint).pipe(
map((response) =>
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
)
// map((response) =>
// response.results.map((movie: any) => this.adaptMovieToEntry(movie))
// )
map((response) => {
// Check if the data is from cache and has `exist` property
if (response.exist) {
// Return the cached data directly
return response.results;
} else {
// Data is not from cache, process and adapt it
const results = response.results ? response.results : response;
const fetchedData = results.map((data: any) =>
this.adaptMovieToEntry(data)
);
this.cach[endPoint] = fetchedData;
return fetchedData;
}
})
);
}
@@ -31,11 +59,7 @@ export class TmdbService {
var type = isMovie ? 'movie' : 'tv';
var endPoint = `${this.tmdb_apiUrl}/${type}/popular?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
return this.fetchdata(endPoint).pipe(
map((response) =>
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
)
);
return this.getEntriesUsingEndPoint(endPoint);
}
getTrending(
@@ -46,10 +70,19 @@ export class TmdbService {
var type = isMovie ? 'movie' : 'tv';
var endPoint = `${this.tmdb_apiUrl}/trending/${type}/${period}?page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
return this.fetchdata(endPoint).pipe(
map((response) =>
response.results.map((movie: any) => this.adaptMovieToEntry(movie))
)
return this.getEntriesUsingEndPoint(endPoint);
}
getByKeyword(keyword: string, type: string, page: number) {
var endPoint = `${this.tmdb_apiUrl}/search/${type}?query=${keyword}&page=${page}&api_key=${this.tmdb_apiKey}&language=en-US`;
// return this.getEntriesUsingEndPoint(endPoint);
return this.fetchdataNoCach(endPoint).pipe(
map((response) => {
return response.results.map((movie: any) =>
this.adaptMovieToEntry(movie)
);
})
);
}