Sepearate controller and tmdb function for fetching personCredits

This commit is contained in:
2022-03-06 11:53:45 +01:00
parent 4d853565d1
commit f89486ae9e
2 changed files with 71 additions and 28 deletions

View File

@@ -31,34 +31,38 @@ class TMDB {
this.cache = cache || redisCache; this.cache = cache || redisCache;
this.cacheTags = { this.cacheTags = {
multiSearch: 'mus', multiSearch: "mus",
movieSearch: 'mos', movieSearch: "mos",
showSearch: 'ss', showSearch: "ss",
personSearch: 'ps', personSearch: "ps",
movieInfo: 'mi', movieInfo: "mi",
movieCredits: 'mc', movieCredits: "mc",
movieReleaseDates: 'mrd', movieReleaseDates: "mrd",
showInfo: 'si', movieImages: "mimg",
showCredits: 'sc', showInfo: "si",
personInfo: 'pi', showCredits: "sc",
miscNowPlayingMovies: 'npm', personInfo: "pi",
miscPopularMovies: 'pm', personCredits: "pc",
miscTopRatedMovies: 'tpm', miscNowPlayingMovies: "npm",
miscUpcomingMovies: 'um', miscPopularMovies: "pm",
tvOnTheAir: 'toa', miscTopRatedMovies: "tpm",
miscPopularTvs: 'pt', miscUpcomingMovies: "um",
miscTopRatedTvs: 'trt', tvOnTheAir: "toa",
miscPopularTvs: "pt",
miscTopRatedTvs: "trt"
}; };
this.defaultTTL = 86400 this.defaultTTL = 86400;
} }
getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) { getFromCacheOrFetchFromTmdb(cacheKey, tmdbMethod, query) {
return new Promise((resolve, reject) => this.cache.get(cacheKey) return new Promise((resolve, reject) =>
this.cache
.get(cacheKey)
.then(resolve) .then(resolve)
.catch(() => this.tmdb(tmdbMethod, query)) .catch(() => this.tmdb(tmdbMethod, query))
.then(resolve) .then(resolve)
.catch(error => reject(tmdbErrorResponse(error, tmdbMethod))) .catch(error => reject(tmdbErrorResponse(error, tmdbMethod)))
) );
} }
/** /**
@@ -144,11 +148,24 @@ class TMDB {
.then(person => Person.convertFromTmdbResponse(person)) .then(person => Person.convertFromTmdbResponse(person))
} }
multiSearch(search_query, page=1, adult=true) { personCredits(identifier) {
const query = { query: search_query, page: page, include_adult: adult }; const query = { id: identifier };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${adult}`; const cacheKey = `tmdb/${this.cacheTags.personCredits}:${identifier}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, 'searchMulti', query) return this.getFromCacheOrFetchFromTmdb(
cacheKey,
"personCombinedCredits",
query
)
.then(credits => this.cache.set(cacheKey, credits, this.defaultTTL))
.then(credits => Credits.convertFromTmdbResponse(credits));
}
multiSearch(search_query, page = 1, include_adult = true) {
const query = { query: search_query, page, include_adult };
const cacheKey = `tmdb/${this.cacheTags.multiSearch}:${page}:${search_query}:${include_adult}`;
return this.getFromCacheOrFetchFromTmdb(cacheKey, "searchMulti", query)
.then(response => this.cache.set(cacheKey, response, this.defaultTTL)) .then(response => this.cache.set(cacheKey, response, this.defaultTTL))
.then(response => this.mapResults(response)); .then(response => this.mapResults(response));
} }

View File

@@ -0,0 +1,26 @@
const configuration = require("src/config/configuration").getInstance();
const TMDB = require("src/tmdb/tmdb");
const tmdb = new TMDB(configuration.get("tmdb", "apiKey"));
const personCreditsController = (req, res) => {
const personId = req.params.id;
return tmdb
.personCredits(personId)
.then(credits => res.send(credits))
.catch(error => {
const { status, message } = error;
if (status && message) {
res.status(status).send({ success: false, message });
} else {
// TODO log unhandled errors
console.log("caugth show credits controller error", error);
res.status(500).send({
message: "An unexpected error occured while requesting person credits"
});
}
});
};
module.exports = personCreditsController;