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