Request now happens at /request with id parameter and query for type selection. Only allows movie or show type and is static set in the controller. AddRequest adds tmdb item to database with time of request.

This commit is contained in:
2018-10-30 19:20:52 +01:00
parent 161a466ab7
commit 5a48158f07
3 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
const assert = require('assert')
const establishedDatabase = require('src/database/database');
class RequestRepository {
constructor(database) {
this.database = database || establishedDatabase;
this.queries = {
add: 'insert into request (id,title,year,type) values(?,?,?,?)',
read: 'select * from request where id is ? and type is ?'
};
}
/**
* Add tmdb movie|show to requests
* @param {tmdb} tmdb class of movie|show to add
* @returns {Promise}
*/
addTmdb(tmdb) {
return Promise.resolve()
.then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type]))
.then(row => assert.equal(row, undefined, 'Id has already been requested'))
.then(() => this.database.run(this.queries.add, [tmdb.id, tmdb.title||tmdb.name, tmdb.year, tmdb.type]))
.catch((error) => {
if (error.name === 'AssertionError' || error.message.endsWith('been requested')) {
throw new Error('This id is already requested', error.message);
}
console.log('Error @ request.addTmdb:', error);
throw new Error('Could not add request');
});
}
}
module.exports = RequestRepository;

View File

@@ -90,6 +90,8 @@ router.get('/v1/plex/hook', require('./controllers/plex/hookDump.js'));
/**
* Requests
*/
router.post('/v2/request/:id', require('./controllers/request/requestTmdbId.js'));
router.get('/v1/plex/requests/all', require('./controllers/plex/fetchRequested.js'));
router.put('/v1/plex/request/:requestId', mustBeAuthenticated, require('./controllers/plex/updateRequested.js'));

View File

@@ -0,0 +1,40 @@
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 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");
}
}
/**
* Controller: Request by id with type param
* @param {Request} req http request variable
* @param {Response} res
* @returns {Callback}
*/
function requestTmdbIdController(req, res) {
const requestId = req.params.id;
const { type } = req.query;
Promise.resolve()
.then(() => typeFunction(type))
// .then(() => checkType
.then(() => tmdb.movieInfo(requestId))
.then((movie) => request.addTmdb(movie))
.then(() => res.send({sucess: true, message: 'Request has been submitted.'}))
.catch((error) => {
res.status(404).send({ success: false, error: error.message });
});
}
module.exports = requestTmdbIdController;