Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.example.solidconnection.comment.domain.Comment;
import com.example.solidconnection.siteuser.dto.PostFindSiteUserResponse;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;

public record PostFindCommentResponse(
Long id,
Long parentId,
String content,
Boolean isOwner,
LocalDateTime createdAt,
LocalDateTime updatedAt,
ZonedDateTime createdAt,
ZonedDateTime updatedAt,
PostFindSiteUserResponse postFindSiteUserResponse

) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.solidconnection.custom.exception.ErrorCode.CAN_NOT_UPDATE_DEPRECATED_COMMENT;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_POST_ACCESS;
import static com.example.solidconnection.custom.exception.ErrorCode.*;

@Service
@RequiredArgsConstructor
Expand All @@ -43,6 +41,13 @@ private void validateDeprecated(Comment comment) {
}
}

// 대대댓글부터 허용하지 않음
private void validateCommentDepth(Comment parentComment) {
if (parentComment.getParentComment() != null) {
throw new CustomException(INVALID_COMMENT_LEVEL);
}
}

@Transactional(readOnly = true)
public List<PostFindCommentResponse> findCommentsByPostId(String email, Long postId) {
return commentRepository.findCommentTreeByPostId(postId)
Expand All @@ -60,6 +65,7 @@ public CommentCreateResponse createComment(String email, Long postId, CommentCre
Comment parentComment = null;
if (commentCreateRequest.parentId() != null) {
parentComment = commentRepository.getById(commentCreateRequest.parentId());
validateCommentDepth(parentComment);
}
Comment createdComment = commentRepository.save(commentCreateRequest.toEntity(siteUser, post, parentComment));

Expand Down Expand Up @@ -87,15 +93,28 @@ public CommentDeleteResponse deleteCommentById(String email, Long postId, Long c
Comment comment = commentRepository.getById(commentId);
validateOwnership(comment, email);

if (comment.getCommentList().isEmpty()) {
// 하위 댓글이 없다면 삭제한다.
if (comment.getParentComment() != null) {
// 대댓글인 경우
Comment parentComment = comment.getParentComment();
// 대댓글을 삭제합니다.
comment.resetPostAndSiteUserAndParentComment();
commentRepository.deleteById(commentId);
// 대댓글 삭제 이후, 부모댓글이 무의미하다면 이역시 삭제합니다.
if (parentComment.getCommentList().isEmpty() && parentComment.getContent() == null) {
parentComment.resetPostAndSiteUserAndParentComment();
commentRepository.deleteById(parentComment.getId());
}
} else {
// 하위 댓글 있으면 value만 null로 수정한다.
comment.deprecateComment();
// 댓글인 경우
if (comment.getCommentList().isEmpty()) {
// 대댓글이 없는 경우
comment.resetPostAndSiteUserAndParentComment();
commentRepository.deleteById(commentId);
} else {
// 대댓글이 있는 경우
comment.deprecateComment();
}
}

return new CommentDeleteResponse(commentId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum ErrorCode {
CAN_NOT_DELETE_OR_UPDATE_QUESTION(HttpStatus.BAD_REQUEST.value(), "질문글은 수정이나 삭제할 수 없습니다."),
CAN_NOT_UPLOAD_MORE_THAN_FIVE_IMAGES(HttpStatus.BAD_REQUEST.value(), "5개 이상의 파일을 업로드할 수 없습니다."),
INVALID_COMMENT_ID(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 댓글입니다."),
INVALID_COMMENT_LEVEL(HttpStatus.BAD_REQUEST.value(), "최대 대댓글까지만 작성할 수 있습니다."),
INVALID_COMMENT_ACCESS(HttpStatus.BAD_REQUEST.value(), "자신의 댓글만 제어할 수 있습니다."),
CAN_NOT_UPDATE_DEPRECATED_COMMENT(HttpStatus.BAD_REQUEST.value(),"이미 삭제된 댓글을 수정할 수 없습니다."),
INVALID_POST_LIKE(HttpStatus.BAD_REQUEST.value(), "존재하지 않는 게시글 좋아요입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import lombok.Getter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
Expand All @@ -18,9 +19,17 @@
@DynamicInsert
public abstract class BaseEntity {

@CreatedDate
private LocalDateTime createdAt;
private ZonedDateTime createdAt;
private ZonedDateTime updatedAt;

@LastModifiedDate
private LocalDateTime updatedAt;
@PrePersist
public void onPrePersist() {
this.createdAt = ZonedDateTime.now(ZoneId.of("UTC"));
this.updatedAt = this.createdAt;
}

@PreUpdate
public void onPreUpdate() {
this.updatedAt = ZonedDateTime.now(ZoneId.of("UTC"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.example.solidconnection.entity.PostImage;
import com.example.solidconnection.post.domain.Post;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -13,8 +13,8 @@ public record BoardFindPostResponse(
String content,
Long likeCount,
Integer commentCount,
LocalDateTime createdAt,
LocalDateTime updatedAt,
ZonedDateTime createdAt,
ZonedDateTime updatedAt,
String postCategory,
String url
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.example.solidconnection.post.domain.Post;
import com.example.solidconnection.siteuser.dto.PostFindSiteUserResponse;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;

public record PostFindResponse(
Expand All @@ -20,8 +20,8 @@ public record PostFindResponse(
String postCategory,
Boolean isOwner,
Boolean isLiked,
LocalDateTime createdAt,
LocalDateTime updatedAt,
ZonedDateTime createdAt,
ZonedDateTime updatedAt,
PostFindBoardResponse postFindBoardResponse,
PostFindSiteUserResponse postFindSiteUserResponse,
List<PostFindCommentResponse> postFindCommentResponses,
Expand Down
Loading