review-api/src/main/java/com/guams/review/model/CommentRepository.java

45 lines
1.9 KiB
Java

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;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public interface CommentRepository extends CrudRepository<Comment, Long> {
List<Comment> findAll();
@Query("select post_fk, comment_fk, author_fk from comment_association where comment_fk = :commentId")
Optional<CommentIds> getCommentIdsByCommentId(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.comment_fk = c.id" +
" JOIN post p ON p.id = ca.post_fk" +
" JOIN author a ON ca.author_fk = a.id" +
" WHERE p.id = 3")
List<CommentWithAuthor> getCommentsByPostId(Long postId);
@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);
}