Skip to content

Commit 799dc0c

Browse files
authored
[feat] 댓글 삭제 기능 구현#66
[feat] 댓글 삭제 기능 구현#66
2 parents e596d53 + 85e64fc commit 799dc0c

File tree

7 files changed

+86
-12
lines changed

7 files changed

+86
-12
lines changed

src/main/java/com/back/domain/post/comment/controller/CommentController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import jakarta.validation.Valid;
1313
import java.util.List;
1414
import lombok.RequiredArgsConstructor;
15+
import org.springframework.web.bind.annotation.DeleteMapping;
1516
import org.springframework.web.bind.annotation.GetMapping;
1617
import org.springframework.web.bind.annotation.PatchMapping;
1718
import org.springframework.web.bind.annotation.PathVariable;
@@ -90,4 +91,20 @@ public RsData<CommentResponseDto> updateComment(
9091
) {
9192
return RsData.successOf(commentService.updateComment(postId, commentId, reqBody)); // code=200, message="success"
9293
}
94+
95+
/**
96+
* 댓글 삭제 API
97+
* @param postId 댓글이 작성된 게시글 ID
98+
* @param commentId 삭제할 댓글 ID
99+
* @return 삭제 성공 메시지
100+
*/
101+
@DeleteMapping("/{commentId}")
102+
@Operation(summary = "댓글 삭제")
103+
public RsData<Void> deleteComment(
104+
@PathVariable Long postId,
105+
@PathVariable Long commentId
106+
) {
107+
commentService.deleteComment(postId, commentId);
108+
return RsData.successOf(null); // code=200, message="success"
109+
}
93110
}

src/main/java/com/back/domain/post/comment/dto/request/CommentCreateRequestDto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.back.domain.post.comment.dto.request;
22

3+
import com.back.domain.post.comment.enums.CommentStatus;
34
import jakarta.validation.constraints.NotBlank;
45

56
public record CommentCreateRequestDto(
7+
CommentStatus status,
68
@NotBlank (message = "내용은 필수입니다.")
79
String content
810
) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.back.domain.post.comment.dto.request;
22

3+
import com.back.domain.post.comment.enums.CommentStatus;
4+
35
public record CommentUpdateRequestDto(
6+
CommentStatus status,
47
String content
58
) {
69
}

src/main/java/com/back/domain/post/comment/dto/response/CommentResponseDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.back.domain.post.comment.dto.response;
22

33
import com.back.domain.post.comment.entity.Comment;
4+
import com.back.domain.post.comment.enums.CommentStatus;
45
import java.time.LocalDateTime;
56

67
public record CommentResponseDto(
@@ -9,6 +10,7 @@ public record CommentResponseDto(
910
String userNickName,
1011
LocalDateTime createdAt,
1112
LocalDateTime updatedAt,
13+
CommentStatus status,
1214
String content
1315
) {
1416

@@ -19,6 +21,7 @@ public CommentResponseDto(Comment comment) {
1921
comment.getUser().getNickname(),
2022
comment.getCreatedAt(),
2123
comment.getUpdatedAt(),
24+
comment.getStatus(),
2225
comment.getContent()
2326
);
2427
}

src/main/java/com/back/domain/post/comment/entity/Comment.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.back.domain.post.comment.entity;
22

3+
import com.back.domain.post.comment.enums.CommentStatus;
34
import com.back.domain.post.post.entity.Post;
5+
import com.back.domain.post.post.enums.PostStatus;
46
import com.back.domain.user.entity.User;
57
import jakarta.persistence.Column;
68
import jakarta.persistence.Entity;
79
import jakarta.persistence.EntityListeners;
10+
import jakarta.persistence.EnumType;
11+
import jakarta.persistence.Enumerated;
812
import jakarta.persistence.FetchType;
913
import jakarta.persistence.GeneratedValue;
1014
import jakarta.persistence.GenerationType;
@@ -53,10 +57,20 @@ public class Comment {
5357
@LastModifiedDate
5458
private LocalDateTime updatedAt;
5559

60+
// 댓글 게시 상태 (기본값: 공개)
61+
@Builder.Default
62+
@Enumerated(EnumType.STRING)
63+
@Column(name = "status", nullable = false)
64+
private CommentStatus status = CommentStatus.PUBLIC;
65+
5666
// 댓글 내용
5767
@Column(name = "content", nullable = false, columnDefinition = "TEXT")
5868
private String content;
5969

70+
public void updateStatus(CommentStatus status) {
71+
this.status = status;
72+
}
73+
6074
public void updateContent(String content) {
6175
this.content = content;
6276
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.back.domain.post.comment.enums;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Getter
7+
@RequiredArgsConstructor
8+
public enum CommentStatus {
9+
PUBLIC("공개", "모든 사용자가 볼 수 있는 상태"),
10+
PRIVATE("비공개", "게시글 작성자와 댓글 작성자만 볼 수 있는 상태"),
11+
DELETED("삭제됨", "삭제 처리된 댓글 상태");
12+
13+
private final String title;
14+
private final String description;
15+
}

src/main/java/com/back/domain/post/comment/service/CommentService.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.back.domain.post.comment.dto.request.CommentUpdateRequestDto;
55
import com.back.domain.post.comment.dto.response.CommentResponseDto;
66
import com.back.domain.post.comment.entity.Comment;
7+
import com.back.domain.post.comment.enums.CommentStatus;
78
import com.back.domain.post.comment.repository.CommentRepository;
89
import com.back.domain.post.post.entity.Post;
910
import com.back.domain.post.post.repository.PostRepository;
@@ -58,12 +59,7 @@ public List<CommentResponseDto> getComments(Long postId, Long lastId) {
5859
// 댓글 단건 조회 로직
5960
@Transactional(readOnly = true)
6061
public CommentResponseDto getComment(Long postId, Long commentId) {
61-
Comment comment = commentRepository.findById(commentId)
62-
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
63-
64-
if (!comment.getPost().getId().equals(postId)) {
65-
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
66-
}
62+
Comment comment = findCommentWithValidation(postId, commentId);
6763

6864
return new CommentResponseDto(comment);
6965
}
@@ -73,12 +69,7 @@ public CommentResponseDto getComment(Long postId, Long commentId) {
7369
public CommentResponseDto updateComment(Long postId, Long commentId, CommentUpdateRequestDto requestDto) {
7470
User user = rq.getActor();
7571

76-
Comment comment = commentRepository.findById(commentId)
77-
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
78-
79-
if (!comment.getPost().getId().equals(postId)) {
80-
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
81-
}
72+
Comment comment = findCommentWithValidation(postId, commentId);
8273

8374
if (!comment.getUser().equals(user)) {
8475
throw new IllegalStateException("본인의 댓글만 수정할 수 있습니다.");
@@ -87,4 +78,33 @@ public CommentResponseDto updateComment(Long postId, Long commentId, CommentUpda
8778
comment.updateContent(requestDto.content());
8879
return new CommentResponseDto(comment);
8980
}
81+
82+
// 댓글 삭제 로직
83+
@Transactional
84+
public void deleteComment(Long postId, Long commentId) {
85+
User user = rq.getActor();
86+
87+
Comment comment = findCommentWithValidation(postId, commentId);
88+
89+
if (!comment.getUser().equals(user)) {
90+
throw new IllegalStateException("본인의 댓글만 삭제할 수 있습니다.");
91+
}
92+
93+
comment.updateStatus(CommentStatus.DELETED);
94+
95+
// soft delete를 사용하기 위해 레포지토리 삭제 작업은 진행하지 않음.
96+
// commentRepository.delete(comment);
97+
}
98+
99+
// 댓글과 게시글의 연관관계 검증
100+
private Comment findCommentWithValidation(Long postId, Long commentId) {
101+
Comment comment = commentRepository.findById(commentId)
102+
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));
103+
104+
if (!comment.getPost().getId().equals(postId)) {
105+
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
106+
}
107+
108+
return comment;
109+
}
90110
}

0 commit comments

Comments
 (0)