mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 02:08:47 +00:00
added function to delete topic
This commit is contained in:
parent
b255006f7a
commit
d9f6fb5d8e
@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable promise/always-return */
|
/* eslint-disable promise/always-return */
|
||||||
const admin = require('firebase-admin');
|
const { admin, db } = require("../util/admin");
|
||||||
exports.putTopic = (req, res) => {
|
exports.putTopic = (req, res) => {
|
||||||
|
|
||||||
const newTopic = {
|
const newTopic = {
|
||||||
@ -9,6 +9,7 @@ exports.putTopic = (req, res) => {
|
|||||||
admin.firestore().collection('topics').add(newTopic)
|
admin.firestore().collection('topics').add(newTopic)
|
||||||
.then((doc) => {
|
.then((doc) => {
|
||||||
const resTopic = newTopic;
|
const resTopic = newTopic;
|
||||||
|
newTopic.topicId = doc.id;
|
||||||
return res.status(200).json(resTopic);
|
return res.status(200).json(resTopic);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
@ -17,8 +18,6 @@ exports.putTopic = (req, res) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
exports.getAllTopics = (req, res) => {
|
exports.getAllTopics = (req, res) => {
|
||||||
admin.firestore().collection('topics').get()
|
admin.firestore().collection('topics').get()
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
@ -30,6 +29,25 @@ exports.getAllTopics = (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).json({error: 'Failed to fetch all posts written by specific user.'})
|
return res.status(500).json({error: 'Failed to fetch all topics.'})
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.deleteTopic = (req, res) => {
|
||||||
|
// TODO: handle add and delete by topic id
|
||||||
|
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!'});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
return res.status(500).json({error: 'Failed to delete topic.'})
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -55,7 +55,10 @@ app.post("/putPost", fbAuth, putPost);
|
|||||||
/*------------------------------------------------------------------*
|
/*------------------------------------------------------------------*
|
||||||
* handlers/topic.js *
|
* handlers/topic.js *
|
||||||
*------------------------------------------------------------------*/
|
*------------------------------------------------------------------*/
|
||||||
const { putTopic, getAllTopics
|
const {
|
||||||
|
putTopic,
|
||||||
|
getAllTopics,
|
||||||
|
deleteTopic
|
||||||
} = require("./handlers/topic");
|
} = require("./handlers/topic");
|
||||||
|
|
||||||
// add topic to database
|
// add topic to database
|
||||||
@ -64,4 +67,7 @@ app.post("/putTopic", fbAuth, putTopic);
|
|||||||
// get all topics from database
|
// get all topics from database
|
||||||
app.get("/getAllTopics", fbAuth, getAllTopics);
|
app.get("/getAllTopics", fbAuth, getAllTopics);
|
||||||
|
|
||||||
|
// delete a specific topic
|
||||||
|
app.delete("/deleteTopic/:topicId", fbAuth, deleteTopic);
|
||||||
|
|
||||||
exports.api = functions.https.onRequest(app);
|
exports.api = functions.https.onRequest(app);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user