crud terminé sur les commentaires
This commit is contained in:
parent
8c7966deb2
commit
6867abd767
@ -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
|
||||
|
@ -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<Comment> 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());
|
||||
}
|
||||
}
|
||||
|
@ -101,7 +101,6 @@ public class PostController {
|
||||
} else {
|
||||
throw new ForbiddenExecption("You do not have permission to delete this post");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,10 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
|
||||
"comment_association.post_fk = :commentId")
|
||||
List<Comment> 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);
|
||||
|
@ -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<Comment> list() {
|
||||
return commentRepository.findAll();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user