|
| 1 | +package org.devridge.api.application.community.project; |
| 2 | + |
| 3 | +import javax.transaction.Transactional; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import org.devridge.api.common.exception.common.DataNotFoundException; |
| 6 | +import org.devridge.api.common.util.SecurityContextHolderUtil; |
| 7 | +import org.devridge.api.domain.community.entity.LikeStatus; |
| 8 | +import org.devridge.api.domain.community.entity.Project; |
| 9 | +import org.devridge.api.domain.community.entity.ProjectComment; |
| 10 | +import org.devridge.api.domain.community.entity.ProjectCommentLikeDislike; |
| 11 | +import org.devridge.api.domain.community.entity.id.ProjectCommentLikeDislikeId; |
| 12 | +import org.devridge.api.domain.community.exception.MyCommunityForbiddenException; |
| 13 | +import org.devridge.api.domain.member.entity.Member; |
| 14 | +import org.devridge.api.infrastructure.community.project.ProjectCommentLikeDislikeRepository; |
| 15 | +import org.devridge.api.infrastructure.community.project.ProjectCommentRepository; |
| 16 | +import org.devridge.api.infrastructure.community.project.ProjectRepository; |
| 17 | +import org.devridge.api.infrastructure.member.MemberRepository; |
| 18 | +import org.springframework.stereotype.Service; |
| 19 | + |
| 20 | +@RequiredArgsConstructor |
| 21 | +@Service |
| 22 | +public class ProjectCommentLikeDislikeService { |
| 23 | + |
| 24 | + private final ProjectCommentLikeDislikeRepository projectCommentLikeDislikeRepository; |
| 25 | + private final ProjectCommentRepository projectCommentRepository; |
| 26 | + private final MemberRepository memberRepository; |
| 27 | + private final ProjectCommentLikeDislikeMapper projectCommentLikeDislikeMapper; |
| 28 | + private final ProjectRepository projectRepository; |
| 29 | + |
| 30 | + @Transactional |
| 31 | + public void createProjectCommentLike(Long projectId, Long commentId) { |
| 32 | + Long accessMemberId = SecurityContextHolderUtil.getMemberId(); |
| 33 | + Member member = getMemberById(accessMemberId); |
| 34 | + Project project = getProjectById(projectId); |
| 35 | + ProjectComment comment = getCommentById(commentId); |
| 36 | + ProjectCommentLikeDislikeId projectCommentLikeDislikeId = |
| 37 | + new ProjectCommentLikeDislikeId(member.getId(), comment.getId()); |
| 38 | + |
| 39 | + if (accessMemberId.equals(comment.getMember().getId())) { |
| 40 | + throw new MyCommunityForbiddenException(403, "내가 작성한 글은 추천할 수 없습니다."); |
| 41 | + } |
| 42 | + |
| 43 | + projectCommentLikeDislikeRepository.findById(projectCommentLikeDislikeId).ifPresentOrElse( |
| 44 | + ProjectCommentLikeDislike -> { |
| 45 | + LikeStatus status = ProjectCommentLikeDislike.getStatus(); |
| 46 | + |
| 47 | + if (status == LikeStatus.G) { |
| 48 | + changeIsDeletedStatus(ProjectCommentLikeDislike); |
| 49 | + } |
| 50 | + |
| 51 | + if (status == LikeStatus.B) { |
| 52 | + if (ProjectCommentLikeDislike.getIsDeleted()) { |
| 53 | + projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislikeId); |
| 54 | + } |
| 55 | + projectCommentLikeDislikeRepository.updateLikeDislike(projectCommentLikeDislikeId, |
| 56 | + LikeStatus.G); |
| 57 | + } |
| 58 | + }, |
| 59 | + () -> { |
| 60 | + ProjectCommentLikeDislike commentLikeDislike = |
| 61 | + projectCommentLikeDislikeMapper.toProjectCommentLikeDislike(member, comment, LikeStatus.G); |
| 62 | + projectCommentLikeDislikeRepository.save(commentLikeDislike); |
| 63 | + } |
| 64 | + ); |
| 65 | + updateLikeDislike(projectCommentLikeDislikeId.getCommentId()); |
| 66 | + } |
| 67 | + |
| 68 | + @Transactional |
| 69 | + public void createProjectCommentDislike(Long projectId, Long commentId) { |
| 70 | + Long accessMemberId = SecurityContextHolderUtil.getMemberId(); |
| 71 | + Member member = getMemberById(accessMemberId); |
| 72 | + Project project = getProjectById(projectId); |
| 73 | + ProjectComment comment = getCommentById(commentId); |
| 74 | + ProjectCommentLikeDislikeId projectCommentLikeDislikeId = |
| 75 | + new ProjectCommentLikeDislikeId(member.getId(), comment.getId()); |
| 76 | + |
| 77 | + if (accessMemberId.equals(comment.getMember().getId())) { |
| 78 | + throw new MyCommunityForbiddenException(403, "내가 작성한 글은 비추천할 수 없습니다."); |
| 79 | + } |
| 80 | + |
| 81 | + projectCommentLikeDislikeRepository.findById(projectCommentLikeDislikeId).ifPresentOrElse( |
| 82 | + ProjectCommentLikeDislike -> { |
| 83 | + LikeStatus status = ProjectCommentLikeDislike.getStatus(); |
| 84 | + |
| 85 | + if (status == LikeStatus.B) { |
| 86 | + changeIsDeletedStatus(ProjectCommentLikeDislike); |
| 87 | + } |
| 88 | + |
| 89 | + if (status == LikeStatus.G) { |
| 90 | + if (ProjectCommentLikeDislike.getIsDeleted()) { |
| 91 | + projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislikeId); |
| 92 | + } |
| 93 | + projectCommentLikeDislikeRepository.updateLikeDislike(projectCommentLikeDislikeId, LikeStatus.B); |
| 94 | + } |
| 95 | + }, |
| 96 | + () -> { |
| 97 | + ProjectCommentLikeDislike commentLikeDislike = |
| 98 | + projectCommentLikeDislikeMapper.toProjectCommentLikeDislike(member, comment, LikeStatus.B); |
| 99 | + projectCommentLikeDislikeRepository.save(commentLikeDislike); |
| 100 | + } |
| 101 | + ); |
| 102 | + updateLikeDislike(projectCommentLikeDislikeId.getCommentId()); |
| 103 | + } |
| 104 | + |
| 105 | + private void updateLikeDislike(Long projectId) { |
| 106 | + Long likes = Long.valueOf(projectCommentLikeDislikeRepository.countProjectLikeDislikeById(projectId, LikeStatus.G)); |
| 107 | + Long disLikes = Long.valueOf(projectCommentLikeDislikeRepository.countProjectLikeDislikeById(projectId, LikeStatus.B)); |
| 108 | + projectCommentRepository.updateLikeDislike(likes, disLikes, projectId); |
| 109 | + } |
| 110 | + |
| 111 | + private void changeIsDeletedStatus(ProjectCommentLikeDislike projectCommentLikeDislike) { |
| 112 | + if (projectCommentLikeDislike.getIsDeleted()) { |
| 113 | + projectCommentLikeDislikeRepository.restoreById(projectCommentLikeDislike.getId()); |
| 114 | + } |
| 115 | + if (!projectCommentLikeDislike.getIsDeleted()) { |
| 116 | + projectCommentLikeDislikeRepository.deleteById(projectCommentLikeDislike.getId()); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private Member getMemberById(Long memberId) { |
| 121 | + return memberRepository.findById(memberId).orElseThrow(() -> new DataNotFoundException()); |
| 122 | + } |
| 123 | + |
| 124 | + private ProjectComment getCommentById(Long commentId) { |
| 125 | + return projectCommentRepository.findById(commentId).orElseThrow(() -> new DataNotFoundException()); |
| 126 | + } |
| 127 | + |
| 128 | + private Project getProjectById(Long projectId) { |
| 129 | + return projectRepository.findById(projectId).orElseThrow(() -> new DataNotFoundException()); |
| 130 | + } |
| 131 | +} |
0 commit comments