Server: little sort refractoring

This commit is contained in:
Chocobozzz 2017-02-26 19:26:57 +01:00
parent e4c87ec269
commit 9c2c18f3ab
2 changed files with 17 additions and 12 deletions

View File

@ -22,9 +22,9 @@ const SEARCHABLE_COLUMNS = {
// Sortable columns per schema // Sortable columns per schema
const SORTABLE_COLUMNS = { const SORTABLE_COLUMNS = {
USERS: [ 'id', '-id', 'username', '-username', 'createdAt', '-createdAt' ], USERS: [ 'id', 'username', 'createdAt' ],
VIDEO_ABUSES: [ 'id', '-id', 'createdAt', '-createdAt' ], VIDEO_ABUSES: [ 'id', 'createdAt' ],
VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt', 'views', '-views' ] VIDEOS: [ 'name', 'duration', 'createdAt', 'views' ]
} }
const OAUTH_LIFETIME = { const OAUTH_LIFETIME = {

View File

@ -10,22 +10,21 @@ const validatorsSort = {
videosSort videosSort
} }
function usersSort (req, res, next) { // Initialize constants here for better performances
const sortableColumns = constants.SORTABLE_COLUMNS.USERS const SORTABLE_USERS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.USERS)
const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEO_ABUSES)
const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(constants.SORTABLE_COLUMNS.VIDEOS)
checkSort(req, res, next, sortableColumns) function usersSort (req, res, next) {
checkSort(req, res, next, SORTABLE_USERS_COLUMNS)
} }
function videoAbusesSort (req, res, next) { function videoAbusesSort (req, res, next) {
const sortableColumns = constants.SORTABLE_COLUMNS.VIDEO_ABUSES checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS)
checkSort(req, res, next, sortableColumns)
} }
function videosSort (req, res, next) { function videosSort (req, res, next) {
const sortableColumns = constants.SORTABLE_COLUMNS.VIDEOS checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS)
checkSort(req, res, next, sortableColumns)
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -41,3 +40,9 @@ function checkSort (req, res, next, sortableColumns) {
checkErrors(req, res, next) checkErrors(req, res, next)
} }
function createSortableColumns (sortableColumns) {
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc)
}