From 9c2c18f3ab2cf18aeb1eb38da48f0ba553f5d9be Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 26 Feb 2017 19:26:57 +0100 Subject: [PATCH] Server: little sort refractoring --- server/initializers/constants.js | 6 +++--- server/middlewares/validators/sort.js | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/server/initializers/constants.js b/server/initializers/constants.js index b99186e13..e33229163 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js @@ -22,9 +22,9 @@ const SEARCHABLE_COLUMNS = { // Sortable columns per schema const SORTABLE_COLUMNS = { - USERS: [ 'id', '-id', 'username', '-username', 'createdAt', '-createdAt' ], - VIDEO_ABUSES: [ 'id', '-id', 'createdAt', '-createdAt' ], - VIDEOS: [ 'name', '-name', 'duration', '-duration', 'createdAt', '-createdAt', 'views', '-views' ] + USERS: [ 'id', 'username', 'createdAt' ], + VIDEO_ABUSES: [ 'id', 'createdAt' ], + VIDEOS: [ 'name', 'duration', 'createdAt', 'views' ] } const OAUTH_LIFETIME = { diff --git a/server/middlewares/validators/sort.js b/server/middlewares/validators/sort.js index b7eec0316..017d266e6 100644 --- a/server/middlewares/validators/sort.js +++ b/server/middlewares/validators/sort.js @@ -10,22 +10,21 @@ const validatorsSort = { videosSort } -function usersSort (req, res, next) { - const sortableColumns = constants.SORTABLE_COLUMNS.USERS +// Initialize constants here for better performances +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) { - const sortableColumns = constants.SORTABLE_COLUMNS.VIDEO_ABUSES - - checkSort(req, res, next, sortableColumns) + checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) } function videosSort (req, res, next) { - const sortableColumns = constants.SORTABLE_COLUMNS.VIDEOS - - checkSort(req, res, next, sortableColumns) + checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) } // --------------------------------------------------------------------------- @@ -41,3 +40,9 @@ function checkSort (req, res, next, sortableColumns) { checkErrors(req, res, next) } + +function createSortableColumns (sortableColumns) { + const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) + + return sortableColumns.concat(sortableColumnDesc) +} -- 2.41.0