Api/v2 #111
@@ -97,22 +97,13 @@ class RequestRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fetchAll(sort_by=undefined, sort_direction='asc', filter_param=undefined, query=undefined) {
|
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()
|
return Promise.resolve()
|
||||||
|
.then(() => utils.validSort(sort_by, sort_direction))
|
||||||
|
.then(() => utils.validFilter(filter_param))
|
||||||
.then(() => this.sortAndFilterToDbQuery(sort_by, sort_direction, filter_param, query))
|
.then(() => this.sortAndFilterToDbQuery(sort_by, sort_direction, filter_param, query))
|
||||||
.then((dbQuery) => this.database.all(dbQuery))
|
.then((dbQuery) => this.database.all(dbQuery))
|
||||||
.then((rows) => Promise.all(this.mapToTmdbByType(rows)))
|
.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;
|
module.exports = RequestRepository;
|
||||||
|
|||||||
@@ -1,22 +1,34 @@
|
|||||||
// TODO : test title and date are valid matches to columns in the database
|
// TODO : test title and date are valid matches to columns in the database
|
||||||
|
|
||||||
const validSortParams = ['title', 'date']
|
const validSortParams = ['title', 'date']
|
||||||
const validSortDirections = ['asc', 'desc']
|
const validSortDirs = ['asc', 'desc']
|
||||||
const validFilterParams = ['movie', 'show', 'seeding', 'downloading', 'paused', 'finished', 'downloaded']
|
const validFilterParams = ['movie', 'show', 'seeding', 'downloading', 'paused', 'finished', 'downloaded']
|
||||||
|
|
||||||
function validSort(by, direction) {
|
function validSort(by, direction) {
|
||||||
if (! validSortParams.includes(by)) {
|
return new Promise((resolve, reject) => {
|
||||||
return false
|
if (by === undefined) {
|
||||||
}
|
resolve()
|
||||||
else if (! validSortDirections.includes(direction)) {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
if (validSortParams.includes(by) && validSortDirs.includes(direction)) {
|
||||||
|
resolve()
|
||||||
|
} else {
|
||||||
|
reject(new Error(`invalid sort parameter, must be of: ${validSortParams} with optional sort directions: ${validSortDirs} appended with ':'`))
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function validFilter(filter_param) {
|
function validFilter(filter_param) {
|
||||||
return validFilterParams.includes(filter_param)
|
return new Promise((resolve, reject) => {
|
||||||
|
if (filter_param === undefined) {
|
||||||
|
resolve()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (filter_param && validFilterParams.includes(filter_param)) {
|
||||||
|
resolve()
|
||||||
|
} else {
|
||||||
|
reject(new Error(`filter parameteres must be of type: ${validFilterParams}`))
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { validSort, validFilter }
|
module.exports = { validSort, validFilter }
|
||||||
@@ -141,7 +141,6 @@ class TMDB {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
multiSearch(search_query, page=1) {
|
multiSearch(search_query, page=1) {
|
||||||
const query = { query: search_query, page: page };
|
const query = { query: search_query, page: page };
|
||||||
const cacheKey = `${this.cacheTags.multiSearch}:${page}:${search_query}`;
|
const cacheKey = `${this.cacheTags.multiSearch}:${page}:${search_query}`;
|
||||||
|
|||||||
@@ -1,26 +1,6 @@
|
|||||||
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 RequestRepository = require('src/request/request');
|
||||||
const cache = new Cache();
|
|
||||||
const tmdb = new TMDB(cache, configuration.get('tmdb', 'apiKey'));
|
|
||||||
const request = new RequestRepository();
|
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
|
* Controller: Request by id with type param
|
||||||
* @param {Request} req http request variable
|
* @param {Request} req http request variable
|
||||||
@@ -28,17 +8,15 @@ const requestAsTmdb = (type, id) => {
|
|||||||
* @returns {Callback}
|
* @returns {Callback}
|
||||||
*/
|
*/
|
||||||
function requestTmdbIdController(req, res) {
|
function requestTmdbIdController(req, res) {
|
||||||
const { filter, sort, query } = req.query;
|
let { filter, sort, query } = req.query;
|
||||||
let sort_by = sort_direction = undefined;
|
let sort_by = sort;
|
||||||
|
let sort_direction = undefined;
|
||||||
|
|
||||||
if (sort !== undefined && sort.includes(':')) {
|
if (sort !== undefined && sort.includes(':')) {
|
||||||
[sort_by, sort_direction] = sort.split(':')
|
[sort_by, sort_direction] = sort.split(':')
|
||||||
}
|
}
|
||||||
// log, but disregard erroros sort param
|
|
||||||
// non valid sort type, throw from request.fetchAll(sort, filter)
|
|
||||||
|
|
||||||
Promise.resolve()
|
Promise.resolve()
|
||||||
// .then(() => requestAsTmdb(type, id))
|
|
||||||
.then(() => request.fetchAll(sort_by, sort_direction, filter, query))
|
.then(() => request.fetchAll(sort_by, sort_direction, filter, query))
|
||||||
.then((result) => res.send(result))
|
.then((result) => res.send(result))
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user