Skip to content

Commit e9d2f61

Browse files
committed
feat: 댓글 수정 기능 구현
1 parent bd517c0 commit e9d2f61

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

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

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

33
import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
4+
import com.back.domain.post.comment.dto.request.CommentUpdateRequestDto;
45
import com.back.domain.post.comment.dto.response.CommentResponseDto;
56
import com.back.domain.post.comment.service.CommentService;
7+
import com.back.domain.post.post.dto.request.PostUpdateRequestDto;
68
import com.back.domain.post.post.dto.response.PostResponseDto;
79
import com.back.global.rsData.RsData;
810
import io.swagger.v3.oas.annotations.Operation;
@@ -11,6 +13,7 @@
1113
import java.util.List;
1214
import lombok.RequiredArgsConstructor;
1315
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.PatchMapping;
1417
import org.springframework.web.bind.annotation.PathVariable;
1518
import org.springframework.web.bind.annotation.PostMapping;
1619
import org.springframework.web.bind.annotation.RequestBody;
@@ -41,6 +44,12 @@ public RsData<CommentResponseDto> createComment(
4144
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
4245
}
4346

47+
/**
48+
* 댓글 다건 조회 API
49+
* @param postId 댓글이 작성된 게시글 ID
50+
* @param lastId 마지막으로 조회한 댓글 ID (페이징 처리용, optional)
51+
* @return 댓글 목록
52+
*/
4453
@GetMapping
4554
@Operation(summary = "댓글 다건 조회")
4655
public RsData<List<CommentResponseDto>> getComments(
@@ -50,6 +59,12 @@ public RsData<List<CommentResponseDto>> getComments(
5059
return RsData.successOf(commentService.getComments(postId, lastId)); // code=200, message="success"
5160
}
5261

62+
/**
63+
* 댓글 단건 조회 API
64+
* @param postId 댓글이 작성된 게시글 ID
65+
* @param commentId 조회할 댓글 ID
66+
* @return 해당 ID의 댓글 정보
67+
*/
5368
@GetMapping("/{commentId}")
5469
@Operation(summary = "댓글 단건 조회")
5570
public RsData<CommentResponseDto> getComment(
@@ -58,4 +73,21 @@ public RsData<CommentResponseDto> getComment(
5873
) {
5974
return RsData.successOf(commentService.getComment(postId, commentId)); // code=200, message="success"
6075
}
76+
77+
/**
78+
* 댓글 수정 API
79+
* @param postId 댓글이 작성된 게시글 ID
80+
* @param commentId 수정할 댓글 ID
81+
* @param reqBody 댓글 수정 요청 DTO
82+
* @return 수정된 댓글 정보
83+
*/
84+
@PatchMapping("/{commentId}")
85+
@Operation(summary = "댓글 수정")
86+
public RsData<CommentResponseDto> updateComment(
87+
@PathVariable Long postId,
88+
@PathVariable Long commentId,
89+
@Valid @RequestBody CommentUpdateRequestDto reqBody
90+
) {
91+
return RsData.successOf(commentService.updateComment(postId, commentId, reqBody)); // code=200, message="success"
92+
}
6193
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import jakarta.validation.constraints.NotBlank;
44

55
public record CommentCreateRequestDto(
6-
Long postId,
76
@NotBlank (message = "내용은 필수입니다.")
87
String content
98
) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.back.domain.post.comment.dto.request;
2+
3+
public record CommentUpdateRequestDto(
4+
String content
5+
) {
6+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ public class Comment {
5656
// 댓글 내용
5757
@Column(name = "content", nullable = false, columnDefinition = "TEXT")
5858
private String content;
59+
60+
public void updateContent(String content) {
61+
this.content = content;
62+
}
5963
}

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

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

33
import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
4+
import com.back.domain.post.comment.dto.request.CommentUpdateRequestDto;
45
import com.back.domain.post.comment.dto.response.CommentResponseDto;
56
import com.back.domain.post.comment.entity.Comment;
67
import com.back.domain.post.comment.repository.CommentRepository;
@@ -66,4 +67,24 @@ public CommentResponseDto getComment(Long postId, Long commentId) {
6667

6768
return new CommentResponseDto(comment);
6869
}
70+
71+
// 댓글 수정 로직
72+
@Transactional
73+
public CommentResponseDto updateComment(Long postId, Long commentId, CommentUpdateRequestDto requestDto) {
74+
User user = rq.getActor();
75+
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+
}
82+
83+
if (!comment.getUser().equals(user)) {
84+
throw new IllegalStateException("본인의 댓글만 수정할 수 있습니다.");
85+
}
86+
87+
comment.updateContent(requestDto.content());
88+
return new CommentResponseDto(comment);
89+
}
6990
}

0 commit comments

Comments
 (0)