diff --git a/functions/handlers/topic.js b/functions/handlers/topic.js index bb5b9f1..c2d2a57 100644 --- a/functions/handlers/topic.js +++ b/functions/handlers/topic.js @@ -1,21 +1,24 @@ const { admin, db } = require("../util/admin"); exports.putTopic = (req, res) => { - const newTopic = { - topic: req.body.topic - }; + let new_following = []; + let userRef = db.doc(`/users/${req.userData.handle}`); + userRef.get().then(doc => { + new_following = doc.data().followedTopics; + new_following.push(req.body.following); - admin - .firestore() - .collection("topics") - .add(newTopic) - .then(doc => { - const resTopic = newTopic; - return res.status(200).json(resTopic); - }) - .catch(err => { - console.error(err); - return res.status(500).json({ error: "something is wrong" }); - }); + // add stuff + userRef + .set({ followedTopics: new_following }, { merge: true }) + .then(doc => { + return res + .status(201) + .json({ message: `Following ${req.body.following}` }); + }) + .catch(err => { + return res.status(500).json({ err }); + }); + return res.status(200).json({ message: "OK" }); + }); }; exports.getAllTopics = (req, res) => { @@ -40,25 +43,51 @@ exports.getAllTopics = (req, res) => { }; exports.deleteTopic = (req, res) => { - const topic = db.doc(`/topics/${req.params.topicId}`); - topic - .get() - .then(doc => { - if (!doc.exists) { - return res.status(404).json({ error: "Topic not found" }); - } else { - return topic.delete(); + let new_following = []; + let userRef = db.doc(`/users/${req.userData.handle}`); + userRef.get().then(doc => { + new_following = doc.data().followedTopics; + // remove username from array + new_following.forEach(function(follower, index) { + if (follower === `${req.body.topic}`) { + new_following.splice(index, 1); } - }) - .then(() => { - return res.json({ message: "Topic successfully deleted!" }); - }) - .catch(err => { - console.error(err); - return res.status(500).json({ error: "Failed to delete topic." }); }); + + // update database + userRef + .set({ followedTopics: new_following }, { merge: true }) + .then(doc => { + return res + .status(202) + .json({ message: `Successfully unfollow ${req.body.unfollow}` }); + }) + .catch(err => { + return res.status(500).json({ err }); + }); + return res.status(200).json({ message: "ok" }); + }); }; +// const topic = db.doc(`/topics/${req.params.topicId}`); +// topic +// .get() +// .then(doc => { +// if (!doc.exists) { +// return res.status(404).json({ error: "Topic not found" }); +// } else { +// return topic.delete(); +// } +// }) +// .then(() => { +// return res.json({ message: "Topic successfully deleted!" }); +// }) +// .catch(err => { +// console.error(err); +// return res.status(500).json({ error: "Failed to delete topic." }); +// }); +// }; + exports.getUserTopics = (req, res) => { let data = []; db.doc(`/users/${req.body.handle}`) diff --git a/functions/index.js b/functions/index.js index df44c82..4626e47 100644 --- a/functions/index.js +++ b/functions/index.js @@ -96,7 +96,7 @@ app.post("/putTopic", fbAuth, putTopic); app.get("/getAllTopics", fbAuth, getAllTopics); // delete a specific topic -app.delete("/deleteTopic/:topicId", fbAuth, deleteTopic); +app.post("/deleteTopic", fbAuth, deleteTopic); // get topic for this user app.post("/getUserTopics", fbAuth, getUserTopics);