From 47f35aba3e87d20dfe60bc37617f061b579b98a8 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 14 Oct 2021 20:51:17 -0700 Subject: [PATCH] query groups and courses --- dbTools/profile.js | 29 +++++++++++++++++++++++++++++ routes/profile.js | 10 ++++++++++ 2 files changed, 39 insertions(+) diff --git a/dbTools/profile.js b/dbTools/profile.js index c3ea60a..64e7a6c 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -163,6 +163,35 @@ userDB = (DB) => { return r; } + DB.searchGroups = async (queryStr, coursesB = false) => { + let regEx = new RegExp(queryStr, 'i'); + let query = queryStr ? { + isGroup: true, + isCourse: coursesB, + $or: [ + {"profile.firstName": { + $regex: regEx + }}, + {"profile.lastName": { + $regex: regEx + }}, + {"profile.description": { + $regex: regEx + }}, + {"data.author": { + $regex: regEx + }} + ] + } : {isGroup: true, isCourse: coursesB}; + let r = await DB.profileCols.find(query) + .sort({ lastUpdate: -1 }).limit(20) + .toArray().catch((err) => { + console.log(err); + return false; + }); + return r; + } + let privateGroupsCache = {}; DB.isGroupPrivate = async (groupid) => { if(userProfileCache[groupid]) return userProfileCache[groupid].isPrivate; diff --git a/routes/profile.js b/routes/profile.js index 668d9fb..9f1e7cb 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -124,6 +124,16 @@ DB.getDB.then((DB)=>{ }); }); + router.get("/groups/search", async (req, res) => { + let query = req.query.query; + let coursesB = req.query.courses ? true : false; + let groups = await DB.searchGroups(query, coursesB); + return res.json({ + status: "ok", + groups + }); + }); + router.get("/groups/:id", async (req, res) => { const groupid = req.params.id; let groups = await DB.getGroup(groupid);