Skip to content

Commit 907581d

Browse files
committed
modify update a comment function
1 parent 78e8f4a commit 907581d

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

backend/src/controllers/activity.controller.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -237,86 +237,87 @@ const getComments = async (req, res) => {
237237
}
238238
};
239239

240-
// delete a comment
241-
const deleteComment = async (req, res) => {
240+
// update a comment
241+
const updateComment = async (req, res) => {
242242
try {
243243
const user = req.user;
244-
const { commentId } = req.params; // get comment id from url
244+
const { commentId } = req.params;
245+
const { body } = req.body;
246+
// validate - body is required
247+
if (!body || body.trim() === "") {
248+
return res.status(400).json({
249+
message: "Comment body is required",
250+
});
251+
}
245252

253+
// find the comment
246254
const comment = await Comment.findByPk(commentId);
247255
if (!comment) {
248256
return res.status(404).json({
249257
message: "Comment not found",
250258
});
251259
}
252260

261+
// check if user is the owner of the comment
253262
if (comment.user_id !== user.id) {
254263
return res.status(403).json({
255-
message: "You can only delete your own comments",
264+
message: "Not authorized to update this comment",
256265
});
257266
}
258267

259-
// delete the comment
260-
await comment.destroy();
268+
// update the comment
269+
comment.body = body;
270+
await comment.save();
271+
261272
res.status(200).json({
262-
message: "Comment deleted successfully",
273+
message: "Comment updated successfully",
274+
comment,
275+
// comment: {
276+
// id: comment.id,
277+
// body: comment.body,
278+
// user_id: comment.user_id,
279+
// dataset_id: comment.dataset_id,
280+
// created_at: comment.created_at,
281+
// updated_at: comment.updated_at,
282+
// },
263283
});
264284
} catch (error) {
265-
console.error("Delete comment error:", error);
285+
console.error("Update comment error:", error);
266286
res.status(500).json({
267-
message: "Error deleting comment",
287+
message: "Error updating comment",
268288
error: error.message,
269289
});
270290
}
271291
};
272292

273-
// update a comment
274-
const updateComment = async (req, res) => {
293+
// delete a comment
294+
const deleteComment = async (req, res) => {
275295
try {
276296
const user = req.user;
277-
const { commentId } = req.params;
278-
const { body } = req.body;
279-
// validate - body is required
280-
if (!body || body.trim() === "") {
281-
return res.status(400).json({
282-
message: "Comment body is required",
283-
});
284-
}
297+
const { commentId } = req.params; // get comment id from url
285298

286-
// find the comment
287299
const comment = await Comment.findByPk(commentId);
288300
if (!comment) {
289301
return res.status(404).json({
290302
message: "Comment not found",
291303
});
292304
}
293305

294-
// check if user is the owner of the comment
295306
if (comment.user_id !== user.id) {
296307
return res.status(403).json({
297-
message: "You can only update your own comments",
308+
message: "You can only delete your own comments",
298309
});
299310
}
300311

301-
// update the comment
302-
comment.body = body;
303-
await comment.save();
304-
312+
// delete the comment
313+
await comment.destroy();
305314
res.status(200).json({
306-
message: "Comment updated successfully",
307-
comment: {
308-
id: comment.id,
309-
body: comment.body,
310-
user_id: comment.user_id,
311-
dataset_id: comment.dataset_id,
312-
created_at: comment.created_at,
313-
updated_at: comment.updated_at,
314-
},
315+
message: "Comment deleted successfully",
315316
});
316317
} catch (error) {
317-
console.error("Update comment error:", error);
318+
console.error("Delete comment error:", error);
318319
res.status(500).json({
319-
message: "Error updating comment",
320+
message: "Error deleting comment",
320321
error: error.message,
321322
});
322323
}

0 commit comments

Comments
 (0)