Fetchall uses promises smarter. Now the utils functions also return promises to be able to nicely chain the steps a request needs to go through. Promise all lets us wait for all items return in the map function. Without the map function would return immidiately and resolve before the map operation completed.
This commit is contained in:
@@ -1,26 +1,6 @@
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const Cache = require('src/tmdb/cache');
|
||||
const TMDB = require('src/tmdb/tmdb');
|
||||
const RequestRepository = require('src/request/request');
|
||||
const cache = new Cache();
|
||||
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
|
||||
const request = new RequestRepository();
|
||||
|
||||
const requestAsTmdb = (type, id) => {
|
||||
if (type !== undefined) {
|
||||
type = type.toLowerCase();
|
||||
|
||||
if (type === 'movie') {
|
||||
return tmdb.movieInfo(id);
|
||||
} else if (type === 'show') {
|
||||
return tmdb.showInfo(id);
|
||||
} 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
|
||||
* @param {Request} req http request variable
|
||||
@@ -28,17 +8,15 @@ const requestAsTmdb = (type, id) => {
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function requestTmdbIdController(req, res) {
|
||||
const { filter, sort, query } = req.query;
|
||||
let sort_by = sort_direction = undefined;
|
||||
let { filter, sort, query } = req.query;
|
||||
let sort_by = sort;
|
||||
let sort_direction = undefined;
|
||||
|
||||
if (sort !== undefined && sort.includes(':')) {
|
||||
[sort_by, sort_direction] = sort.split(':')
|
||||
}
|
||||
// log, but disregard erroros sort param
|
||||
// non valid sort type, throw from request.fetchAll(sort, filter)
|
||||
|
||||
Promise.resolve()
|
||||
// .then(() => requestAsTmdb(type, id))
|
||||
.then(() => request.fetchAll(sort_by, sort_direction, filter, query))
|
||||
.then((result) => res.send(result))
|
||||
.catch((error) => {
|
||||
|
||||
Reference in New Issue
Block a user