update de la structure d'un commentaire lors du get

This commit is contained in:
Guams 2025-01-12 22:48:49 +01:00
parent a2bc87e1a9
commit 48427e757a
4 changed files with 39 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import com.guams.review.model.AuthorRepository;
import com.guams.review.model.dao.Author;
import com.guams.review.model.dao.Comment;
import com.guams.review.model.dao.CommentIds;
import com.guams.review.model.dao.CommentWithAuthor;
import com.guams.review.service.AuthorService;
import com.guams.review.service.CommentService;
import com.guams.review.service.PostService;
@ -55,7 +56,7 @@ public class CommentController {
}
@GetMapping("/posts/{post-id}")
public List<Comment> listCommentsByPostId(@PathVariable("post-id") Long postId) {
public List<CommentWithAuthor> listCommentsByPostId(@PathVariable("post-id") Long postId) {
postService.findById(postId).orElseThrow(() -> new NotFoundException("Post not found"));
return commentService.getCommentsByCommentId(postId);
}

View File

@ -2,6 +2,7 @@ package com.guams.review.model;
import com.guams.review.model.dao.Comment;
import com.guams.review.model.dao.CommentIds;
import com.guams.review.model.dao.CommentWithAuthor;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
@ -17,9 +18,19 @@ public interface CommentRepository extends CrudRepository<Comment, Long> {
@Query("select post_fk, comment_fk, author_fk from comment_association where comment_fk = :commentId")
Optional<CommentIds> getCommentIdsByCommentId(Long commentId);
@Query("select distinct comment.id, comment.content, comment.comment_date, comment.is_updated from comment join comment_association on " +
"comment_association.post_fk = :commentId")
List<Comment> getCommentsByCommentId(Long commentId);
@Query(value = " SELECT DISTINCT " +
"c.id AS id, " +
"a.id AS author_id, " +
"a.name AS author_name, " +
"a.profile_picture AS profile_picture, " +
"a.role AS author_role, " +
"c.content AS content, " +
"c.comment_date AS comment_date, " +
"c.is_updated AS is_updated " +
"FROM comment c " +
"JOIN comment_association ca ON ca.post_fk = :postId " +
"JOIN author a ON ca.author_fk = a.id")
List<CommentWithAuthor> getCommentsByCommentId(Long postId);
@Modifying
@Query("delete from comment_association where comment_fk = :commentId")

View File

@ -0,0 +1,21 @@
package com.guams.review.model.dao;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.sql.Timestamp;
import java.util.UUID;
@Getter
@Setter
@Accessors(chain = true)
public class CommentWithAuthor {
private Long id;
private UUID authorId;
private String authorName;
private String profilePicture;
private String authorRole;
private String content;
private Timestamp commentDate;
private Boolean isUpdated;
}

View File

@ -3,6 +3,7 @@ package com.guams.review.service;
import com.guams.review.model.CommentRepository;
import com.guams.review.model.dao.Comment;
import com.guams.review.model.dao.CommentIds;
import com.guams.review.model.dao.CommentWithAuthor;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -32,7 +33,7 @@ public class CommentService {
return commentRepository.findAll();
}
public List<Comment> getCommentsByCommentId(Long commentId) {
public List<CommentWithAuthor> getCommentsByCommentId(Long commentId) {
return commentRepository.getCommentsByCommentId(commentId);
}