Skip to content

Commit

Permalink
Merge pull request #48 from Leets-Official/#47/feat/댓글삭제시-댓글수-다시계산
Browse files Browse the repository at this point in the history
#48 Feat: 댓글삭제시 댓글수 다시계산
  • Loading branch information
hyxklee authored Aug 21, 2024
2 parents d3182c6 + c39f30b commit d5615b0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public List<NoticeDTO.ResponseAll> findNotices(Long noticeId, Integer count) {

Long finalNoticeId = noticeFindService.findFinalNoticeId();

if(noticeId==null){ // 첫번째 요청인 경우
// 첫번째 요청인 경우
if(noticeId==null){
noticeId = finalNoticeId + 1;
}

// postId가 1 이하이거나 최대값보다 클경우
if(noticeId < 1 || noticeId > finalNoticeId + 1){
throw new NoticeNotFoundException(); // postId가 1 이하이거나 최대값보다 클경우
throw new NoticeNotFoundException();
}

Pageable pageable = PageRequest.of(0, count); // 첫 페이지, 페이지당 15개 게시글
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package leets.weeth.domain.board.application.usecase;

import leets.weeth.domain.board.application.dto.NoticeDTO;
import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.board.application.mapper.PostMapper;
import leets.weeth.domain.board.domain.entity.Notice;
import leets.weeth.domain.board.domain.entity.Post;
import leets.weeth.domain.board.domain.service.PostDeleteService;
import leets.weeth.domain.board.domain.service.PostFindService;
Expand Down Expand Up @@ -56,11 +54,14 @@ public List<PostDTO.ResponseAll> findPosts(Long postId, Integer count) {

Long finalPostId = postFindService.findFinalPostId();

if(postId==null){ // 첫번째 요청인 경우
// 첫번째 요청인 경우
if(postId==null){
postId = finalPostId + 1;
}

// postId가 1 이하이거나 최대값보다 클경우
if(postId < 1 || postId > finalPostId + 1){
throw new PostNotFoundException(); // postId가 1 이하이거나 최대값보다 클경우
throw new PostNotFoundException();
}

Pageable pageable = PageRequest.of(0, count); // 첫 페이지, 페이지당 15개 게시글
Expand All @@ -72,11 +73,6 @@ public List<PostDTO.ResponseAll> findPosts(Long postId, Integer count) {
.toList();
}

/*
게시글 수정 시 파일이 넘어오지 않으면, 파일이 제거됨.
수정할 때도 파일을 같이 넘기면, 또 업로드가 발생함 -> 근데 동일한 파일은 재 업로드가 아닌거 같기도 함.
-> 테스트 해보기
*/
@Override
public void update(Long postId, PostDTO.Update dto, List<MultipartFile> files, Long userId) throws UserNotMatchException {
Post post = validateOwner(postId, userId);
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/leets/weeth/domain/board/domain/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import jakarta.persistence.*;
import leets.weeth.domain.board.application.dto.NoticeDTO;
import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.comment.domain.entity.Comment;
import leets.weeth.domain.file.converter.FileListConverter;
import leets.weeth.domain.user.domain.entity.User;
import leets.weeth.global.common.entity.BaseEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.ArrayList;
Expand All @@ -20,6 +22,7 @@
@EntityListeners(AuditingEntityListener.class)
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Slf4j
public class Board extends BaseEntity {

@Id
Expand All @@ -31,39 +34,43 @@ public class Board extends BaseEntity {
@Column(columnDefinition = "TEXT")
private String content;

private Integer commentCount;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@Convert(converter = FileListConverter.class)
private List<String> fileUrls = new ArrayList<>();

private Integer commentCount;

@PrePersist
public void prePersist() {
if (commentCount == null) {
commentCount = 0;
}
commentCount = 0;
}

public void incrementCommentCount(){
public void increaseCommentCount() {
commentCount++;
}

public void decrementCommentCount(){
if(this.commentCount > 0){
public void decreaseCommentCount() {
if(commentCount > 0) {
commentCount--;
}
}

public void updateUpperClass(NoticeDTO.Update dto, List<String> fileUrls){
public void updateCommentCount(List<Comment> comments) {
this.commentCount = (int) comments.stream()
.filter(comment -> !comment.getIsDeleted())
.count();
}

public void updateUpperClass(NoticeDTO.Update dto, List<String> fileUrls) {
this.title = dto.title();
this.content = dto.content();
this.fileUrls = fileUrls;
}

public void updateUpperClass(PostDTO.Update dto, List<String> fileUrls){
public void updateUpperClass(PostDTO.Update dto, List<String> fileUrls) {
this.title = dto.title();
this.content = dto.content();
this.fileUrls = fileUrls;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/leets/weeth/domain/board/domain/entity/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.PreUpdate;
import leets.weeth.domain.board.application.dto.NoticeDTO;
import leets.weeth.domain.comment.domain.entity.Comment;
import lombok.AccessLevel;
Expand All @@ -22,8 +23,13 @@ public class Notice extends Board {
@JsonManagedReference
private List<Comment> comments;

public void addComment(Comment newComment) {
this.comments.add(newComment);
@PreUpdate
public void updateCommentCount() {
this.updateCommentCount(this.comments);
}

public void addComment(Comment comment) {
comments.add(comment);
}

public void update(NoticeDTO.Update dto, List<String> fileUrls){
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/leets/weeth/domain/board/domain/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.PreUpdate;
import leets.weeth.domain.board.application.dto.PostDTO;
import leets.weeth.domain.comment.domain.entity.Comment;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
Expand All @@ -23,9 +23,13 @@ public class Post extends Board {
@JsonManagedReference
private List<Comment> comments;

@PreUpdate
public void updateCommentCount() {
this.updateCommentCount(this.comments);
}

public void addComment(Comment newComment) {
this.comments.add(newComment);
public void addComment(Comment comment) {
comments.add(comment);
}

public void update(PostDTO.Update dto, List<String> fileUrls) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import leets.weeth.domain.user.domain.service.UserGetService;
import leets.weeth.global.common.error.exception.custom.UserNotMatchException;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -30,7 +31,6 @@ public class NoticeCommentUsecaseImpl implements NoticeCommentUsecase {
private final UserGetService userGetService;
private final CommentMapper commentMapper;


@Override
@Transactional
public void saveNoticeComment(CommentDTO.Save dto, Long noticeId, Long userId) {
Expand All @@ -45,16 +45,15 @@ public void saveNoticeComment(CommentDTO.Save dto, Long noticeId, Long userId) {
commentSaveService.save(comment);

// 부모 댓글이 없다면 새 댓글로 추가
if(parentComment == null)
if(parentComment == null) {
notice.addComment(comment);
else
} else {
// 부모 댓글이 있다면 자녀 댓글로 추가
parentComment.addChild(comment);

notice.incrementCommentCount();
}
notice.increaseCommentCount();
}


@Override
@Transactional
public void updateNoticeComment(CommentDTO.Update dto, Long noticeId, Long commentId, Long userId) throws UserNotMatchException {
Expand All @@ -72,30 +71,21 @@ public void deleteNoticeComment(Long commentId, Long userId) throws UserNotMatch
Comment comment = validateOwner(commentId, userId);
Notice notice = comment.getNotice();

/*
1. 지우고자 하는 댓글이 맨 아래층인 경우(child, child가 없는 댓글
- 현재 댓글.getChildren이 NULL 이면 해당
- 내가 child인지 child가 없는 댓글인지 구분해야함
- child인 경우 -> 부모가 있음. 하지만 부모를 삭제하는게 아니라 나만 삭제함, 부모의 childern에서 나를 제거해야함
- child가 없는 댓글인 경우 -> 자식이 없기 떄문에 나만 삭제함
*/
// 현재 삭제하고자 하는 댓글이 자식이 없는 경우
if(comment.getChildren().isEmpty()){
if (comment.getChildren().isEmpty()) {
Comment parentComment = findParentComment(commentId);
commentDeleteService.delete(commentId);
if(parentComment != null){
if (parentComment != null) {
parentComment.getChildren().remove(comment);
if(parentComment.getIsDeleted() && parentComment.getChildren().isEmpty()){
if (parentComment.getIsDeleted() && parentComment.getChildren().isEmpty()) {
notice.getComments().remove(parentComment);
commentDeleteService.delete(parentComment.getId());
}
}
} else{
} else {
comment.markAsDeleted();
commentSaveService.save(comment);
}
notice.decrementCommentCount();

notice.decreaseCommentCount();
}

private Comment findParentComment(Long commentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public void savePostComment(CommentDTO.Save dto, Long postId, Long userId) {
commentSaveService.save(comment);

// 부모 댓글이 없다면 새 댓글로 추가
if(parentComment == null)
if(parentComment == null) {
post.addComment(comment);
else
} else {
// 부모 댓글이 있다면 자녀 댓글로 추가
parentComment.addChild(comment);

post.incrementCommentCount();
}
post.increaseCommentCount();
}

@Override
Expand Down Expand Up @@ -94,9 +94,7 @@ public void deletePostComment(Long commentId, Long userId) throws UserNotMatchEx
comment.markAsDeleted();
commentSaveService.save(comment);
}

post.decrementCommentCount();

post.decreaseCommentCount();
}

private Comment findParentComment(Long commentId) {
Expand Down

0 comments on commit d5615b0

Please sign in to comment.