crud terminé sur les commentaires

This commit is contained in:
Guams 2025-01-05 01:27:29 +01:00
parent 8c7966deb2
commit 6867abd767
5 changed files with 24 additions and 4 deletions

View File

@ -39,6 +39,7 @@ public class SpringSecurityConfig {
"/api/authors/{id}/posts", "/api/authors/{id}/posts",
"/api/posts", "/api/posts",
"/api/posts/{id}", "/api/posts/{id}",
"/api/comments/{id}",
"/api/comments/posts/{id}", "/api/comments/posts/{id}",
"/api/comments").permitAll() // Autorise les GET sur ces routes "/api/comments").permitAll() // Autorise les GET sur ces routes
.requestMatchers("/api/authors/login", "/api/authors/register").permitAll() // Autorise sans authentification .requestMatchers("/api/authors/login", "/api/authors/register").permitAll() // Autorise sans authentification

View File

@ -1,5 +1,4 @@
package com.guams.review.controller; package com.guams.review.controller;
import com.guams.review.exception.ForbiddenExecption; import com.guams.review.exception.ForbiddenExecption;
import com.guams.review.exception.NotFoundException; import com.guams.review.exception.NotFoundException;
import com.guams.review.model.AuthorRepository; import com.guams.review.model.AuthorRepository;
@ -34,6 +33,11 @@ public class CommentController {
return commentService.list(); 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}") @PostMapping("/posts/{id}")
public ResponseEntity<Comment> addComment(@RequestBody Comment comment, Authentication authentication, @PathVariable("id") Long postId) { public ResponseEntity<Comment> addComment(@RequestBody Comment comment, Authentication authentication, @PathVariable("id") Long postId) {
if (authentication == null || !authentication.isAuthenticated()) { if (authentication == null || !authentication.isAuthenticated()) {
@ -74,6 +78,10 @@ public class CommentController {
.setContent(commentIds.getContent())); .setContent(commentIds.getContent()));
} }
// @DeleteMapping("/{id}") @DeleteMapping("/{id}")
// public void deleteComment(@PathVariable Long id, Authentication authentication) {} 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());
}
} }

View File

@ -101,7 +101,6 @@ public class PostController {
} else { } else {
throw new ForbiddenExecption("You do not have permission to delete this post"); throw new ForbiddenExecption("You do not have permission to delete this post");
} }
} }
} }

View File

@ -21,6 +21,10 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
"comment_association.post_fk = :commentId") "comment_association.post_fk = :commentId")
List<Comment> getCommentsByCommentId(Long commentId); List<Comment> getCommentsByCommentId(Long commentId);
@Modifying
@Query("delete from comment_association where comment_fk = :commentId")
void deleteAssociationByCommentId(Long commentId);
@Modifying @Modifying
@Query("insert into comment_association(comment_fk, post_fk, author_fk) values (:commentId, :postId, :authorId);") @Query("insert into comment_association(comment_fk, post_fk, author_fk) values (:commentId, :postId, :authorId);")
void associateCommentToUserAndPost(UUID authorId, Long postId, Long commentId); void associateCommentToUserAndPost(UUID authorId, Long postId, Long commentId);

View File

@ -20,6 +20,14 @@ public class CommentService {
return commentRepository.findById(id); return commentRepository.findById(id);
} }
public void deleteAssociationByCommentId(Long commentId) {
commentRepository.deleteAssociationByCommentId(commentId);
}
public void delete(Comment comment) {
commentRepository.delete(comment);
}
public List<Comment> list() { public List<Comment> list() {
return commentRepository.findAll(); return commentRepository.findAll();
} }