diff --git a/src/main/java/com/guams/review/controller/CommentController.java b/src/main/java/com/guams/review/controller/CommentController.java index 4686222..c602d13 100644 --- a/src/main/java/com/guams/review/controller/CommentController.java +++ b/src/main/java/com/guams/review/controller/CommentController.java @@ -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 listCommentsByPostId(@PathVariable("post-id") Long postId) { + public List listCommentsByPostId(@PathVariable("post-id") Long postId) { postService.findById(postId).orElseThrow(() -> new NotFoundException("Post not found")); return commentService.getCommentsByCommentId(postId); } diff --git a/src/main/java/com/guams/review/model/CommentRepository.java b/src/main/java/com/guams/review/model/CommentRepository.java index 4a49651..18fea4b 100644 --- a/src/main/java/com/guams/review/model/CommentRepository.java +++ b/src/main/java/com/guams/review/model/CommentRepository.java @@ -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 { @Query("select post_fk, comment_fk, author_fk from comment_association where comment_fk = :commentId") Optional 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 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 getCommentsByCommentId(Long postId); @Modifying @Query("delete from comment_association where comment_fk = :commentId") diff --git a/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java b/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java new file mode 100644 index 0000000..68044d1 --- /dev/null +++ b/src/main/java/com/guams/review/model/dao/CommentWithAuthor.java @@ -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; +} diff --git a/src/main/java/com/guams/review/service/CommentService.java b/src/main/java/com/guams/review/service/CommentService.java index 48f4f8b..a1a8b08 100644 --- a/src/main/java/com/guams/review/service/CommentService.java +++ b/src/main/java/com/guams/review/service/CommentService.java @@ -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 getCommentsByCommentId(Long commentId) { + public List getCommentsByCommentId(Long commentId) { return commentRepository.getCommentsByCommentId(commentId); }