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
@@ -1,8 +1,10 @@
package com.back.domain.post.comment.controller;

import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
import com.back.domain.post.comment.dto.request.CommentUpdateRequestDto;
import com.back.domain.post.comment.dto.response.CommentResponseDto;
import com.back.domain.post.comment.service.CommentService;
import com.back.domain.post.post.dto.request.PostUpdateRequestDto;
import com.back.domain.post.post.dto.response.PostResponseDto;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -11,6 +13,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -41,6 +44,12 @@ public RsData<CommentResponseDto> createComment(
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
}

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

/**
* 댓글 단건 조회 API
* @param postId 댓글이 작성된 게시글 ID
* @param commentId 조회할 댓글 ID
* @return 해당 ID의 댓글 정보
*/
@GetMapping("/{commentId}")
@Operation(summary = "댓글 단건 조회")
public RsData<CommentResponseDto> getComment(
Expand All @@ -58,4 +73,21 @@ public RsData<CommentResponseDto> getComment(
) {
return RsData.successOf(commentService.getComment(postId, commentId)); // code=200, message="success"
}

/**
* 댓글 수정 API
* @param postId 댓글이 작성된 게시글 ID
* @param commentId 수정할 댓글 ID
* @param reqBody 댓글 수정 요청 DTO
* @return 수정된 댓글 정보
*/
@PatchMapping("/{commentId}")
@Operation(summary = "댓글 수정")
public RsData<CommentResponseDto> updateComment(
@PathVariable Long postId,
@PathVariable Long commentId,
@Valid @RequestBody CommentUpdateRequestDto reqBody
) {
return RsData.successOf(commentService.updateComment(postId, commentId, reqBody)); // code=200, message="success"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.validation.constraints.NotBlank;

public record CommentCreateRequestDto(
Long postId,
@NotBlank (message = "내용은 필수입니다.")
String content
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.back.domain.post.comment.dto.request;

public record CommentUpdateRequestDto(
String content
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public class Comment {
// 댓글 내용
@Column(name = "content", nullable = false, columnDefinition = "TEXT")
private String content;

public void updateContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.back.domain.post.comment.service;

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

return new CommentResponseDto(comment);
}

// 댓글 수정 로직
@Transactional
public CommentResponseDto updateComment(Long postId, Long commentId, CommentUpdateRequestDto requestDto) {
User user = rq.getActor();

Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new IllegalArgumentException("댓글이 존재하지 않습니다. id=" + commentId));

if (!comment.getPost().getId().equals(postId)) {
throw new IllegalStateException("댓글이 해당 게시글에 속하지 않습니다.");
}

if (!comment.getUser().equals(user)) {
throw new IllegalStateException("본인의 댓글만 수정할 수 있습니다.");
}

comment.updateContent(requestDto.content());
return new CommentResponseDto(comment);
}
}