-
Notifications
You must be signed in to change notification settings - Fork 55
Feat] User can update their respective comment #685 Open #700
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
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the comments
src/main/java/hng_java_boilerplate/comment/service/CommentService.java
Outdated
Show resolved
Hide resolved
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")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use NotFoundException class
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throw BadRequestException
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
Response
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.