diff --git a/pom.xml b/pom.xml
index 1c8dd9c27..3125b5fbe 100644
--- a/pom.xml
+++ b/pom.xml
@@ -21,6 +21,7 @@
+
diff --git a/src/main/java/hng_java_boilerplate/comment/controller/CommentController.java b/src/main/java/hng_java_boilerplate/comment/controller/CommentController.java
index 3990be743..5eb398d83 100644
--- a/src/main/java/hng_java_boilerplate/comment/controller/CommentController.java
+++ b/src/main/java/hng_java_boilerplate/comment/controller/CommentController.java
@@ -52,6 +52,18 @@ public ResponseEntity> createComment(@Valid @RequestBody RequestDto request){
}
}
+
+ @PutMapping("/edit/{commentId}")
+ @PreAuthorize("@CommentService.isUserAuthorizedToUpdateComment(#commentId, principal.username)")
+ public ResponseEntity updateComment(@PathVariable String commentId, @RequestParam String userId,@RequestBody Map requestBody) {
+ String newCommentText = requestBody.get("comment");
+ if (newCommentText == null || newCommentText.trim().isEmpty()) {
+ return ResponseEntity.badRequest().build();
+ }
+
+ Comment updatedComment = commentService.updateComment(commentId, userId, newCommentText);
+ return ResponseEntity.ok(updatedComment);
+ }
@DeleteMapping("/delete/{commentId}")
@PreAuthorize("@CommentService.isUserAuthorizedToDeleteComment(#commentId, principal.username)")
diff --git a/src/main/java/hng_java_boilerplate/comment/repository/CommentRepository.java b/src/main/java/hng_java_boilerplate/comment/repository/CommentRepository.java
index 2df106354..29ae494ae 100644
--- a/src/main/java/hng_java_boilerplate/comment/repository/CommentRepository.java
+++ b/src/main/java/hng_java_boilerplate/comment/repository/CommentRepository.java
@@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
+
import java.util.Optional;
diff --git a/src/main/java/hng_java_boilerplate/comment/service/CommentService.java b/src/main/java/hng_java_boilerplate/comment/service/CommentService.java
index ebe27d698..56fd01627 100644
--- a/src/main/java/hng_java_boilerplate/comment/service/CommentService.java
+++ b/src/main/java/hng_java_boilerplate/comment/service/CommentService.java
@@ -1,20 +1,25 @@
package hng_java_boilerplate.comment.service;
+import java.time.LocalDateTime;
+
+import hng_java_boilerplate.exception.NotFoundException;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Service;
+import org.springframework.web.server.ResponseStatusException;
+
import hng_java_boilerplate.comment.entity.Comment;
import hng_java_boilerplate.comment.repository.CommentRepository;
import hng_java_boilerplate.exception.UnAuthorizedException;
import hng_java_boilerplate.user.entity.User;
-import lombok.RequiredArgsConstructor;
-import org.springframework.http.HttpStatus;
-import org.springframework.stereotype.Service;
-import org.springframework.web.server.ResponseStatusException;
+import hng_java_boilerplate.user.repository.UserRepository;
-import java.time.LocalDateTime;
+import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
+ private final UserRepository userRepository;
public Comment createComment(String userId, String name, String comment){
User user = new User();
@@ -29,22 +34,41 @@ public Comment createComment(String userId, String name, String comment){
}
public Boolean isUserAuthorizedToDeleteComment(String commentId, String username){
- Comment comment = commentRepository.findById(commentId).orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND, "comment not found"));
+ Comment comment = commentRepository.findById(commentId)
+ .orElseThrow(()-> new NotFoundException("comment not found"));
return comment.getUser().getId().equals(username);
}
- public Comment softDeleteComment(String commentId, String userId){
- 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 NotFoundException("comment not found"));
+ return null;
+ }
+
+ public Comment softDeleteComment (String commentId, String userId){
+ Comment comment = commentRepository.findByCommentIdAndDeletedFalse(commentId)
+ .orElseThrow(() -> new NotFoundException("Comment not found"));
- if(!comment.getUser().getId().equals(userId)){
+ if (!comment.getUser().getId().equals(userId)) {
throw new UnAuthorizedException("Unauthorized user");
}
comment.setDeleted(true);
comment.setUpdatedAt(LocalDateTime.now());
return commentRepository.save(comment);
- }
+ }
+ public Comment updateComment(String commentId, String userId, String newCommentText) {
+ Comment comment = commentRepository.findById(commentId)
+ .orElseThrow(() -> new NotFoundException("Comment not found"));
+ userRepository.findById(userId)
+ .orElseThrow(() -> new NotFoundException("User not found"));
+ if (!comment.getUser().getId().equals(userId)) {
+ throw new UnAuthorizedException("Unable to update comment");
+ }
+ comment.setComment(newCommentText);
+ return commentRepository.save(comment);
+ }
}
diff --git a/src/test/java/hng_java_boilerplate/comment_unit_test/CommentServiceTest.java b/src/test/java/hng_java_boilerplate/comment_unit_test/CommentServiceTest.java
index b0ff30d7b..4a745aa84 100644
--- a/src/test/java/hng_java_boilerplate/comment_unit_test/CommentServiceTest.java
+++ b/src/test/java/hng_java_boilerplate/comment_unit_test/CommentServiceTest.java
@@ -5,6 +5,8 @@
import hng_java_boilerplate.comment.service.CommentService;
import hng_java_boilerplate.exception.UnAuthorizedException;
import hng_java_boilerplate.user.entity.User;
+import hng_java_boilerplate.user.repository.UserRepository;
+
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
@@ -27,6 +29,9 @@ class CommentServiceTest {
@InjectMocks
private CommentService commentService;
+ @Mock
+ private UserRepository userRepository;
+
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
@@ -143,4 +148,32 @@ void softDeleteComment_ShouldThrowUnauthorizedExceptionIfUserIsNotAuthorized() {
assertThrows(UnAuthorizedException.class, () ->
commentService.softDeleteComment(commentId, userId));
}
+
+
+
+
+
+ @Test
+ void updateComment_ShouldThrowUnauthorizedExceptionIfUserIsNotAuthorized() {
+ String commentId = "comment1";
+ String userId = "user1";
+ String differentUserId = "user2";
+ String newCommentText = "This is my updated comment.";
+
+ User user = new User();
+ user.setId(differentUserId);
+
+ Comment comment = new Comment();
+ comment.setCommentId(commentId);
+ comment.setUser(user);
+
+ when(commentRepository.findById(commentId)).thenReturn(Optional.of(comment));
+ when(userRepository.findById(userId)).thenReturn(Optional.of(new User()));
+ assertThrows(UnAuthorizedException.class, () ->
+ commentService.updateComment(commentId, userId, newCommentText));
+
+ verify(commentRepository, times(1)).findById(commentId);
+ verify(userRepository, times(1)).findById(userId);
+ }
+
}
\ No newline at end of file