mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 02:08:47 +00:00
modified function to add/delete topic only from user
This commit is contained in:
parent
3da2449050
commit
7976110e2b
@ -1,21 +1,24 @@
|
|||||||
const { admin, db } = require("../util/admin");
|
const { admin, db } = require("../util/admin");
|
||||||
exports.putTopic = (req, res) => {
|
exports.putTopic = (req, res) => {
|
||||||
const newTopic = {
|
let new_following = [];
|
||||||
topic: req.body.topic
|
let userRef = db.doc(`/users/${req.userData.handle}`);
|
||||||
};
|
userRef.get().then(doc => {
|
||||||
|
new_following = doc.data().followedTopics;
|
||||||
|
new_following.push(req.body.following);
|
||||||
|
|
||||||
admin
|
// add stuff
|
||||||
.firestore()
|
userRef
|
||||||
.collection("topics")
|
.set({ followedTopics: new_following }, { merge: true })
|
||||||
.add(newTopic)
|
.then(doc => {
|
||||||
.then(doc => {
|
return res
|
||||||
const resTopic = newTopic;
|
.status(201)
|
||||||
return res.status(200).json(resTopic);
|
.json({ message: `Following ${req.body.following}` });
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.error(err);
|
return res.status(500).json({ err });
|
||||||
return res.status(500).json({ error: "something is wrong" });
|
});
|
||||||
});
|
return res.status(200).json({ message: "OK" });
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.getAllTopics = (req, res) => {
|
exports.getAllTopics = (req, res) => {
|
||||||
@ -40,25 +43,51 @@ exports.getAllTopics = (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.deleteTopic = (req, res) => {
|
exports.deleteTopic = (req, res) => {
|
||||||
const topic = db.doc(`/topics/${req.params.topicId}`);
|
let new_following = [];
|
||||||
topic
|
let userRef = db.doc(`/users/${req.userData.handle}`);
|
||||||
.get()
|
userRef.get().then(doc => {
|
||||||
.then(doc => {
|
new_following = doc.data().followedTopics;
|
||||||
if (!doc.exists) {
|
// remove username from array
|
||||||
return res.status(404).json({ error: "Topic not found" });
|
new_following.forEach(function(follower, index) {
|
||||||
} else {
|
if (follower === `${req.body.topic}`) {
|
||||||
return topic.delete();
|
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) => {
|
exports.getUserTopics = (req, res) => {
|
||||||
let data = [];
|
let data = [];
|
||||||
db.doc(`/users/${req.body.handle}`)
|
db.doc(`/users/${req.body.handle}`)
|
||||||
|
|||||||
@ -96,7 +96,7 @@ app.post("/putTopic", fbAuth, putTopic);
|
|||||||
app.get("/getAllTopics", fbAuth, getAllTopics);
|
app.get("/getAllTopics", fbAuth, getAllTopics);
|
||||||
|
|
||||||
// delete a specific topic
|
// delete a specific topic
|
||||||
app.delete("/deleteTopic/:topicId", fbAuth, deleteTopic);
|
app.post("/deleteTopic", fbAuth, deleteTopic);
|
||||||
|
|
||||||
// get topic for this user
|
// get topic for this user
|
||||||
app.post("/getUserTopics", fbAuth, getUserTopics);
|
app.post("/getUserTopics", fbAuth, getUserTopics);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user