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

import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
import com.back.domain.post.comment.dto.response.CommentResponseDto;
import com.back.domain.post.comment.service.CommentService;
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -11,4 +20,21 @@
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

/**
* 댓글 작성 API
* @param postId 댓글을 작성할 게시글 ID
* @param reqBody 댓글 작성 요청 DTO
* @return 작성된 댓글 정보
*/
@PostMapping
@Operation(summary = "댓글 작성")
public RsData<CommentResponseDto> createComment(
@PathVariable Long postId,
@Valid @RequestBody CommentCreateRequestDto reqBody
) {
return RsData.successOf(commentService.createComment(postId, reqBody)); // code=200, message="success"
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.back.domain.post.comment.dto.request;

import jakarta.validation.constraints.NotBlank;

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

import com.back.domain.post.comment.entity.Comment;
import java.time.LocalDateTime;

public record CommentResponseDto(
Long commentId,
Long postId,
String userNickName,
LocalDateTime createdAt,
LocalDateTime updatedAt,
String content
) {

public CommentResponseDto(Comment comment) {
this(
comment.getId(),
comment.getPost().getId(),
comment.getUser().getNickname(),
comment.getCreatedAt(),
comment.getUpdatedAt(),
comment.getContent()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
package com.back.domain.post.comment.service;

import com.back.domain.post.comment.dto.request.CommentCreateRequestDto;
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;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.repository.PostRepository;
import com.back.domain.user.entity.User;
import com.back.global.rq.Rq;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final Rq rq;

// 댓글 작성 로직
@Transactional
public CommentResponseDto createComment(Long postId, CommentCreateRequestDto reqBody) {
User user = rq.getActor();

Post post = postRepository.findById(postId)
.orElseThrow(() -> new IllegalArgumentException("게시글이 존재하지 않습니다. id=" + postId));

Comment comment = Comment.builder()
.post(post)
.user(user)
.content(reqBody.content())
.build();

return new CommentResponseDto(commentRepository.save(comment));
}
}