From 73bf713af1d331b14c709f570c927d33d708f768 Mon Sep 17 00:00:00 2001 From: Matt Kirby <MattJKirby@outlook.com> Date: Sun, 16 Apr 2023 18:10:35 +0100 Subject: [PATCH] Fixed routes --- .../user-service/controllers/appController.js | 14 ++++++++------ backend-services/user-service/router/route.js | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/backend-services/user-service/controllers/appController.js b/backend-services/user-service/controllers/appController.js index d453a4f7..40e1ca87 100644 --- a/backend-services/user-service/controllers/appController.js +++ b/backend-services/user-service/controllers/appController.js @@ -206,7 +206,7 @@ export async function updateUser(req,res){ } /** - * GET /userlist + * POST /userlist * This DOES NOT return emails and passwords * @param {*} req * @param {*} res @@ -235,15 +235,17 @@ export const GetUserList = async (req,res) => { */ export const Search = async (req,res) => { try { - const {query} = req.body; + const {searchQuery} = req.query; - if(query === undefined || query.length === 0){ + console.log(searchQuery) + + if(searchQuery === undefined || searchQuery.length === 0){ throw new Error("Please provide a valid query!") } - const usersWithMatchingId = await UserModel.find({"username": {"$regex": `^${query}`}}, {password: 0, email: 0}) - const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${query}`}}, {"lastName": {"$regex": `^${query}`}}]}, {password: 0, email: 0}) - return res.status(201).send({ usersById: usersWithMatchingId, usersByName: usersWithMatchingName}); + const usersWithMatchingUsername = await UserModel.find({"username": {"$regex": `^${searchQuery}`}}, {password: 0, email: 0}) + const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${searchQuery}`,"$options": 'i'}}, {"lastName": {"$regex": `^${searchQuery}`,"$options": 'i'}}]}, {password: 0, email: 0}) + return res.status(201).send({ usersByUsername: usersWithMatchingUsername, usersByName: usersWithMatchingName}); } catch(error){ return res.status(401).send({ error: error.message }); } diff --git a/backend-services/user-service/router/route.js b/backend-services/user-service/router/route.js index 06ca4709..fc224040 100644 --- a/backend-services/user-service/router/route.js +++ b/backend-services/user-service/router/route.js @@ -8,10 +8,10 @@ const router = Router(); /** POST Methods */ router.route('/register').post(controller.register); // Register router.route('/login').post(controller.verifyUser, controller.login); // Login +router.route('/userlist').post(controller.GetUserList) // Get user list /** GET Methods */ router.route('/user/:username').get(controller.getUser); // GetUser -router.route('/userlist').get(controller.GetUserList) // Get user list router.route("/search").get(controller.Search) // Search -- GitLab