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