update de la structure d'un commentaire lors du get
This commit is contained in:
parent
a2bc87e1a9
commit
48427e757a
@ -5,6 +5,7 @@ import com.guams.review.model.AuthorRepository;
|
|||||||
import com.guams.review.model.dao.Author;
|
import com.guams.review.model.dao.Author;
|
||||||
import com.guams.review.model.dao.Comment;
|
import com.guams.review.model.dao.Comment;
|
||||||
import com.guams.review.model.dao.CommentIds;
|
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.AuthorService;
|
||||||
import com.guams.review.service.CommentService;
|
import com.guams.review.service.CommentService;
|
||||||
import com.guams.review.service.PostService;
|
import com.guams.review.service.PostService;
|
||||||
@ -55,7 +56,7 @@ public class CommentController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/posts/{post-id}")
|
@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"));
|
postService.findById(postId).orElseThrow(() -> new NotFoundException("Post not found"));
|
||||||
return commentService.getCommentsByCommentId(postId);
|
return commentService.getCommentsByCommentId(postId);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.guams.review.model;
|
|||||||
|
|
||||||
import com.guams.review.model.dao.Comment;
|
import com.guams.review.model.dao.Comment;
|
||||||
import com.guams.review.model.dao.CommentIds;
|
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.Modifying;
|
||||||
import org.springframework.data.jdbc.repository.query.Query;
|
import org.springframework.data.jdbc.repository.query.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
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")
|
@Query("select post_fk, comment_fk, author_fk from comment_association where comment_fk = :commentId")
|
||||||
Optional<CommentIds> getCommentIdsByCommentId(Long 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 " +
|
@Query(value = " SELECT DISTINCT " +
|
||||||
"comment_association.post_fk = :commentId")
|
"c.id AS id, " +
|
||||||
List<Comment> getCommentsByCommentId(Long commentId);
|
"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
|
@Modifying
|
||||||
@Query("delete from comment_association where comment_fk = :commentId")
|
@Query("delete from comment_association where comment_fk = :commentId")
|
||||||
|
@ -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;
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.guams.review.service;
|
|||||||
import com.guams.review.model.CommentRepository;
|
import com.guams.review.model.CommentRepository;
|
||||||
import com.guams.review.model.dao.Comment;
|
import com.guams.review.model.dao.Comment;
|
||||||
import com.guams.review.model.dao.CommentIds;
|
import com.guams.review.model.dao.CommentIds;
|
||||||
|
import com.guams.review.model.dao.CommentWithAuthor;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -32,7 +33,7 @@ public class CommentService {
|
|||||||
return commentRepository.findAll();
|
return commentRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Comment> getCommentsByCommentId(Long commentId) {
|
public List<CommentWithAuthor> getCommentsByCommentId(Long commentId) {
|
||||||
return commentRepository.getCommentsByCommentId(commentId);
|
return commentRepository.getCommentsByCommentId(commentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user