Reformat code

This commit is contained in:
Leon Liang
2019-10-27 02:53:12 -04:00
parent 1c81ae1663
commit 13a1401759
5 changed files with 258 additions and 224 deletions

View File

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

View File

@@ -44,8 +44,7 @@ app.get("/user", fbAuth, getAuthenticatedUser);
/*------------------------------------------------------------------*
* handlers/post.js *
*------------------------------------------------------------------*/
const { getallPostsforUser, putPost
} = require("./handlers/post");
const { getallPostsforUser, putPost } = require("./handlers/post");
app.get("/getallPostsforUser", getallPostsforUser);
@@ -55,11 +54,7 @@ app.post("/putPost", fbAuth, putPost);
/*------------------------------------------------------------------*
* handlers/topic.js *
*------------------------------------------------------------------*/
const {
putTopic,
getAllTopics,
deleteTopic
} = require("./handlers/topic");
const { putTopic, getAllTopics, deleteTopic } = require("./handlers/topic");
// add topic to database
app.post("/putTopic", fbAuth, putTopic);