sepia-search-motore-di-rice.../server/controllers/api/search-videos.ts

40 lines
1.4 KiB
TypeScript
Raw Normal View History

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, commonFiltersValidators } from '../../middlewares/validators/search'
2020-02-14 16:14:45 +01:00
import { setDefaultSearchSort } from '../../middlewares/sort'
import { VideosSearchQuery } from 'server/types/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
2020-02-19 15:39:35 +01:00
searchVideosRouter.get('/search/videos',
2020-02-14 16:14:45 +01:00
paginationValidator,
setDefaultPagination,
videosSearchSortValidator,
setDefaultSearchSort,
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) {
const query = req.query as VideosSearchQuery
const resultList = await queryVideos(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, query.fromHost))
2020-02-18 15:33:21 +01:00
})
2020-02-14 16:14:45 +01:00
}