diff --git a/backend/src/main/java/com/project/capstone/comment/controller/CommentController.java b/backend/src/main/java/com/project/capstone/comment/controller/CommentController.java new file mode 100644 index 0000000000..70571da939 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/controller/CommentController.java @@ -0,0 +1,33 @@ +package com.project.capstone.comment.controller; + +import com.project.capstone.auth.domain.PrincipalDetails; +import com.project.capstone.comment.controller.dto.CommentResponse; +import com.project.capstone.comment.controller.dto.CreateCommentRequest; +import com.project.capstone.comment.service.CommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/comment") +public class CommentController { + + private final CommentService commentService; + + @PostMapping("create") + public ResponseEntity createComment(@AuthenticationPrincipal PrincipalDetails details, + @RequestParam Long postId, @RequestBody CreateCommentRequest request) { + commentService.createPost(details.getUserId(), postId, request); + return ResponseEntity.ok().body("댓글 생성 완료"); + } + + @GetMapping("/{id}") + public ResponseEntity getComment(@AuthenticationPrincipal PrincipalDetails details, + @PathVariable Long id) { + CommentResponse commentResponse = commentService.getComment(details.getUserId(), id); + return ResponseEntity.ok().body(commentResponse); + } + +} diff --git a/backend/src/main/java/com/project/capstone/comment/controller/dto/CommentResponse.java b/backend/src/main/java/com/project/capstone/comment/controller/dto/CommentResponse.java new file mode 100644 index 0000000000..fb8d6d3e19 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/controller/dto/CommentResponse.java @@ -0,0 +1,18 @@ +package com.project.capstone.comment.controller.dto; + +import com.project.capstone.comment.domain.Comment; + +import java.time.LocalDateTime; +import java.util.UUID; + +public record CommentResponse( + Long id, + Long postId, + UUID memberId, + String body, + LocalDateTime createdAt +) { + public CommentResponse(Comment comment) { + this(comment.getId(), comment.getPost().getId(), comment.getMember().getId(), comment.getBody(), comment.getCreatedAt()); + } +} diff --git a/backend/src/main/java/com/project/capstone/comment/controller/dto/CreateCommentRequest.java b/backend/src/main/java/com/project/capstone/comment/controller/dto/CreateCommentRequest.java new file mode 100644 index 0000000000..6dadaa4ca5 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/controller/dto/CreateCommentRequest.java @@ -0,0 +1,6 @@ +package com.project.capstone.comment.controller.dto; + +public record CreateCommentRequest ( + String body +) { +} diff --git a/backend/src/main/java/com/project/capstone/comment/domain/Comment.java b/backend/src/main/java/com/project/capstone/comment/domain/Comment.java index 26b1884f41..0ccb661e8c 100644 --- a/backend/src/main/java/com/project/capstone/comment/domain/Comment.java +++ b/backend/src/main/java/com/project/capstone/comment/domain/Comment.java @@ -1,5 +1,6 @@ package com.project.capstone.comment.domain; +import com.fasterxml.jackson.annotation.JsonBackReference; import com.project.capstone.member.domain.Member; import com.project.capstone.post.domain.Post; import jakarta.persistence.*; @@ -27,9 +28,11 @@ public class Comment { @Column(name = "created_at") private LocalDateTime createdAt; + @JsonBackReference @ManyToOne private Member member; + @JsonBackReference @ManyToOne private Post post; } diff --git a/backend/src/main/java/com/project/capstone/comment/domain/CommentRepository.java b/backend/src/main/java/com/project/capstone/comment/domain/CommentRepository.java new file mode 100644 index 0000000000..a5e39f660c --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/domain/CommentRepository.java @@ -0,0 +1,9 @@ +package com.project.capstone.comment.domain; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface CommentRepository extends JpaRepository { + Optional findCommentById(Long id); +} diff --git a/backend/src/main/java/com/project/capstone/comment/exception/CommentException.java b/backend/src/main/java/com/project/capstone/comment/exception/CommentException.java new file mode 100644 index 0000000000..3803fe7794 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/exception/CommentException.java @@ -0,0 +1,10 @@ +package com.project.capstone.comment.exception; + +import com.project.capstone.common.exception.BaseException; +import com.project.capstone.common.exception.ExceptionType; + +public class CommentException extends BaseException { + public CommentException(ExceptionType exceptionType) { + super(exceptionType); + } +} diff --git a/backend/src/main/java/com/project/capstone/comment/exception/CommentExceptionType.java b/backend/src/main/java/com/project/capstone/comment/exception/CommentExceptionType.java new file mode 100644 index 0000000000..c31291e063 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/exception/CommentExceptionType.java @@ -0,0 +1,32 @@ +package com.project.capstone.comment.exception; + +import com.project.capstone.common.exception.ExceptionType; +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; + +import static org.springframework.http.HttpStatus.NOT_FOUND; + +@AllArgsConstructor +public enum CommentExceptionType implements ExceptionType { + COMMENT_NOT_FOUND(NOT_FOUND, 501, "해당 댓글을 찾을 수 없습니다."); + ; + + private final HttpStatus status; + private final int exceptionCode; + private final String message; + + @Override + public HttpStatus httpStatus() { + return status; + } + + @Override + public int exceptionCode() { + return exceptionCode; + } + + @Override + public String message() { + return message; + } +} diff --git a/backend/src/main/java/com/project/capstone/comment/service/CommentService.java b/backend/src/main/java/com/project/capstone/comment/service/CommentService.java new file mode 100644 index 0000000000..5e5fa7c47f --- /dev/null +++ b/backend/src/main/java/com/project/capstone/comment/service/CommentService.java @@ -0,0 +1,62 @@ +package com.project.capstone.comment.service; + +import com.project.capstone.comment.controller.dto.CommentResponse; +import com.project.capstone.comment.controller.dto.CreateCommentRequest; +import com.project.capstone.comment.domain.Comment; +import com.project.capstone.comment.domain.CommentRepository; +import com.project.capstone.comment.exception.CommentException; +import com.project.capstone.member.domain.Member; +import com.project.capstone.member.domain.MemberRepository; +import com.project.capstone.member.exception.MemberException; +import com.project.capstone.post.domain.Post; +import com.project.capstone.post.domain.PostRepository; +import com.project.capstone.post.exception.PostException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.UUID; + +import static com.project.capstone.comment.exception.CommentExceptionType.COMMENT_NOT_FOUND; +import static com.project.capstone.member.exception.MemberExceptionType.MEMBER_NOT_FOUND; +import static com.project.capstone.post.exception.PostExceptionType.POST_NOT_FOUND; + +@Service +@RequiredArgsConstructor +@Slf4j +public class CommentService { + + private final CommentRepository commentRepository; + private final MemberRepository memberRepository; + private final PostRepository postRepository; + + public void createPost(String userId, Long postId, CreateCommentRequest request) { + Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow( + () -> new MemberException(MEMBER_NOT_FOUND) + ); + + Post post = postRepository.findPostById(postId).orElseThrow( + () -> new PostException(POST_NOT_FOUND) + ); + + Comment saved = commentRepository.save(Comment.builder() + .body(request.body()) + .member(member) + .post(post) + .build()); + + member.getComments().add(saved); + post.getComments().add(saved); + } + + + public CommentResponse getComment(String userId, Long id) { + if (memberRepository.findMemberById(UUID.fromString(userId)).isEmpty()) { + throw new MemberException(MEMBER_NOT_FOUND); + } + Comment comment = commentRepository.findCommentById(id).orElseThrow( + () -> new CommentException(COMMENT_NOT_FOUND) + ); + return new CommentResponse(comment); + } +} diff --git a/backend/src/main/java/com/project/capstone/member/controller/MemberController.java b/backend/src/main/java/com/project/capstone/member/controller/MemberController.java index 64397c5dd4..3f809acad5 100644 --- a/backend/src/main/java/com/project/capstone/member/controller/MemberController.java +++ b/backend/src/main/java/com/project/capstone/member/controller/MemberController.java @@ -10,7 +10,6 @@ @RequiredArgsConstructor @RequestMapping("/member") public class MemberController { - @GetMapping("/test") public ResponseEntity test() { return ResponseEntity.ok("ok"); diff --git a/backend/src/main/java/com/project/capstone/member/domain/Member.java b/backend/src/main/java/com/project/capstone/member/domain/Member.java index 30e8b0e554..74fa70fe69 100644 --- a/backend/src/main/java/com/project/capstone/member/domain/Member.java +++ b/backend/src/main/java/com/project/capstone/member/domain/Member.java @@ -1,5 +1,6 @@ package com.project.capstone.member.domain; +import com.fasterxml.jackson.annotation.JsonManagedReference; import com.project.capstone.auth.controller.dto.SignupRequest; import com.project.capstone.comment.domain.Comment; import com.project.capstone.memberclub.domain.MemberClub; @@ -38,15 +39,19 @@ public class Member { @Column(name = "created_at") private LocalDateTime createdAt; + @JsonManagedReference @OneToMany(mappedBy = "member") private List clubs = new ArrayList<>(); + @JsonManagedReference @OneToMany(mappedBy = "member") private List posts = new ArrayList<>(); + @JsonManagedReference @OneToMany(mappedBy = "member") private List comments = new ArrayList<>(); + @JsonManagedReference @OneToMany(mappedBy = "member") private List contents = new ArrayList<>(); diff --git a/backend/src/main/resources/data.sql b/backend/src/main/resources/data.sql index 34bb115ee7..e0280d343d 100644 --- a/backend/src/main/resources/data.sql +++ b/backend/src/main/resources/data.sql @@ -46,3 +46,10 @@ insert into member_club (id, member_id, club_id) values insert into post (id, club_id, member_id, title, body, created_at, is_sticky) values (1, 1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), '게시글 제목 1', '게시글 내용 1', '2023-05-26 15:48:57.450179', true); + +insert into comment (id, post_id, member_id, body, created_at) values +(1, 1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), "댓글 1", "2023-05-26 15:48:57.450179"); +insert into comment (id, post_id, member_id, body, created_at) values + (2, 1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), "댓글 2", "2023-05-26 15:48:57.450179"); +insert into comment (id, post_id, member_id, body, created_at) values + (3, 1, UNHEX(REPLACE("3f06af63-a93c-11e4-9797-00505690773f", "-","")), "댓글 3", "2023-05-26 15:48:57.450179");