Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<developers>
<developer/>
</developers>

<scm>
<connection/>
<developerConnection/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public ResponseEntity<?> createComment(@Valid @RequestBody RequestDto request){
}

}

@PutMapping("/edit/{commentId}")
@PreAuthorize("@CommentService.isUserAuthorizedToUpdateComment(#commentId, principal.username)")
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

}

Comment updatedComment = commentService.updateComment(commentId, userId, newCommentText);
return ResponseEntity.ok(updatedComment);
}

@DeleteMapping("/delete/{commentId}")
@PreAuthorize("@CommentService.isUserAuthorizedToDeleteComment(#commentId, principal.username)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


import java.util.Optional;


Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +29,9 @@ class CommentServiceTest {
@InjectMocks
private CommentService commentService;

@Mock
private UserRepository userRepository;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
Expand Down Expand Up @@ -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);
}

}