CS307-Team24/functions/handlers/topic.js
2019-10-31 16:54:34 -04:00

61 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { admin, db } = require("../util/admin");
exports.putTopic = (req, res) => {
const newTopic = {
topic: req.body.topic
};
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" });
});
};
exports.getAllTopics = (req, res) => {
admin
.firestore()
.collection("topics")
.get()
.then(data => {
let topics = [];
data.forEach(function(doc) {
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." });
});
};
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();
}
})
.then(() => {
return res.json({ message: "Topic successfully deleted!" });
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to delete topic." });
});
};