Skip to content

Commit

Permalink
Merge pull request #306 from 6QuizOnTheBlock/be/feat/#295-social_count
Browse files Browse the repository at this point in the history
feat: ν•™μƒκ°„μ˜ 관계 - κ²Œμ‹œκΈ€, λŒ“κΈ€ Count μ €μž₯ν•˜κΈ°
  • Loading branch information
Henry-Cha authored May 13, 2024
2 parents eb6adae + f201a6b commit dbaa649
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface CommentControllerDocs {
(message : "멀버가 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.")

(message : "κ²Œμ‹œκΈ€μ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")

(message : "두 λ©€λ²„κ°„μ˜ 관계λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
""", content = @Content),
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Post {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
long id;
@ManyToOne(fetch = FetchType.LAZY)
Member author;
@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.quiz.ourclass.domain.board.repository;

import com.quiz.ourclass.domain.board.entity.Comment;
import com.quiz.ourclass.domain.board.entity.Post;
import com.quiz.ourclass.domain.member.entity.Member;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -11,4 +13,8 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
void deleteByParentId(Long parentId);

void deleteByPostId(Long postId);

boolean existsByPostAndMember(Post post, Member member);

boolean existsByParentIdAndMember(Long parentId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.quiz.ourclass.domain.notice.entity.Notice;
import com.quiz.ourclass.domain.notice.entity.NoticeType;
import com.quiz.ourclass.domain.notice.repository.NoticeRepository;
import com.quiz.ourclass.domain.organization.entity.Relationship;
import com.quiz.ourclass.domain.organization.repository.RelationshipRepository;
import com.quiz.ourclass.global.dto.FcmDTO;
import com.quiz.ourclass.global.exception.ErrorCode;
import com.quiz.ourclass.global.exception.GlobalException;
Expand All @@ -30,6 +32,7 @@ public class CommentServiceImpl implements CommentService {
private final PostRepository postRepository;
private final CommentRepository commentRepository;
private final NoticeRepository noticeRepository;
private final RelationshipRepository relationshipRepository;
private final UserAccessUtil userAccessUtil;
private final FcmUtil fcmUtil;
private final CommentMapper commentMapper;
Expand All @@ -43,6 +46,8 @@ public Long write(CommentRequest request) {
Post post = postRepository.findById(request.boardId())
.orElseThrow(() -> new GlobalException(ErrorCode.POST_NOT_FOUND));

long orgId = post.getOrganization().getId();

//κ²Œμ‹œκΈ€μ„ μž‘μ„±ν•œ μ‚¬μš©μž 단체와 λŒ“κΈ€ μž‘μ„±μžμ˜ 단체가 같은지 확인
if (commentWriter.getRole() == Role.STUDENT) {
boolean isSameOrganization = post.getOrganization().getMemberOrganizations().stream()
Expand All @@ -56,6 +61,22 @@ public Long write(CommentRequest request) {
.orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_IN_ORGANIZATION));
}

if (post.getAuthor().getId() != commentWriter.getId()) {
//ν•΄λ‹Ή κ²Œμ‹œκΈ€μ— λ°˜μ‘(λŒ“κΈ€ μž‘μ„±)을 ν•˜μ˜€λŠ”μ§€ 확인
boolean isFirstComment = request.parentId() == 0L
&& !commentRepository.existsByPostAndMember(post, commentWriter);
if (isFirstComment) {
updateSocialCount(orgId, post.getAuthor(), commentWriter);
updateSocialCount(orgId, commentWriter, post.getAuthor());
}
//ν•΄λ‹Ή λŒ“κΈ€μ— λ°˜μ‘(λŒ€λŒ“κΈ€ μž‘μ„±)을 ν•˜μ˜€λŠ”μ§€ 확인
if (request.parentId() > 0L) {
Comment parentComment = commentRepository.findById(request.parentId())
.orElseThrow(() -> new GlobalException(ErrorCode.COMMENT_NOT_FOUND));
checkAndUpdateFirstResponse(post, commentWriter, parentComment);
}
}

//κ²Œμ‹œκΈ€ λŒ“κΈ€ μ €μž₯ (λΆ€λͺ¨ λŒ“κΈ€μ΄λ©΄ 0L둜 μ €μž₯λ©λ‹ˆλ‹€.)
Comment comment = commentMapper.CommentRequestTocomment(request);
comment.setCreateTime(LocalDateTime.now());
Expand Down Expand Up @@ -161,4 +182,34 @@ public Boolean report(Long commentId) {

return true;
}

/*
* λΆ€λͺ¨ λŒ“κΈ€ μž‘μ„±μžκ°€ κ²Œμ‹œκΈ€ μž‘μ„±μžμΌ λ•Œ
* λŒ€λŒ“κΈ€ μž‘μ„±μžκ°€ κ²Œμ‹œκΈ€μ— λŒ“κΈ€λ‘œ λ°˜μ‘μ„ ν•œ 적이 없을 λ•Œ
*
* λΆ€λͺ¨ λŒ“κΈ€ μž‘μ„±μžκ°€ κ²Œμ‹œκΈ€ μž‘μ„±μžλž‘ λ‹€λ₯Ό λ–„
* λŒ€λŒ“κΈ€ μž‘μ„±μžκ°€ λΆ€λͺ¨ λŒ“κΈ€ μž‘μ„±μžμ—κ²Œ λ°˜μ‘μ„ ν•˜μ§€ μ•Šμ•˜μ„ λ•Œ
*/
private void checkAndUpdateFirstResponse(
Post post, Member commentWriter, Comment parentComment) {
if ((post.getAuthor().getId() == parentComment.getMember().getId() &&
!commentRepository.existsByPostAndMember(post, commentWriter))
||
(post.getAuthor().getId() != parentComment.getMember().getId() &&
!commentRepository.existsByParentIdAndMember(parentComment.getId(),
commentWriter))) {
long orgId = post.getOrganization().getId();
updateSocialCount(orgId, parentComment.getMember(), commentWriter);
updateSocialCount(orgId, commentWriter, parentComment.getMember());
}
}

private void updateSocialCount(Long orgId, Member member1, Member member2) {
Relationship relationship =
relationshipRepository.findByOrganizationIdAndMember1IdAndMember2Id(
orgId, member1.getId(), member2.getId()
).orElseThrow(() -> new GlobalException(ErrorCode.RELATION_NOT_FOUND));

relationship.updateSocialCount();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ChatFilter {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
long id;
@ManyToOne(fetch = FetchType.LAZY)
Organization organization;
String badWord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Relationship {
int tagGreetingCount;
int designGroupCount;
int freeGroupCount;
int socialCount;

public int updateTagGreetingCount() {
this.tagGreetingCount += 1;
Expand All @@ -47,4 +48,8 @@ public int updateFreeGroupCount() {
this.freeGroupCount += 1;
return this.freeGroupCount;
}

public void updateSocialCount() {
this.socialCount += 1;
}
}

0 comments on commit dbaa649

Please sign in to comment.