diff --git a/src/main/java/com/guams/review/configuration/SpringSecurityConfig.java b/src/main/java/com/guams/review/configuration/SpringSecurityConfig.java index 7801301..7f89e97 100644 --- a/src/main/java/com/guams/review/configuration/SpringSecurityConfig.java +++ b/src/main/java/com/guams/review/configuration/SpringSecurityConfig.java @@ -39,6 +39,7 @@ public class SpringSecurityConfig { "/api/authors/{id}/posts", "/api/posts", "/api/posts/{id}", + "/api/comments/{id}", "/api/comments/posts/{id}", "/api/comments").permitAll() // Autorise les GET sur ces routes .requestMatchers("/api/authors/login", "/api/authors/register").permitAll() // Autorise sans authentification diff --git a/src/main/java/com/guams/review/controller/CommentController.java b/src/main/java/com/guams/review/controller/CommentController.java index 5e05d7d..25b1258 100644 --- a/src/main/java/com/guams/review/controller/CommentController.java +++ b/src/main/java/com/guams/review/controller/CommentController.java @@ -1,5 +1,4 @@ package com.guams.review.controller; - import com.guams.review.exception.ForbiddenExecption; import com.guams.review.exception.NotFoundException; import com.guams.review.model.AuthorRepository; @@ -34,6 +33,11 @@ public class CommentController { return commentService.list(); } + @GetMapping("/{id}") + public Comment getCommentById(@PathVariable("id") Long id) { + return commentService.findById(id).orElseThrow(() -> new NotFoundException("Comment not found")); + } + @PostMapping("/posts/{id}") public ResponseEntity addComment(@RequestBody Comment comment, Authentication authentication, @PathVariable("id") Long postId) { if (authentication == null || !authentication.isAuthenticated()) { @@ -74,6 +78,10 @@ public class CommentController { .setContent(commentIds.getContent())); } -// @DeleteMapping("/{id}") -// public void deleteComment(@PathVariable Long id, Authentication authentication) {} + @DeleteMapping("/{id}") + public void deleteComment(@PathVariable Long id, Authentication authentication) { + Comment commentToDelete = commentService.findById(id).orElseThrow(() -> new NotFoundException("Comment not found")); + commentService.delete(commentToDelete); + commentService.deleteAssociationByCommentId(commentToDelete.getId()); + } } diff --git a/src/main/java/com/guams/review/controller/PostController.java b/src/main/java/com/guams/review/controller/PostController.java index 9f0efc2..67ff885 100644 --- a/src/main/java/com/guams/review/controller/PostController.java +++ b/src/main/java/com/guams/review/controller/PostController.java @@ -101,7 +101,6 @@ public class PostController { } else { throw new ForbiddenExecption("You do not have permission to delete this post"); } - } } diff --git a/src/main/java/com/guams/review/model/CommentRepository.java b/src/main/java/com/guams/review/model/CommentRepository.java index a306253..4a49651 100644 --- a/src/main/java/com/guams/review/model/CommentRepository.java +++ b/src/main/java/com/guams/review/model/CommentRepository.java @@ -21,6 +21,10 @@ public interface CommentRepository extends CrudRepository { "comment_association.post_fk = :commentId") List getCommentsByCommentId(Long commentId); + @Modifying + @Query("delete from comment_association where comment_fk = :commentId") + void deleteAssociationByCommentId(Long commentId); + @Modifying @Query("insert into comment_association(comment_fk, post_fk, author_fk) values (:commentId, :postId, :authorId);") void associateCommentToUserAndPost(UUID authorId, Long postId, Long commentId); diff --git a/src/main/java/com/guams/review/service/CommentService.java b/src/main/java/com/guams/review/service/CommentService.java index d5cb6ce..48f4f8b 100644 --- a/src/main/java/com/guams/review/service/CommentService.java +++ b/src/main/java/com/guams/review/service/CommentService.java @@ -20,6 +20,14 @@ public class CommentService { return commentRepository.findById(id); } + public void deleteAssociationByCommentId(Long commentId) { + commentRepository.deleteAssociationByCommentId(commentId); + } + + public void delete(Comment comment) { + commentRepository.delete(comment); + } + public List list() { return commentRepository.findAll(); }