Rewrote posting request controller to handle body parameters and use new requestsRepository function istead of plexRepository functions

This commit is contained in:
2019-06-28 22:31:52 +02:00
parent 270a259cee
commit 91dcfaccb9

View File

@@ -6,21 +6,14 @@ const cache = new Cache();
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey')); const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
const request = new RequestRepository(); const request = new RequestRepository();
const requestAsTmdb = (type, id) => { const tmdbMovieInfo = (id) => {
if (type !== undefined) { return tmdb.movieInfo(id)
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")
} }
const tmdbShowInfo = (id) => {
return tmdb.showInfo(id)
}
/** /**
* Controller: Request by id with type param * Controller: Request by id with type param
* @param {Request} req http request variable * @param {Request} req http request variable
@@ -28,15 +21,32 @@ const requestAsTmdb = (type, id) => {
* @returns {Callback} * @returns {Callback}
*/ */
function requestTmdbIdController(req, res) { function requestTmdbIdController(req, res) {
const { id, type } = req.body; const { id, type } = req.body
console.log('body', req.body)
console.log('id & type', id, type)
Promise.resolve() const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
.then(() => requestAsTmdb(type, id)) const user_agent = req.headers['user-agent'];
.then((requesAsTmdb) => request.addTmdb(requesAsTmdb)) const user = req.loggedInUser;
.then(() => res.send({sucess: true, message: 'Request has been submitted.'})) let mediaFunction = undefined
.catch((error) => {
res.status(404).send({ success: false, error: error.message }); if (type === 'movie') {
}); console.log('movie')
mediaFunction = tmdbMovieInfo
} else if (type === 'show') {
console.log('show')
mediaFunction = tmdbShowInfo
} else {
res.status(422).send({ success: false, error: 'Incorrect type. Allowed types: "movie" or "show"'})
}
mediaFunction(id)
.catch((error) => { console.error(error); res.status(404).send({ success: false, error: 'Id not found' }) })
.then((tmdbMedia) => request.requestFromTmdb(tmdbMedia, ip, user_agent, user))
.then(() => res.send({success: true, message: 'Request has been submitted.'}))
.catch((error) => {
res.status(501).send({ success: false, error: error.message });
})
} }
module.exports = requestTmdbIdController; module.exports = requestTmdbIdController;