1
0
Fork 0

Move sort middleware utils in utils file

This commit is contained in:
Chocobozzz 2018-03-13 11:28:37 +01:00
parent 0dd46b46e8
commit 5c328e6610
No known key found for this signature in database
GPG key ID: 583A612D890159BE
2 changed files with 25 additions and 28 deletions

View file

@ -1,8 +1,5 @@
import * as express from 'express'
import { query } from 'express-validator/check'
import { logger } from '../../helpers/logger'
import { SORTABLE_COLUMNS } from '../../initializers' import { SORTABLE_COLUMNS } from '../../initializers'
import { areValidationErrors } from './utils' import { checkSort, createSortableColumns } from './utils'
// Initialize constants here for better performances // Initialize constants here for better performances
const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
@ -41,25 +38,3 @@ export {
jobsSortValidator, jobsSortValidator,
videoCommentThreadsSortValidator videoCommentThreadsSortValidator
} }
// ---------------------------------------------------------------------------
function checkSort (sortableColumns: string[]) {
return [
query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking sort parameters', { parameters: req.query })
if (areValidationErrors(req, res)) return
return next()
}
]
}
function createSortableColumns (sortableColumns: string[]) {
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc)
}

View file

@ -1,5 +1,5 @@
import * as express from 'express' import * as express from 'express'
import { validationResult } from 'express-validator/check' import { query, validationResult } from 'express-validator/check'
import { logger } from '../../helpers/logger' import { logger } from '../../helpers/logger'
function areValidationErrors (req: express.Request, res: express.Response) { function areValidationErrors (req: express.Request, res: express.Response) {
@ -15,8 +15,30 @@ function areValidationErrors (req: express.Request, res: express.Response) {
return false return false
} }
function checkSort (sortableColumns: string[]) {
return [
query('sort').optional().isIn(sortableColumns).withMessage('Should have correct sortable column'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking sort parameters', { parameters: req.query })
if (areValidationErrors(req, res)) return
return next()
}
]
}
function createSortableColumns (sortableColumns: string[]) {
const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn)
return sortableColumns.concat(sortableColumnDesc)
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
areValidationErrors areValidationErrors,
checkSort,
createSortableColumns
} }