From f92aa7062145411f4ba8679410f44dce55dd6f18 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Mon, 13 Sep 2021 18:40:08 -0700 Subject: [PATCH] keyvalue storage using data in the profile --- dbTools/profile.js | 18 ++++++++++++++++++ routes/profile.js | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/dbTools/profile.js b/dbTools/profile.js index 90469da..f3a36c7 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -81,6 +81,24 @@ userDB = (DB) => { }); } + DB.getData = async (profileid, key) => { + let profile = await DB.getProfile(profileid); + return profile.data[key]; + } + + DB.setData = async (profileid, key, value) => { + const _id = DB.ObjectID(profileid); + let update = { + $set:{ + ["data." + key]: value + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + DB.addNotification = async (profileid, message) => { const _id = DB.ObjectID(profileid); let update = { diff --git a/routes/profile.js b/routes/profile.js index e08fde2..b107966 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -104,6 +104,16 @@ DB.getDB.then((DB)=>{ }); }); + router.post("/setData", (req, res) => { + const key = req.body.key; + const value = req.body.value; + const profileid = getProfileId(req); + DB.setData(profileid, key, value); + return res.json({ + status: "ok", + }); + }); + router.get("/:id", async (req, res) => { let profileId = req.params.id; let profile = await DB.getProfile(profileId);