Skip to content

Commit

Permalink
Merge pull request #64 from UniBond-jijijin/feature/61-block-comment
Browse files Browse the repository at this point in the history
[feature/61-block-comment] 댓글 차단하기
  • Loading branch information
5jisoo authored Jan 8, 2024
2 parents d5685f9 + 0937441 commit 59a11c7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public BaseResponse<?> blockPost(@RequestHeader("Authorization") Long loginId,
return new BaseResponse<>(e.getStatus());
}
}

@PostMapping("/comment")
public BaseResponse<?> blockComment(@RequestHeader("Authorization") Long loginId,
@RequestBody BlockReqDto reqDto) {
try {
return new BaseResponse<>(blockService.blockComment(reqDto));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.unibond.unibond.block.domain;

import com.unibond.unibond.comment.domain.Comment;
import com.unibond.unibond.common.BaseEntity;
import com.unibond.unibond.member.domain.Member;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@DynamicInsert
@NoArgsConstructor(access = PROTECTED)
public class CommentBlock extends BaseEntity {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "reporterId")
private Member reporter;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "reportedCommentId")
private Comment reportedComment;

@Builder
public CommentBlock(Member reporter, Comment reportedComment) {
this.reporter = reporter;
this.reportedComment = reportedComment;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.unibond.unibond.block.dto;

import com.unibond.unibond.block.domain.CommentBlock;
import com.unibond.unibond.block.domain.MemberBlock;
import com.unibond.unibond.block.domain.PostBlock;
import com.unibond.unibond.comment.domain.Comment;
import com.unibond.unibond.common.BaseException;
import com.unibond.unibond.member.domain.Member;
import com.unibond.unibond.post.domain.Post;
Expand All @@ -17,6 +19,7 @@
public class BlockReqDto {
private Long blockedMemberId;
private Long blockedPostId;
private Long blockedCommentId;

public MemberBlock toEntity(Member reporter, Member respondent) {
return MemberBlock.builder()
Expand All @@ -31,4 +34,11 @@ public PostBlock toEntity(Member reporter, Post reportedPost) {
.reportedPost(reportedPost)
.build();
}

public CommentBlock toEntity(Member reporter, Comment reportedComment) {
return CommentBlock.builder()
.reporter(reporter)
.reportedComment(reportedComment)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.unibond.unibond.block.repository;

import com.unibond.unibond.block.domain.CommentBlock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CommentBlockRepository extends JpaRepository<CommentBlock, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.unibond.unibond.block.service;

import com.unibond.unibond.block.domain.CommentBlock;
import com.unibond.unibond.block.domain.MemberBlock;
import com.unibond.unibond.block.domain.PostBlock;
import com.unibond.unibond.block.dto.BlockReqDto;
import com.unibond.unibond.block.repository.CommentBlockRepository;
import com.unibond.unibond.block.repository.MemberBlockRepository;
import com.unibond.unibond.block.repository.PostBlockRepository;
import com.unibond.unibond.comment.domain.Comment;
import com.unibond.unibond.comment.repository.CommentRepository;
import com.unibond.unibond.common.BaseException;
import com.unibond.unibond.common.BaseResponseStatus;
import com.unibond.unibond.common.service.LoginInfoService;
Expand All @@ -16,6 +20,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.unibond.unibond.common.BaseResponseStatus.*;

@Service
Expand All @@ -24,8 +31,10 @@ public class BlockService {
private final LoginInfoService loginInfoService;
private final MemberBlockRepository memberBlockRepository;
private final PostBlockRepository postBlockRepository;
private final CommentBlockRepository commentBlockRepository;
private final MemberRepository memberRepository;
private final PostRepository postRepository;
private final CommentRepository commentRepository;

@Transactional
public BaseResponseStatus blockMember(BlockReqDto reqDto) throws BaseException {
Expand Down Expand Up @@ -59,6 +68,37 @@ public BaseResponseStatus blockPost(BlockReqDto reqDto) throws BaseException {
}
}

@Transactional
public BaseResponseStatus blockComment(BlockReqDto reqDto) throws BaseException {
try {
Member reporter = loginInfoService.getLoginMember();
if (reqDto.getBlockedCommentId() == null) throw new BaseException(NULL_PROPERTY);
Comment comment = findComment(reqDto.getBlockedCommentId());
CommentBlock commentBlock = reqDto.toEntity(reporter, comment);
blockChildComments(reporter, comment);
commentBlockRepository.save(commentBlock);
return SUCCESS;
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

private void blockChildComments(Member reporter, Comment comment) {
List<Comment> childCommentList = comment.getChildCommentList();
if (!childCommentList.isEmpty()) {
List<CommentBlock> commentBlockList = childCommentList.stream().map(
childComment -> CommentBlock.builder()
.reportedComment(childComment)
.reporter(reporter)
.build()
).collect(Collectors.toList());

commentBlockRepository.saveAll(commentBlockList);
}
}

private Member findMember(Long memberId) throws BaseException {
return memberRepository.findById(memberId)
.orElseThrow(() -> new BaseException(INVALID_MEMBER_ID));
Expand All @@ -68,4 +108,9 @@ private Post findPost(Long postId) throws BaseException {
return postRepository.findById(postId)
.orElseThrow(() -> new BaseException(INVALID_POST_ID));
}

private Comment findComment(Long commentId) throws BaseException {
return commentRepository.findById(commentId)
.orElseThrow(() -> new BaseException(INVALID_COMMENT_ID));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {

@Query("select c from Comment c " +
"left join MemberBlock mb on ( c.member = mb.respondent and mb.reporter.id = :loginId ) " +
"left join CommentBlock cb on ( c = cb.reportedComment and cb.reporter.id = :loginId ) " +
"join fetch c.member " +
"join fetch c.member.disease " +
"where c.parentComment = :parentComment " +
"and c.post.id = :postId " +
"and c.status = 'ACTIVE' " +
"and mb.id IS NULL " +
"and c.post.id = :postId " +
"and c.status = 'ACTIVE' " +
"and mb.id IS NULL and cb.id IS NULL " +
"order by c.createdDate desc ")
Page<Comment> findCommentsByParentCommentFetchOwner(@Param("postId") Long postId,
@Param("parentComment") Comment parentComment,
Expand All @@ -34,16 +35,18 @@ Page<Comment> findCommentsByParentCommentFetchOwner(@Param("postId") Long postId

@Query("select c from Comment c " +
"left join MemberBlock mb on ( c.member = mb.respondent and mb.reporter.id = :loginId ) " +
"left join CommentBlock cb on ( c = cb.reportedComment and cb.reporter.id = :loginId ) " +
"join fetch c.member " +
"where c.post = :post and c.parentComment = null and c.status = 'ACTIVE' and mb.id IS NULL " +
"where c.post = :post and c.parentComment = null and c.status = 'ACTIVE' and mb.id IS NULL and cb.id IS NULL " +
"order by c.createdDate desc ")
Page<Comment> findParentCommentsByPostFetchOwner(@Param("post") Post post,
@Param("loginId") Long loginId,
Pageable pageable);

@Query("select COUNT(c) from Comment c " +
"left join MemberBlock mb on ( c.member = mb.respondent and mb.reporter.id = :loginId ) " +
"where c.post = :post and c.status = 'ACTIVE' and mb.id IS NULL ")
"left join CommentBlock cb on ( c = cb.reportedComment and cb.reporter.id = :loginId ) " +
"where c.post = :post and c.status = 'ACTIVE' and mb.id IS NULL and cb.id IS NULL ")
Integer getCommentCountByPost(@Param("post") Post post,
@Param("loginId") Long loginId);

Expand Down

0 comments on commit 59a11c7

Please sign in to comment.