delete topic fully works

This commit is contained in:
Leon Liang
2019-10-31 14:32:54 -04:00
parent b20408c144
commit 50bc73870b
2 changed files with 49 additions and 43 deletions

View File

@@ -1,9 +1,8 @@
/* eslint-disable promise/always-return */
/* eslint-disable promise/always-return */
const { admin, db } = require("../util/admin");
exports.putTopic = (req, res) => {
const newTopic = {
topic: req.body.topic,
topicId: null
topic: req.body.topic
};
admin
@@ -12,12 +11,11 @@ exports.putTopic = (req, res) => {
.add(newTopic)
.then(doc => {
const resTopic = newTopic;
newTopic.topicId = doc.id;
return res.status(200).json(resTopic);
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "something is wrong" });
return res.status(500).json({ error: "something is wrong" });
});
};
@@ -29,13 +27,16 @@ exports.getAllTopics = (req, res) => {
.then(data => {
let topics = [];
data.forEach(function(doc) {
topics.push(doc.data());
topics.push({
topic: doc.data().topic,
id: doc.id
});
});
return res.status(200).json(topics);
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to fetch all topics." });
return res.status(500).json({ error: "Failed to fetch all topics." });
});
};
@@ -45,16 +46,16 @@ exports.deleteTopic = (req, res) => {
.get()
.then(doc => {
if (!doc.exists) {
return res.status(404).json({ error: "Topic not found" });
return res.status(404).json({ error: "Topic not found" });
} else {
return topic.delete();
}
})
.then(() => {
res.json({ message: "Topic successfully deleted!" });
res.json({ message: "Topic successfully deleted!" });
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to delete topic." });
return res.status(500).json({ error: "Failed to delete topic." });
});
};