aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/search.ts
blob: 2ff340b597df0ef566035d83918be05f499679de (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import * as express from 'express'
import { isNSFWHidden } from '../../helpers/express-utils'
import { getFormattedObjects } from '../../helpers/utils'
import { VideoModel } from '../../models/video/video'
import {
  asyncMiddleware,
  optionalAuthenticate,
  paginationValidator,
  searchValidator,
  setDefaultPagination,
  setDefaultSearchSort,
  videosSearchSortValidator
} from '../../middlewares'

const searchRouter = express.Router()

searchRouter.get('/videos',
  paginationValidator,
  setDefaultPagination,
  videosSearchSortValidator,
  setDefaultSearchSort,
  optionalAuthenticate,
  searchValidator,
  asyncMiddleware(searchVideos)
)

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

export { searchRouter }

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

async function searchVideos (req: express.Request, res: express.Response) {
  const resultList = await VideoModel.searchAndPopulateAccountAndServer(
    req.query.search as string,
    req.query.start as number,
    req.query.count as number,
    req.query.sort as string,
    isNSFWHidden(res)
  )

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}