]>
Commit | Line | Data |
---|---|---|
65fcc311 C |
1 | import { checkErrors } from './utils' |
2 | import { logger } from '../../helpers' | |
3 | import { SORTABLE_COLUMNS } from '../../initializers' | |
a877d5ac | 4 | |
9c2c18f3 | 5 | // Initialize constants here for better performances |
65fcc311 C |
6 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) |
7 | const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) | |
8 | const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) | |
5c39adb7 | 9 | |
65fcc311 | 10 | function usersSortValidator (req, res, next) { |
9c2c18f3 | 11 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) |
55fa55a9 | 12 | } |
5c39adb7 | 13 | |
65fcc311 | 14 | function videoAbusesSortValidator (req, res, next) { |
9c2c18f3 | 15 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) |
5c39adb7 C |
16 | } |
17 | ||
65fcc311 | 18 | function videosSortValidator (req, res, next) { |
9c2c18f3 | 19 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) |
55fa55a9 C |
20 | } |
21 | ||
22 | // --------------------------------------------------------------------------- | |
23 | ||
65fcc311 C |
24 | export { |
25 | usersSortValidator, | |
26 | videoAbusesSortValidator, | |
27 | videosSortValidator | |
28 | } | |
55fa55a9 C |
29 | |
30 | // --------------------------------------------------------------------------- | |
31 | ||
32 | function checkSort (req, res, next, sortableColumns) { | |
a877d5ac C |
33 | req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) |
34 | ||
35 | logger.debug('Checking sort parameters', { parameters: req.query }) | |
36 | ||
37 | checkErrors(req, res, next) | |
38 | } | |
9c2c18f3 C |
39 | |
40 | function createSortableColumns (sortableColumns) { | |
41 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) | |
42 | ||
43 | return sortableColumns.concat(sortableColumnDesc) | |
44 | } |