request endpoint finds type by body not query. Better error handling on what goes wrong if incorrect type or missing body parameter.

This commit is contained in:
2018-10-30 20:34:26 +01:00
parent 7e46d32e30
commit 4250b1bd17
2 changed files with 15 additions and 10 deletions

View File

@@ -88,7 +88,7 @@ class TMDB {
return Promise.resolve()
.then(() => this.cache.get(cacheKey))
.catch(() => this.tmdb('movieInfo', query))
.catch(() => { throw new Error('Could not find a movie with that id.'); })
.catch((error) => { console.log(error); throw new Error('Could not find a movie with that id.'); })
.then(response => this.cache.set(cacheKey, response))
.then((response) => {
try {

View File

@@ -7,14 +7,19 @@ const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
const request = new RequestRepository();
const typeFunction = (type) => {
type = type.toLowerCase();
if (type === 'movie') {
return tmdb.movieInfo;
} else if (type === 'show') {
return tmdb.showInfo;
} else {
throw new Error("Unprocessable Entity: Invalid type for query 'type'. Allowed values: movie|show");
if (type !== undefined) {
type = type.toLowerCase();
if (type === 'movie') {
return tmdb.movieInfo;
} else if (type === 'show') {
return tmdb.showInfo;
} else {
throw new Error("Unprocessable Entity: Invalid type for body parameter 'type'. Allowed values: movie|show");
}
}
throw new Error("tmdbType body parameter not defined. Allowed values: movie|show")
}
/**
* Controller: Request by id with type param
@@ -24,10 +29,10 @@ const typeFunction = (type) => {
*/
function requestTmdbIdController(req, res) {
const requestId = req.params.id;
const { type } = req.query;
const tmdbType = req.body.tmdbType;
Promise.resolve()
.then(() => typeFunction(type))
.then(() => typeFunction(tmdbType))
// .then(() => checkType
.then(() => tmdb.movieInfo(requestId))
.then((movie) => request.addTmdb(movie))