Fixed conflicts with Sprint 2 checkpoint and the most up-to-date version

This commit is contained in:
Aaron Sun
2019-11-17 10:32:59 -05:00
parent c6022dbc38
commit 6184a22607
9 changed files with 310 additions and 632 deletions

View File

@@ -1,60 +1,52 @@
/* eslint-disable promise/always-return */
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);
const newTopic = {
topic: req.body.topic
};
admin.firestore().collection('topics').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" });
.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
admin.firestore().collection('topics').get()
.then((data) => {
let topics = [];
data.forEach(function(doc) {
topics.push(doc.data());
});
});
return res.status(200).json(topics);
return res.status(200).json(topics);
})
.catch((err) => {
console.error(err);
return res.status(500).json({error: 'Failed to fetch all 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();
}
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!" });
res.json({ message: 'Topic successfully deleted!'});
})
.catch(err => {
console.error(err);
return res.status(500).json({ error: "Failed to delete topic." });
});
};
.catch((err) => {
console.error(err);
return res.status(500).json({error: 'Failed to delete topic.'})
})
}