Skip to content

Commit

Permalink
🐛 Fix: 댓글 삭제시 댓글 수 감소하게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Amepistheo committed Jul 24, 2024
1 parent 1183552 commit cfdf38b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
20 changes: 7 additions & 13 deletions src/main/java/com/ticle/server/mypage/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -98,7 +98,6 @@ public SavedTicleResponseList getSavedArticlesByCategory(CustomUserDetails custo

public List<QnAResponse> getMyQnA(Long userId, Pageable pageable) {
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException(UserErrorCode.USER_NOT_FOUND));
// Page<Opinion> opinions = opinionRepository.findByUserId(userId, pageable);
Page<Comment> comments = commentRepository.findByUser(user,pageable);
PageInfo pageInfo = PageInfo.from(comments);

Expand All @@ -113,33 +112,29 @@ public List<QnAResponse> getMyQnA(Long userId, Pageable pageable) {
}

@Transactional
public void updateComment(Long userId, Long opinionId, String newContent) {
// Optional<User> user = userRepository.findById(userId);
// Optional<Opinion> opinion = opinionRepository.findByOpinionIdWithFetch(opinionId);
// Optional<Comment> 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)) {
throw new RuntimeException("You do not have permission to edit this comment");
}

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();
}

//////////////////////////////////////////////티클노트///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -175,5 +170,4 @@ public void deleteNote(CustomUserDetails customUserDetails, Long noteId) {
}
memoRepository.delete(memo);
}

}
10 changes: 4 additions & 6 deletions src/main/java/com/ticle/server/opinion/domain/Opinion.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Comment> comments = new ArrayList<>();

Expand All @@ -42,7 +37,6 @@ public class Opinion extends BaseTimeEntity {
public Opinion(String question, Long viewCount,List<Comment> comments, Long commentCount) {
this.question = question;
this.viewCount = viewCount;
// this.user = user;
this.comments = comments;
this.commentCount = commentCount;
}
Expand All @@ -54,4 +48,8 @@ public void addViewCount() {
public void addCommentCount() {
this.commentCount++;
}

public void subCommentCount() {
this.commentCount--;
}
}

0 comments on commit cfdf38b

Please sign in to comment.