Skip to content

Commit b3c0e2e

Browse files
authored
[feat] 댓글 작성 기능 구현#62
[feat] 댓글 작성 기능 구현#62
2 parents 2191e71 + 2deb0f3 commit b3c0e2e

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

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

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

3+
import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
4+
import com.back.domain.post.comment.dto.response.CommentResponseDto;
5+
import com.back.domain.post.comment.service.CommentService;
6+
import com.back.global.rsData.RsData;
7+
import io.swagger.v3.oas.annotations.Operation;
38
import io.swagger.v3.oas.annotations.tags.Tag;
9+
import jakarta.validation.Valid;
410
import lombok.RequiredArgsConstructor;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
514
import org.springframework.web.bind.annotation.RequestMapping;
615
import org.springframework.web.bind.annotation.RestController;
716

@@ -11,4 +20,21 @@
1120
@RequiredArgsConstructor
1221
public class CommentController {
1322

23+
private final CommentService commentService;
24+
25+
/**
26+
* 댓글 작성 API
27+
* @param postId 댓글을 작성할 게시글 ID
28+
* @param reqBody 댓글 작성 요청 DTO
29+
* @return 작성된 댓글 정보
30+
*/
31+
@PostMapping
32+
@Operation(summary = "댓글 작성")
33+
public RsData<CommentResponseDto> createComment(
34+
@PathVariable Long postId,
35+
@Valid @RequestBody CommentCreateRequestDto reqBody
36+
) {
37+
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
38+
}
39+
1440
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.back.domain.post.comment.dto.request;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
5+
public record CommentCreateRequestDto(
6+
Long postId,
7+
@NotBlank (message = "내용은 필수입니다.")
8+
String content
9+
) {
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.back.domain.post.comment.dto.response;
2+
3+
import com.back.domain.post.comment.entity.Comment;
4+
import java.time.LocalDateTime;
5+
6+
public record CommentResponseDto(
7+
Long commentId,
8+
Long postId,
9+
String userNickName,
10+
LocalDateTime createdAt,
11+
LocalDateTime updatedAt,
12+
String content
13+
) {
14+
15+
public CommentResponseDto(Comment comment) {
16+
this(
17+
comment.getId(),
18+
comment.getPost().getId(),
19+
comment.getUser().getNickname(),
20+
comment.getCreatedAt(),
21+
comment.getUpdatedAt(),
22+
comment.getContent()
23+
);
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
package com.back.domain.post.comment.service;
22

3+
import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
4+
import com.back.domain.post.comment.dto.response.CommentResponseDto;
5+
import com.back.domain.post.comment.entity.Comment;
6+
import com.back.domain.post.comment.repository.CommentRepository;
7+
import com.back.domain.post.post.entity.Post;
8+
import com.back.domain.post.post.repository.PostRepository;
9+
import com.back.domain.user.entity.User;
10+
import com.back.global.rq.Rq;
311
import lombok.RequiredArgsConstructor;
412
import org.springframework.stereotype.Service;
13+
import org.springframework.transaction.annotation.Transactional;
514

615
@Service
716
@RequiredArgsConstructor
817
public class CommentService {
918

19+
private final CommentRepository commentRepository;
20+
private final PostRepository postRepository;
21+
private final Rq rq;
22+
23+
// 댓글 작성 로직
24+
@Transactional
25+
public CommentResponseDto createComment(Long postId, CommentCreateRequestDto reqBody) {
26+
User user = rq.getActor();
27+
28+
Post post = postRepository.findById(postId)
29+
.orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다. id=" + postId));
30+
31+
Comment comment = Comment.builder()
32+
.post(post)
33+
.user(user)
34+
.content(reqBody.content())
35+
.build();
36+
37+
return new CommentResponseDto(commentRepository.save(comment));
38+
}
1039
}

0 commit comments

Comments
 (0)