]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
06215f15 | 2 | import { SortType } from '../models/utils' |
792dbaf0 | 3 | |
b764380a | 4 | const setDefaultSort = setDefaultSortFactory('-createdAt') |
8054669f | 5 | const setDefaultVideosSort = setDefaultSortFactory('-publishedAt') |
7a7724e6 | 6 | |
b764380a | 7 | const setDefaultVideoRedundanciesSort = setDefaultSortFactory('name') |
57c36b27 | 8 | |
b764380a | 9 | const setDefaultSearchSort = setDefaultSortFactory('-match') |
57c36b27 | 10 | |
792dbaf0 | 11 | function setBlacklistSort (req: express.Request, res: express.Response, next: express.NextFunction) { |
a1587156 | 12 | const newSort: SortType = { sortModel: undefined, sortValue: '' } |
792dbaf0 GS |
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') { | |
5bcbcbe3 | 19 | // If we want to sort onto the BlacklistedVideos relation, we won't specify it in the query parameter... |
792dbaf0 GS |
20 | newSort.sortModel = undefined |
21 | } else { | |
3fd3ab2d | 22 | newSort.sortModel = 'Video' |
792dbaf0 GS |
23 | } |
24 | ||
25 | newSort.sortValue = req.query.sort | |
26 | ||
27 | req.query.sort = newSort | |
28 | ||
29 | return next() | |
30 | } | |
31 | ||
a877d5ac C |
32 | // --------------------------------------------------------------------------- |
33 | ||
65fcc311 | 34 | export { |
1174a847 | 35 | setDefaultSort, |
57c36b27 | 36 | setDefaultSearchSort, |
8054669f | 37 | setDefaultVideosSort, |
b764380a | 38 | setDefaultVideoRedundanciesSort, |
1174a847 | 39 | setBlacklistSort |
65fcc311 | 40 | } |
b764380a C |
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 | } |