Feat] User can update their respective comment #685 Open#700
Open
Ajiboye-01 wants to merge 7 commits intohngprojects:devfrom
Open
Feat] User can update their respective comment #685 Open#700Ajiboye-01 wants to merge 7 commits intohngprojects:devfrom
Ajiboye-01 wants to merge 7 commits intohngprojects:devfrom
Conversation
Am0du
requested changes
Mar 2, 2025
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")); |
Author
There was a problem hiding this comment.
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")); |
| // 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")); |
|
|
||
| // Ensure the user exists | ||
| userRepository.findById(userId) | ||
| .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")); |
Am0du
requested changes
Mar 2, 2025
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.