2022-06-03 10:54:30 +02:00
|
|
|
import express from 'express'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { Searcher } from '../../lib/controllers/searcher'
|
2023-11-09 14:34:36 +01:00
|
|
|
import { formatVideoForAPI, queryVideos } from '../../lib/meilisearch/meilisearch-videos'
|
2021-03-10 09:09:04 +01:00
|
|
|
import { asyncMiddleware } from '../../middlewares/async'
|
|
|
|
import { setDefaultPagination } from '../../middlewares/pagination'
|
|
|
|
import { setDefaultSearchSort } from '../../middlewares/sort'
|
|
|
|
import { methodsValidator } from '../../middlewares/validators/method'
|
|
|
|
import { paginationValidator } from '../../middlewares/validators/pagination'
|
|
|
|
import { commonFiltersValidators, commonVideosFiltersValidator, videosSearchValidator } from '../../middlewares/validators/search'
|
|
|
|
import { videosSearchSortValidator } from '../../middlewares/validators/sort'
|
2021-06-24 15:18:54 +02:00
|
|
|
import { VideosSearchQuery } from '../../types/search-query/video-search.model'
|
2020-02-14 16:14:45 +01:00
|
|
|
|
2020-02-19 15:39:35 +01:00
|
|
|
const searchVideosRouter = express.Router()
|
2020-02-14 16:14:45 +01:00
|
|
|
|
2021-03-10 09:09:04 +01:00
|
|
|
searchVideosRouter.all('/search/videos',
|
|
|
|
methodsValidator([ 'POST', 'GET' ]),
|
2020-02-14 16:14:45 +01:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
2020-05-28 16:32:49 +02:00
|
|
|
commonFiltersValidators,
|
2020-02-14 16:14:45 +01:00
|
|
|
commonVideosFiltersValidator,
|
|
|
|
videosSearchValidator,
|
|
|
|
asyncMiddleware(searchVideos)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-02-19 15:39:35 +01:00
|
|
|
export { searchVideosRouter }
|
2020-02-14 16:14:45 +01:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function searchVideos (req: express.Request, res: express.Response) {
|
2020-05-29 16:16:55 +02:00
|
|
|
const query = Object.assign(req.query || {}, req.body || {}) as VideosSearchQuery
|
2021-06-24 15:18:54 +02:00
|
|
|
const searcher = new Searcher(queryVideos, formatVideoForAPI)
|
|
|
|
const result = await searcher.getResult(query)
|
2020-06-11 11:04:00 +02:00
|
|
|
|
2021-06-24 15:18:54 +02:00
|
|
|
return res.json(result)
|
2020-02-14 16:14:45 +01:00
|
|
|
}
|