sort.ts 1.2 KB

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