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 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")
const tmdbMovieInfo = (id) => {
return tmdb.movieInfo(id)
}
const tmdbShowInfo = (id) => {
return tmdb.showInfo(id)
}
/**
* Controller: Request by id with type param
* @param {Request} req http request variable
@@ -28,15 +21,32 @@ const requestAsTmdb = (type, id) => {
* @returns {Callback}
*/
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()
.then(() => requestAsTmdb(type, id))
.then((requesAsTmdb) => request.addTmdb(requesAsTmdb))
.then(() => res.send({sucess: true, message: 'Request has been submitted.'}))
.catch((error) => {
res.status(404).send({ success: false, error: error.message });
});
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const user_agent = req.headers['user-agent'];
const user = req.loggedInUser;
let mediaFunction = undefined
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;