aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/sort.ts
blob: 77a532276d26de286b44ccb7c8700d82a4270504 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import express from 'express'

const setDefaultSort = setDefaultSortFactory('-createdAt')
const setDefaultVideosSort = setDefaultSortFactory('-publishedAt')

const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name')

const setDefaultSearchSort = setDefaultSortFactory('-match')
const setBlacklistSort = setDefaultSortFactory('-createdAt')

// ---------------------------------------------------------------------------

export {
  setDefaultSort,
  setDefaultSearchSort,
  setDefaultVideosSort,
  setDefaultVideoRedundanciesSort,
  setBlacklistSort
}

// ---------------------------------------------------------------------------

function setDefaultSortFactory (sort: string) {
  return (req: express.Request, res: express.Response, next: express.NextFunction) => {
    if (!req.query.sort) req.query.sort = sort

    return next()
  }
}