Fetchall uses promises smarter. Now the utils functions also return promises to be able to nicely chain the steps a request needs to go through. Promise all lets us wait for all items return in the map function. Without the map function would return immidiately and resolve before the map operation completed.
This commit is contained in:
@@ -97,22 +97,13 @@ class RequestRepository {
|
||||
}
|
||||
|
||||
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(() => utils.validSort(sort_by, sort_direction))
|
||||
.then(() => utils.validFilter(filter_param))
|
||||
.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;
|
||||
|
||||
@@ -1,22 +1,34 @@
|
||||
// TODO : test title and date are valid matches to columns in the database
|
||||
|
||||
const validSortParams = ['title', 'date']
|
||||
const validSortDirections = ['asc', 'desc']
|
||||
const validSortDirs = ['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
|
||||
return new Promise((resolve, reject) => {
|
||||
if (by === undefined) {
|
||||
resolve()
|
||||
}
|
||||
|
||||
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) {
|
||||
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 }
|
||||
Reference in New Issue
Block a user