sort.ts 1.5 KB

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