From cfdf38b1d5c64f91cfabd7177ab37e98b6a24009 Mon Sep 17 00:00:00 2001 From: Amepistheo Date: Wed, 24 Jul 2024 15:53:43 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20Fix:=20=EB=8C=93=EA=B8=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=EC=8B=9C=20=EB=8C=93=EA=B8=80=20=EC=88=98=20=EA=B0=90?= =?UTF-8?q?=EC=86=8C=ED=95=98=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/mypage/service/MyPageService.java | 20 +++++++------------ .../ticle/server/opinion/domain/Opinion.java | 10 ++++------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/ticle/server/mypage/service/MyPageService.java b/src/main/java/com/ticle/server/mypage/service/MyPageService.java index f2deb13..fdfb115 100644 --- a/src/main/java/com/ticle/server/mypage/service/MyPageService.java +++ b/src/main/java/com/ticle/server/mypage/service/MyPageService.java @@ -9,12 +9,13 @@ import com.ticle.server.mypage.dto.response.SavedTicleResponseList; import com.ticle.server.opinion.domain.Comment; import com.ticle.server.opinion.domain.Opinion; +import com.ticle.server.opinion.exception.OpinionNotFoundException; import com.ticle.server.opinion.repository.CommentRepository; import com.ticle.server.opinion.repository.OpinionRepository; import com.ticle.server.post.repository.MemoRepository; import com.ticle.server.scrapped.domain.Scrapped; -import com.ticle.server.user.domain.User; import com.ticle.server.scrapped.repository.ScrappedRepository; +import com.ticle.server.user.domain.User; import com.ticle.server.user.domain.type.Category; import com.ticle.server.user.exception.UserNotFoundException; import com.ticle.server.user.exception.errorcode.UserErrorCode; @@ -30,12 +31,11 @@ import java.time.format.DateTimeFormatter; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; import static com.ticle.server.opinion.domain.type.Order.TIME; import static com.ticle.server.opinion.domain.type.Order.getOrder; -import static java.util.stream.Collectors.toList; +import static com.ticle.server.opinion.exception.errorcode.OpinionErrorCode.OPINION_NOT_FOUND; @Service @RequiredArgsConstructor @@ -98,7 +98,6 @@ public SavedTicleResponseList getSavedArticlesByCategory(CustomUserDetails custo public List getMyQnA(Long userId, Pageable pageable) { User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException(UserErrorCode.USER_NOT_FOUND)); -// Page opinions = opinionRepository.findByUserId(userId, pageable); Page comments = commentRepository.findByUser(user,pageable); PageInfo pageInfo = PageInfo.from(comments); @@ -113,12 +112,7 @@ public List getMyQnA(Long userId, Pageable pageable) { } @Transactional - public void updateComment(Long userId, Long opinionId, String newContent) { -// Optional user = userRepository.findById(userId); -// Optional opinion = opinionRepository.findByOpinionIdWithFetch(opinionId); -// Optional comment = commentRepository.findByUserIdAndOpinionId(userId,opinionId); - - Comment comment = commentRepository.findByUserIdAndOpinionId(userId,opinionId) + public void updateComment(Long userId, Long opinionId, String newContent) {Comment comment = commentRepository.findByUserIdAndOpinionId(userId,opinionId) .orElseThrow(() -> new RuntimeException("Comment not found")); if (!comment.getUser().getId().equals(userId)) { @@ -126,20 +120,21 @@ public void updateComment(Long userId, Long opinionId, String newContent) { } comment.updateContent(newContent); -// return commentRepository.save(comment); } @Transactional public void deleteComment(Long userId, Long questionId) { Comment comment = commentRepository.findByUserIdAndOpinionId(userId, questionId) .orElseThrow(() -> new RuntimeException("Comment not found")); + Opinion opinion = opinionRepository.findById(questionId) + .orElseThrow(() -> new OpinionNotFoundException(OPINION_NOT_FOUND)); if (!comment.getUser().getId().equals(userId)) { throw new RuntimeException("You do not have permission to delete this comment"); } commentRepository.delete(comment); - comment.subHeartCount(); + opinion.subCommentCount(); } //////////////////////////////////////////////티클노트/////////////////////////////////////////////////////////////// @@ -175,5 +170,4 @@ public void deleteNote(CustomUserDetails customUserDetails, Long noteId) { } memoRepository.delete(memo); } - } diff --git a/src/main/java/com/ticle/server/opinion/domain/Opinion.java b/src/main/java/com/ticle/server/opinion/domain/Opinion.java index afb3d3b..ba0310a 100644 --- a/src/main/java/com/ticle/server/opinion/domain/Opinion.java +++ b/src/main/java/com/ticle/server/opinion/domain/Opinion.java @@ -1,7 +1,6 @@ package com.ticle.server.opinion.domain; import com.ticle.server.global.domain.BaseTimeEntity; -import com.ticle.server.user.domain.User; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; @@ -28,10 +27,6 @@ public class Opinion extends BaseTimeEntity { @Column(name = "view_count") private Long viewCount; -// @JoinColumn(name = "user_id") -// @ManyToOne(fetch = FetchType.LAZY) -// private User user; - @OneToMany(mappedBy = "opinion", cascade = CascadeType.REMOVE, orphanRemoval = true) private List comments = new ArrayList<>(); @@ -42,7 +37,6 @@ public class Opinion extends BaseTimeEntity { public Opinion(String question, Long viewCount,List comments, Long commentCount) { this.question = question; this.viewCount = viewCount; -// this.user = user; this.comments = comments; this.commentCount = commentCount; } @@ -54,4 +48,8 @@ public void addViewCount() { public void addCommentCount() { this.commentCount++; } + + public void subCommentCount() { + this.commentCount--; + } }