File tree Expand file tree Collapse file tree 4 files changed +90
-0
lines changed
src/main/java/com/back/domain/post/comment Expand file tree Collapse file tree 4 files changed +90
-0
lines changed Original file line number Diff line number Diff line change 11package 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 ;
38import io .swagger .v3 .oas .annotations .tags .Tag ;
9+ import jakarta .validation .Valid ;
410import 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 ;
514import org .springframework .web .bind .annotation .RequestMapping ;
615import org .springframework .web .bind .annotation .RestController ;
716
1120@ RequiredArgsConstructor
1221public 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package 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 ;
311import lombok .RequiredArgsConstructor ;
412import org .springframework .stereotype .Service ;
13+ import org .springframework .transaction .annotation .Transactional ;
514
615@ Service
716@ RequiredArgsConstructor
817public 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}
You can’t perform that action at this time.
0 commit comments