From c0e20b5817fb35cae2f997d580ab4caf741e59c0 Mon Sep 17 00:00:00 2001 From: json Date: Thu, 2 Sep 2021 20:58:38 +0100 Subject: [PATCH] separate search route --- routes/index.js | 2 ++ routes/search.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 routes/search.js diff --git a/routes/index.js b/routes/index.js index 5e891a8..8763311 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,6 +2,7 @@ const homeRoute = require('./home'); const overridingRoutes = require('./overides'); const preferenceRoutes = require('./preferences'); const saveRoutes = require('./save'); +const searchRoute = require('./search'); const staticRoutes = require('./static'); const subredditRoutes = require('./subreddit'); const subscriptionRoutes = require('./subscription'); @@ -16,6 +17,7 @@ allRoutes.use(subredditRoutes); allRoutes.use(userRoutes); allRoutes.use(subscriptionRoutes); allRoutes.use(saveRoutes); +allRoutes.use(searchRoute); allRoutes.use(homeRoute); module.exports = allRoutes; diff --git a/routes/search.js b/routes/search.js new file mode 100644 index 0000000..29427b5 --- /dev/null +++ b/routes/search.js @@ -0,0 +1,48 @@ +const searchRoute = require('express').Router(); + +searchRoute.get('/search', (req, res, next) => { + let q = req.query.q; + + if (typeof q === 'undefined') { + return res.render('search', { + json: { posts: [] }, + no_query: true, + q: '', + restrict_sr: undefined, + nsfw: undefined, + subreddit: 'all', + sortby: undefined, + past: undefined, + user_preferences: req.cookies, + }); + } + + let restrict_sr = req.query.restrict_sr; + let nsfw = req.query.nsfw; + let sortby = req.query.sort; + let past = req.query.t; + let after = req.query.after; + let before = req.query.before; + if (!after) { + after = ''; + } + if (!before) { + before = ''; + } + if (restrict_sr !== 'on') { + restrict_sr = 'off'; + } + + if (nsfw !== 'on') { + nsfw = 'off'; + } + let d = `&after=${after}`; + if (before) { + d = `&before=${before}`; + } + return res.redirect( + `/r/all/search?q=${q}&restrict_sr=${restrict_sr}&nsfw=${nsfw}&sort=${sortby}&t=${past}${d}` + ); +}); + +module.exports = searchRoute;