request returns all requested items. Optional sort, query and filter params.
This commit is contained in:
@@ -1,15 +1,82 @@
|
||||
const assert = require('assert')
|
||||
const configuration = require('src/config/configuration').getInstance();
|
||||
const Cache = require('src/tmdb/cache');
|
||||
const TMDB = require('src/tmdb/tmdb');
|
||||
const cache = new Cache();
|
||||
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
|
||||
const establishedDatabase = require('src/database/database');
|
||||
const utils = require('./utils');
|
||||
|
||||
class RequestRepository {
|
||||
constructor(database) {
|
||||
this.database = database || establishedDatabase;
|
||||
this.queries = {
|
||||
add: 'insert into request (id,title,year,type) values(?,?,?,?)',
|
||||
fetchAll: 'select id, type from request',
|
||||
fetchAllSort: `select id, type from request order by ? ?`,
|
||||
fetchAllFilter: `select id, type from request where ? is "?"`,
|
||||
fetchAllQuery: `select id, type from request where title like "%?%" or year like "%?%"`,
|
||||
fetchAllFilterAndSort: `select id, type from request where ? is "?" order by ? ?`,
|
||||
downloaded: '(select status from requests where id is request.id and type is request.type limit 1)',
|
||||
// deluge: '(select status from deluge_torrent where id is request.id and type is request.type limit 1)',
|
||||
// fetchAllFilterStatus: 'select * from request where '
|
||||
read: 'select * from request where id is ? and type is ?'
|
||||
};
|
||||
}
|
||||
|
||||
sortAndFilterToDbQuery(by, direction, filter, query) {
|
||||
let dbQuery = undefined;
|
||||
|
||||
if (query !== undefined) {
|
||||
const dbParams = [query, query];
|
||||
const dbquery = this.queries.fetchAllQuery
|
||||
|
||||
dbQuery = dbquery.split('').map((char) => char === '?' ? dbParams.shift() : char).join('')
|
||||
}
|
||||
else if (by !== undefined && filter !== undefined) {
|
||||
const paramToColumnAndValue = {
|
||||
movie: ['type', 'movie'],
|
||||
show: ['type', 'show']
|
||||
}
|
||||
const dbParams = paramToColumnAndValue[filter].concat([by, direction]);
|
||||
const query = this.queries.fetchAllFilterAndSort;
|
||||
|
||||
dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('')
|
||||
}
|
||||
else if (by !== undefined) {
|
||||
const dbParams = [by, direction];
|
||||
const query = this.queries.fetchAllSort;
|
||||
|
||||
dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('')
|
||||
}
|
||||
else if (filter !== undefined) {
|
||||
const paramToColumnAndValue = {
|
||||
movie: ['type', 'movie'],
|
||||
show: ['type', 'show'],
|
||||
downloaded: [this.queries.downloaded, 'downloaded']
|
||||
// downloading: [this.database.delugeStatus, 'downloading']
|
||||
}
|
||||
const dbParams = paramToColumnAndValue[filter]
|
||||
const query = this.queries.fetchAllFilter;
|
||||
|
||||
dbQuery = query.split('').map((char) => char === '?' ? dbParams.shift() : char).join('')
|
||||
}
|
||||
else {
|
||||
dbQuery = this.queries.fetchAll;
|
||||
}
|
||||
|
||||
return dbQuery
|
||||
}
|
||||
|
||||
mapToTmdbByType(rows) {
|
||||
return rows.map((row) => {
|
||||
if (row.type === 'movie')
|
||||
return tmdb.movieInfo(row.id)
|
||||
else if (row.type === 'show')
|
||||
return tmdb.showInfo(row.id)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Add tmdb movie|show to requests
|
||||
* @param {tmdb} tmdb class of movie|show to add
|
||||
@@ -28,6 +95,24 @@ class RequestRepository {
|
||||
throw new Error('Could not add request');
|
||||
});
|
||||
}
|
||||
|
||||
fetchAll(sort_by=undefined, sort_direction='asc', filter_param=undefined, query=undefined) {
|
||||
if (sort_by !== undefined && ! utils.validSort(sort_by, sort_direction)) {
|
||||
throw new Error('Invalid sort parameters')
|
||||
} else if (filter_param !== undefined && ! utils.validFilter(filter_param)) {
|
||||
throw new Error('Invalid filter parameter')
|
||||
}
|
||||
|
||||
return Promise.resolve()
|
||||
.then(() => this.sortAndFilterToDbQuery(sort_by, sort_direction, filter_param, query))
|
||||
.then((dbQuery) => this.database.all(dbQuery))
|
||||
.then((rows) => Promise.all(this.mapToTmdbByType(rows)))
|
||||
.catch((err) => { throw new Error(`err ${err}`) })
|
||||
}
|
||||
|
||||
// return Promise.resolve()
|
||||
// .then(() => this.database.get(this.))
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = RequestRepository;
|
||||
|
||||
22
seasoned_api/src/request/utils.js
Normal file
22
seasoned_api/src/request/utils.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// TODO : test title and date are valid matches to columns in the database
|
||||
|
||||
const validSortParams = ['title', 'date']
|
||||
const validSortDirections = ['asc', 'desc']
|
||||
const validFilterParams = ['movie', 'show', 'seeding', 'downloading', 'paused', 'finished', 'downloaded']
|
||||
|
||||
function validSort(by, direction) {
|
||||
if (! validSortParams.includes(by)) {
|
||||
return false
|
||||
}
|
||||
else if (! validSortDirections.includes(direction)) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
function validFilter(filter_param) {
|
||||
return validFilterParams.includes(filter_param)
|
||||
}
|
||||
|
||||
module.exports = { validSort, validFilter }
|
||||
@@ -101,6 +101,7 @@ router.get('/v1/plex/hook', require('./controllers/plex/hookDump.js'));
|
||||
* Requests
|
||||
*/
|
||||
|
||||
router.get('/v2/request', require('./controllers/request/fetchAllRequests.js'));
|
||||
router.post('/v2/request', 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'));
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
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 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")
|
||||
|
||||
}
|
||||
/**
|
||||
* Controller: Request by id with type param
|
||||
* @param {Request} req http request variable
|
||||
* @param {Response} res
|
||||
* @returns {Callback}
|
||||
*/
|
||||
function requestTmdbIdController(req, res) {
|
||||
const { filter, sort, query } = req.query;
|
||||
let sort_by = sort_direction = undefined;
|
||||
|
||||
if (sort !== undefined && sort.includes(':')) {
|
||||
[sort_by, sort_direction] = sort.split(':')
|
||||
}
|
||||
// log, but disregard erroros sort param
|
||||
// non valid sort type, throw from request.fetchAll(sort, filter)
|
||||
|
||||
Promise.resolve()
|
||||
// .then(() => requestAsTmdb(type, id))
|
||||
.then(() => request.fetchAll(sort_by, sort_direction, filter, query))
|
||||
.then((result) => res.send(result))
|
||||
.catch((error) => {
|
||||
res.status(404).send({ success: false, error: error.message });
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = requestTmdbIdController;
|
||||
Reference in New Issue
Block a user