Skip to content

Feat] User can update their respective comment #685 Open#700

Open
Ajiboye-01 wants to merge 7 commits intohngprojects:devfrom
Ajiboye-01:dev
Open

Feat] User can update their respective comment #685 Open#700
Ajiboye-01 wants to merge 7 commits intohngprojects:devfrom
Ajiboye-01:dev

Conversation

@Ajiboye-01
Copy link

Description
Users should be able to edit comments they have made.

Acceptance Criteria
A user can update their own comments.
Endpoint
PUT /edit/{commentId}

Payload

{
"commentId": "comment ID"
"userId": "user@gmail.com"
}

Response

{
  "status": "200",
  "message": "The account has been successfully edited.",
}

Proper authorization checks should be in place to prevent unauthorized editing.
Purpose
Allow users to manage their own content.
Requirements
Implement role-based access control (RBAC) to ensure only authorized users can edit comments.

Ensure database consistency and integrity after editing

Handle potential errors such as trying to edit a non-existent comment.

Provide a PUT API endpoint.

Expected Outcome
Users can edit their own comments.
Edit comments are either removed or replaced with a placeholder message.
Unauthorized users cannot edit comments.

Copy link
Contributor

@Am0du Am0du left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the comments

Comment comment = commentRepository.findByCommentIdAndDeletedFalse(commentId).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "Comment not found"));
public Boolean isUserAuthorizedToUpdateComment(String commentId, String username) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "comment not found"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NotFoundException class

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Comment object is there to ensure that the comment is inside the database befor deleting it


public Comment softDeleteComment (String commentId, String userId){
Comment comment = commentRepository.findByCommentIdAndDeletedFalse(commentId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Comment not found"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NotFoundException class

// It returns the updated comment
public Comment updateComment(String commentId, String userId, String newCommentText) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Comment not found"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NotFoundException class


// Ensure the user exists
userRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use NotFoundException class

public ResponseEntity<Comment> updateComment(@PathVariable String commentId, @RequestParam String userId,@RequestBody Map<String, String> requestBody) {
String newCommentText = requestBody.get("comment");
if (newCommentText == null || newCommentText.trim().isEmpty()) {
return ResponseEntity.badRequest().build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw BadRequestException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants