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