2020-02-14 16:14:45 +01:00
|
|
|
import * as express from 'express'
|
|
|
|
import { paginationValidator } from '../../middlewares/validators/pagination'
|
|
|
|
import { setDefaultPagination } from '../../middlewares/pagination'
|
|
|
|
import { asyncMiddleware } from '../../middlewares/async'
|
2020-02-18 15:33:21 +01:00
|
|
|
import { formatVideoForAPI, queryVideos } from '../../lib/elastic-search-videos'
|
2020-02-14 16:14:45 +01:00
|
|
|
import { videosSearchSortValidator } from '../../middlewares/validators/sort'
|
|
|
|
import { commonVideosFiltersValidator, videosSearchValidator } from '../../middlewares/validators/search'
|
|
|
|
import { setDefaultSearchSort } from '../../middlewares/sort'
|
|
|
|
|
|
|
|
const searchRouter = express.Router()
|
|
|
|
|
2020-02-18 15:33:21 +01:00
|
|
|
searchRouter.get('/search/videos',
|
2020-02-14 16:14:45 +01:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
videosSearchSortValidator,
|
|
|
|
setDefaultSearchSort,
|
|
|
|
commonVideosFiltersValidator,
|
|
|
|
videosSearchValidator,
|
|
|
|
asyncMiddleware(searchVideos)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export { searchRouter }
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function searchVideos (req: express.Request, res: express.Response) {
|
2020-02-18 15:33:21 +01:00
|
|
|
const resultList = await queryVideos(req.query)
|
2020-02-14 16:14:45 +01:00
|
|
|
|
2020-02-18 15:33:21 +01:00
|
|
|
return res.json({
|
|
|
|
total: resultList.total,
|
|
|
|
data: resultList.data.map(v => formatVideoForAPI(v, req.query.fromHost))
|
|
|
|
})
|
2020-02-14 16:14:45 +01:00
|
|
|
}
|