From 57c36b277e68b764dd34cb2e449f6e2ca3d1e9b6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Jul 2018 16:17:54 +0200 Subject: Begin advanced search --- server/controllers/api/index.ts | 2 ++ server/controllers/api/search.ts | 43 ++++++++++++++++++++++++++++++++++ server/controllers/api/videos/index.ts | 23 ------------------ 3 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 server/controllers/api/search.ts (limited to 'server/controllers/api') diff --git a/server/controllers/api/index.ts b/server/controllers/api/index.ts index c386a6710..e928a7478 100644 --- a/server/controllers/api/index.ts +++ b/server/controllers/api/index.ts @@ -9,6 +9,7 @@ import { videosRouter } from './videos' import { badRequest } from '../../helpers/express-utils' import { videoChannelRouter } from './video-channel' import * as cors from 'cors' +import { searchRouter } from './search' const apiRouter = express.Router() @@ -26,6 +27,7 @@ apiRouter.use('/accounts', accountsRouter) apiRouter.use('/video-channels', videoChannelRouter) apiRouter.use('/videos', videosRouter) apiRouter.use('/jobs', jobsRouter) +apiRouter.use('/search', searchRouter) apiRouter.use('/ping', pong) apiRouter.use('/*', badRequest) diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts new file mode 100644 index 000000000..2ff340b59 --- /dev/null +++ b/server/controllers/api/search.ts @@ -0,0 +1,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)) +} diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index bbb5b8b4c..547522123 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -38,7 +38,6 @@ import { videosAddValidator, videosGetValidator, videosRemoveValidator, - videosSearchValidator, videosSortValidator, videosUpdateValidator } from '../../../middlewares' @@ -50,7 +49,6 @@ import { blacklistRouter } from './blacklist' import { videoCommentRouter } from './comment' import { rateVideoRouter } from './rate' import { VideoFilter } from '../../../../shared/models/videos/video-query.type' -import { VideoSortField } from '../../../../client/src/app/shared/video/sort-field.type' import { createReqFiles, isNSFWHidden } from '../../../helpers/express-utils' import { ScheduleVideoUpdateModel } from '../../../models/video/schedule-video-update' import { videoCaptionsRouter } from './captions' @@ -94,15 +92,6 @@ videosRouter.get('/', optionalAuthenticate, asyncMiddleware(listVideos) ) -videosRouter.get('/search', - videosSearchValidator, - paginationValidator, - videosSortValidator, - setDefaultSort, - setDefaultPagination, - optionalAuthenticate, - asyncMiddleware(searchVideos) -) videosRouter.put('/:id', authenticate, reqVideoFileUpdate, @@ -432,15 +421,3 @@ async function removeVideo (req: express.Request, res: express.Response) { return res.type('json').status(204).end() } - -async function searchVideos (req: express.Request, res: express.Response, next: express.NextFunction) { - const resultList = await VideoModel.searchAndPopulateAccountAndServer( - req.query.search as string, - req.query.start as number, - req.query.count as number, - req.query.sort as VideoSortField, - isNSFWHidden(res) - ) - - return res.json(getFormattedObjects(resultList.data, resultList.total)) -} -- cgit v1.2.3