]>
Commit | Line | Data |
---|---|---|
69818c93 C |
1 | import 'express-validator' |
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) | |
5c39adb7 | 12 | |
69818c93 | 13 | function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
9c2c18f3 | 14 | checkSort(req, res, next, SORTABLE_USERS_COLUMNS) |
55fa55a9 | 15 | } |
5c39adb7 | 16 | |
69818c93 | 17 | function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
9c2c18f3 | 18 | checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) |
5c39adb7 C |
19 | } |
20 | ||
69818c93 | 21 | function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { |
9c2c18f3 | 22 | checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) |
55fa55a9 C |
23 | } |
24 | ||
25 | // --------------------------------------------------------------------------- | |
26 | ||
65fcc311 C |
27 | export { |
28 | usersSortValidator, | |
29 | videoAbusesSortValidator, | |
30 | videosSortValidator | |
31 | } | |
55fa55a9 C |
32 | |
33 | // --------------------------------------------------------------------------- | |
34 | ||
69818c93 | 35 | function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) { |
a877d5ac C |
36 | req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) |
37 | ||
38 | logger.debug('Checking sort parameters', { parameters: req.query }) | |
39 | ||
40 | checkErrors(req, res, next) | |
41 | } | |
9c2c18f3 | 42 | |
69818c93 | 43 | function createSortableColumns (sortableColumns: string[]) { |
9c2c18f3 C |
44 | const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) |
45 | ||
46 | return sortableColumns.concat(sortableColumnDesc) | |
47 | } |