sort.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as express from 'express'
  2. import 'express-validator'
  3. import { SortType } from '../models/utils'
  4. function setDefaultSort (req: express.Request, res: express.Response, next: express.NextFunction) {
  5. if (!req.query.sort) req.query.sort = '-createdAt'
  6. return next()
  7. }
  8. function setDefaultSearchSort (req: express.Request, res: express.Response, next: express.NextFunction) {
  9. if (!req.query.sort) req.query.sort = '-match'
  10. return next()
  11. }
  12. function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) {
  13. let newSort: SortType = { sortModel: undefined, sortValue: '' }
  14. if (!req.query.sort) req.query.sort = '-createdAt'
  15. // Set model we want to sort onto
  16. if (req.query.sort === '-createdAt' || req.query.sort === 'createdAt' ||
  17. req.query.sort === '-id' || req.query.sort === 'id') {
  18. // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter ...
  19. newSort.sortModel = undefined
  20. } else {
  21. newSort.sortModel = 'Video'
  22. }
  23. newSort.sortValue = req.query.sort
  24. req.query.sort = newSort
  25. return next()
  26. }
  27. // ---------------------------------------------------------------------------
  28. export {
  29. setDefaultSort,
  30. setDefaultSearchSort,
  31. setBlacklistSort
  32. }