Delete profiles and posts
This commit is contained in:
@@ -9,7 +9,12 @@ DB.getDB.then((DB) => {
|
||||
|
||||
const getProfileId = (req) => {
|
||||
return DB.ObjectID(req.cookies.profile_id || req.query.profile_id || req.body.profile_id);
|
||||
}
|
||||
};
|
||||
|
||||
const postBelongToProfile = (post, profileid) => {
|
||||
if(!post) return false;
|
||||
return post.profileid === profileid;
|
||||
};
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const profileid = getProfileId(req);
|
||||
@@ -144,6 +149,20 @@ DB.getDB.then((DB) => {
|
||||
});
|
||||
});
|
||||
|
||||
router.delete("/:id", async (req, res) => {
|
||||
const profileid = getProfileId(req);
|
||||
const postId = req.params.id;
|
||||
const post = await DB.getPost(postId);
|
||||
if(!postBelongToProfile(post, profileid))
|
||||
return res.json({
|
||||
status: "This post is not yours."
|
||||
});
|
||||
await DB.removePost(postId);
|
||||
return res.json({
|
||||
status: "ok"
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user