import * as express from 'express' import { ChannelsSearchQuery } from 'server/types/channel-search.model' import { formatChannelForAPI, queryChannels } from '../../lib/elastic-search-channels' import { asyncMiddleware } from '../../middlewares/async' import { setDefaultPagination } from '../../middlewares/pagination' import { setDefaultSearchSort } from '../../middlewares/sort' import { paginationValidator } from '../../middlewares/validators/pagination' import { videoChannelsSearchValidator } from '../../middlewares/validators/search' import { channelsSearchSortValidator } from '../../middlewares/validators/sort' const searchChannelsRouter = express.Router() searchChannelsRouter.get('/search/video-channels', paginationValidator, setDefaultPagination, channelsSearchSortValidator, setDefaultSearchSort, videoChannelsSearchValidator, asyncMiddleware(searchChannels) ) // --------------------------------------------------------------------------- export { searchChannelsRouter } // --------------------------------------------------------------------------- async function searchChannels (req: express.Request, res: express.Response) { const query = req.query as ChannelsSearchQuery const resultList = await queryChannels(query) return res.json({ total: resultList.total, data: resultList.data.map(v => formatChannelForAPI(v, query.fromHost)) }) }