This pull request implements the PUT /comments/:id route to update a specific comment by ID and includes author verification to ensure that only the author of the comment or an admin/moderator can update the comment.
Changes Implemented
Added updateCommentById method to commentController to handle updating a specific comment by ID.
Created a new route in commentRoutes.js for PUT /comments/:id, protected by authenticateToken middleware.
Implemented error handling to return 404 Not Found if the comment is not found.
Added verification to ensure only the author of the comment or an admin/moderator can update the comment.
Implemented error handling to return 403 Forbidden if the user does not have permission.
Verified functionality with Postman tests.
How to Test
Update Comment as Author:
Send a PUT request to http://localhost:3000/comments/:id with a valid comment ID where the author matches the authenticated user.
Ensure the comment is updated successfully.
Update Comment as Admin:
Send a PUT request to http://localhost:3000/comments/:id with a valid comment ID using an admin token.
Ensure the comment is updated successfully.
Unauthorized Update Attempt:
Send a PUT request to http://localhost:3000/comments/:id with a valid comment ID using a token from a different user (not the author and not an admin).
Ensure the response is 403 Forbidden.
Comment Not Found:
Send a PUT request to http://localhost:3000/comments/:id with a non-existent comment ID.
Summary
This pull request implements the
PUT /comments/:id
route to update a specific comment by ID and includes author verification to ensure that only the author of the comment or an admin/moderator can update the comment.Changes Implemented
updateCommentById
method tocommentController
to handle updating a specific comment by ID.commentRoutes.js
forPUT /comments/:id
, protected byauthenticateToken
middleware.404 Not Found
if the comment is not found.403 Forbidden
if the user does not have permission.How to Test
Update Comment as Author:
PUT
request tohttp://localhost:3000/comments/:id
with a validcomment ID
where theauthor
matches the authenticated user.Update Comment as Admin:
PUT
request tohttp://localhost:3000/comments/:id
with a validcomment ID
using an admin token.Unauthorized Update Attempt:
PUT
request tohttp://localhost:3000/comments/:id
with a validcomment ID
using a token from a different user (not the author and not an admin).403 Forbidden
.Comment Not Found:
PUT
request tohttp://localhost:3000/comments/:id
with a non-existentcomment ID
.404 Not Found
.Example Request
Example Response
Resolves #150