Submitting requests now use requests repository

This commit is contained in:
2019-06-28 21:51:11 +02:00
parent 9f1badc1b1
commit 162d20ae52
2 changed files with 41 additions and 19 deletions

View File

@@ -11,7 +11,7 @@ class RequestRepository {
constructor(database) {
this.database = database || establishedDatabase;
this.queries = {
add: 'insert into request (id,title,year,type) values(?,?,?,?)',
add: 'insert into requests (id,title,year,poster_path,background_path,requested_by,ip,user_agent,type) values(?,?,?,?,?,?,?,?,?)',
fetchAll: 'select * from requests where status != "downloaded" order by date desc LIMIT 25 OFFSET ?*25-25',
fetchAllSort: `select id, type from request order by ? ?`,
fetchAllFilter: `select id, type from request where ? is "?"`,
@@ -82,11 +82,11 @@ class RequestRepository {
* @param {tmdb} tmdb class of movie|show to add
* @returns {Promise}
*/
addTmdb(tmdb) {
requestFromTmdb(tmdb, ip, user_agent, user) {
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]))
.then(() => this.database.run(this.queries.add, [tmdb.id, tmdb.title, tmdb.year, tmdb.poster, tmdb.backdrop, user, ip, user_agent, tmdb.type]))
.catch((error) => {
if (error.name === 'AssertionError' || error.message.endsWith('been requested')) {
throw new Error('This id is already requested', error.message);

View File

@@ -1,6 +1,19 @@
const RequestRepository = require('src/plex/requestRepository.js');
const configuration = require('src/config/configuration').getInstance()
const RequestRepository = require('src/request/request');
const Cache = require('src/tmdb/cache')
const TMDB = require('src/tmdb/tmdb')
const requestRepository = new RequestRepository();
const cache = new Cache()
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'))
const request = new RequestRepository()
const tmdbMovieInfo = (id) => {
return tmdb.movieInfo(id)
}
const tmdbShowInfo = (id) => {
return tmdb.showInfo(id)
}
/**
* Controller: POST a media id to be donwloaded
@@ -8,22 +21,31 @@ const requestRepository = new RequestRepository();
* @param {Response} res
* @returns {Callback}
*/
function submitRequestController(req, res) {
// This is the id that is the param of the url
const id = req.params.mediaId;
const type = req.query.type;
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const user_agent = req.headers['user-agent'];
const user = req.loggedInUser;
// This is the id that is the param of the url
const id = req.params.mediaId;
const type = req.query.type ? req.query.type.toLowerCase() : undefined
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const user_agent = req.headers['user-agent'];
const user = req.loggedInUser;
let mediaFunction = undefined
requestRepository.sendRequest(id, type, ip, user_agent, user)
.then(() => {
res.send({ success: true, message: 'Media item sucessfully requested!' });
})
.catch((error) => {
res.status(500).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"'})
}
if (mediaFunction === undefined) { res.status(200); return }
mediaFunction(id)
.then(tmdbMedia => request.requestFromTmdb(tmdbMedia, ip, user_agent, user))
.then(() => res.send({ success: true, message: 'Media item successfully requested' }))
.catch(err => res.status(500).send({ success: false, error: err.message }))
}
module.exports = submitRequestController;