feat: add search functionality

this commit adds long awaited search feature

fix https://github.com/zyachel/quetre/issues/21
This commit is contained in:
zyachel 2022-10-31 01:05:02 +05:30
parent 86df58367a
commit 1cdafe0380
16 changed files with 546 additions and 27 deletions

View file

@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////
@ -6,6 +7,7 @@ import catchAsyncErrors from '../utils/catchAsyncErrors.js';
import getAnswers from '../fetchers/getAnswers.js';
import getTopic from '../fetchers/getTopic.js';
import getProfile from '../fetchers/getProfile.js';
import getSearch from '../fetchers/getSearch.js';
////////////////////////////////////////////////////////
// EXPORTS
@ -33,6 +35,15 @@ export const profile = catchAsyncErrors(async (req, res, next) => {
res.status(200).json({ status: 'success', data });
});
export const search = catchAsyncErrors(async (req, res, next) => {
const searchText = req.urlObj.searchParams.get('q')?.trim(); // no search to perform if there isn't any query
let searchData = null;
if (searchText) searchData = await getSearch(req.urlObj.search);
res.status(200).json({ status: 'success', data: searchData });
});
export const unimplemented = (req, res, next) => {
res.status(501).json({
status: 'fail',