Skip to content

Commit

Permalink
Merge pull request #146 from Me1tingPot/feature/#145
Browse files Browse the repository at this point in the history
[FIX] 커뮤니티 기능 수정
  • Loading branch information
moonyaeyoon authored Sep 20, 2024
2 parents 70d1465 + d77b534 commit e7b96fe
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
25 changes: 11 additions & 14 deletions src/main/java/meltingpot/server/comment/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,22 @@ public CommentsListResponse getCommentsList(Account account, Long postId, Long c
@Transactional
public ResponseCode deleteComment(Long commentId, Account account) {
Comment comment = findCommentById(commentId);

if (!comment.getAccount().getId().equals(account.getId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "삭제 권한이 없습니다.");
}
if (comment.getParent() == null) {
comment.setContent("삭제된 댓글입니다.");
comment.setAccount(null);
comment.setIsAnonymous(true);

if (comment.getCommentImage() != null) {
CommentImage commentImage = comment.getCommentImage();
comment.setCommentImage(null);
commentImageRepository.delete(commentImage);
}
// 자식 댓글이 남아있지 않으면 부모 댓글도 삭제
deleteCommentImage(comment);
if (comment.getChildren().isEmpty()) {
commentRepository.delete(comment);
}


} else {
deleteCommentImage(comment);
Comment parentComment = comment.getParent();
commentRepository.delete(comment);
parentComment.getChildren().remove(comment);
if (parentComment.getChildren().isEmpty()) {
commentRepository.delete(parentComment);
}
commentRepository.delete(comment);
}
return ResponseCode.COMMENT_DELETE_SUCCESS;
}
Expand Down Expand Up @@ -215,6 +204,14 @@ private void updateCommentImage(Comment comment, Account account, String newImag
}
}

private void deleteCommentImage(Comment comment) {
if (comment.getCommentImage() != null) {
CommentImage commentImage = comment.getCommentImage();
comment.setCommentImage(null);
commentImageRepository.delete(commentImage);
}
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meltingpot.server.domain.entity.comment;

import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.*;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.post.Post;
Expand All @@ -20,6 +21,7 @@ public class Comment extends BaseEntity {
@Column(name = "comment_id")
private Long id;

@Size(min = 10, max = 500)
private String content;

@Column(name = "is_anonymous")
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/meltingpot/server/domain/entity/post/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import jakarta.persistence.*;
import jakarta.validation.constraints.Size;
import lombok.*;
import meltingpot.server.domain.entity.Account;
import meltingpot.server.domain.entity.Report;
Expand All @@ -28,8 +29,10 @@ public class Post extends BaseEntity {
@Column(name = "post_id")
private Long id;

@Size(min = 10, max = 500)
private String title;

@Size(min = 10, max = 500)
private String content;

@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public interface PostRepository extends JpaRepository<Post, Long> {

@Query("SELECT p FROM Post p WHERE p.postType = :postType AND p.isDraft = false AND (:cursor IS NULL OR p.id > :cursor) ORDER BY p.id ASC")
@Query("SELECT p FROM Post p WHERE p.postType = :postType AND p.isDraft = false AND (:cursor IS NULL OR p.id < :cursor) ORDER BY p.id DESC")
List<Post> findByPostTypeAndCursor(@Param("postType") PostType postType, @Param("cursor") Long cursor, Pageable pageable);

Slice<Post> findAllByAccountAndDeletedAtIsNullOrderByIdDesc(Account account, Pageable page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class PostService {
public ResponseCode createPost(PostCreateRequest postCreateRequest, Account account, boolean isDraft) {
Optional<Post> optionalDraft = getDraftPost(account);
Post post = optionalDraft.orElseGet(() -> postCreateRequest.toEntity(account));
System.out.println("Post " + post.getId() + post.getTitle() + post.getIsDraft());

if (optionalDraft.isPresent()) {
updatePostContent(post, account, postCreateRequest);
Expand Down

0 comments on commit e7b96fe

Please sign in to comment.