Submitting requests now use requests repository
This commit is contained in:
		| @@ -11,7 +11,7 @@ class RequestRepository { | |||||||
|   constructor(database) { |   constructor(database) { | ||||||
|     this.database = database || establishedDatabase; |     this.database = database || establishedDatabase; | ||||||
|     this.queries = { |     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', |       fetchAll: 'select * from requests where status != "downloaded" order by date desc LIMIT 25 OFFSET ?*25-25', | ||||||
|       fetchAllSort: `select id, type from request order by ? ?`, |       fetchAllSort: `select id, type from request order by ? ?`, | ||||||
|       fetchAllFilter: `select id, type from request where ? is "?"`, |       fetchAllFilter: `select id, type from request where ? is "?"`, | ||||||
| @@ -82,11 +82,11 @@ class RequestRepository { | |||||||
|    * @param {tmdb} tmdb class of movie|show to add |    * @param {tmdb} tmdb class of movie|show to add | ||||||
|    * @returns {Promise} |    * @returns {Promise} | ||||||
|    */ |    */ | ||||||
|   addTmdb(tmdb) { |   requestFromTmdb(tmdb, ip, user_agent, user) { | ||||||
|     return Promise.resolve() |     return Promise.resolve() | ||||||
|     .then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type])) |     .then(() => this.database.get(this.queries.read, [tmdb.id, tmdb.type])) | ||||||
|     .then(row => assert.equal(row, undefined, 'Id has already been requested')) |     .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) => { |     .catch((error) => { | ||||||
|       if (error.name === 'AssertionError' || error.message.endsWith('been requested')) { |       if (error.name === 'AssertionError' || error.message.endsWith('been requested')) { | ||||||
|         throw new Error('This id is already requested', error.message); |         throw new Error('This id is already requested', error.message); | ||||||
|   | |||||||
| @@ -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 |  * Controller: POST a media id to be donwloaded | ||||||
| @@ -8,22 +21,31 @@ const requestRepository = new RequestRepository(); | |||||||
|  * @param {Response} res |  * @param {Response} res | ||||||
|  * @returns {Callback} |  * @returns {Callback} | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| function submitRequestController(req, res) { | function submitRequestController(req, res) { | ||||||
|    // This is the id that is the param of the url |   // This is the id that is the param of the url | ||||||
|    const id = req.params.mediaId; |   const id = req.params.mediaId; | ||||||
|    const type = req.query.type; |   const type = req.query.type ? req.query.type.toLowerCase() : undefined | ||||||
|    const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; |   const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; | ||||||
|    const user_agent = req.headers['user-agent']; |   const user_agent = req.headers['user-agent']; | ||||||
|    const user = req.loggedInUser; |   const user = req.loggedInUser; | ||||||
|  |   let mediaFunction = undefined | ||||||
|  |  | ||||||
|    requestRepository.sendRequest(id, type, ip, user_agent, user) |   if (type === 'movie') { | ||||||
|       .then(() => { |     console.log('movie') | ||||||
|          res.send({ success: true, message: 'Media item sucessfully requested!' }); |     mediaFunction = tmdbMovieInfo | ||||||
|       }) |   } else if (type === 'show') { | ||||||
|       .catch((error) => { |     console.log('show') | ||||||
|          res.status(500).send({ success: false, error: error.message }); |     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; | module.exports = submitRequestController; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user