Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/delete-comments-by-user-id #204

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions server/controllers/commentController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Comment from "../models/commentModel.js";
import User from "../models/userModel.js";

// Create a new comment
export const createComment = async (req, res) => {
Expand Down Expand Up @@ -157,6 +158,34 @@ export const deleteCommentById = async (req, res) => {
};

// Delete all comments by user id
export const deleteCommentsByUserId = async (req, res) => {
try {
// Check if the user exists
const user = await User.findById(req.params.id);
if (!user) {
return res.status(404).json({ message: 'User not found'});
}

// Check if there are comments to delete
const comments = await Comment.find({ author: req.params.id });
if (comments.length === 0) {
return res.status(404).json({ message: 'Comments not found'});
}

// Check if the user is the author of the comments
const isAuthor = comments.every(comment => comment.author.toString() === req.user._id);
if (!isAuthor) {
return res.status(403).json({ message: 'You do not have permission to delete these comments'});
}

// Delete the comments
await Comment.deleteMany({ author: req.params.id });
res.json({ message: 'Comments deleted successfully'});
}
catch (err) {
res.status(500).json({ message: err.message});
}
}

// Delete all comments by event id

Expand Down
4 changes: 2 additions & 2 deletions server/routes/commentRoutes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import authenticateToken, { isAdmin } from "../middleware/auth.js";
import { createComment, deleteCommentById, getAllComments, getCommentById, getCommentsByEventId, getCommentsByUserId, patchCommentById, updateCommentById } from "../controllers/commentController.js";
import { createComment, deleteCommentById, deleteCommentsByUserId, getAllComments, getCommentById, getCommentsByEventId, getCommentsByUserId, patchCommentById, updateCommentById } from "../controllers/commentController.js";

import express from "express";

Expand Down Expand Up @@ -34,7 +34,7 @@ router.patch("/:id", authenticateToken, patchCommentById);
router.delete("/:id", authenticateToken, deleteCommentById);

// Delete all comments by user id
// router.delete("/user/:id", authenticateToken, deleteCommentsByUserId);
router.delete("/user/:id", authenticateToken, deleteCommentsByUserId);

// Delete all comments by event id
// router.delete("/event/:id", authenticateToken, deleteCommentsByEventId);
Expand Down