From d2ac4445d62b43a772bd539729e07924ba9ea24c Mon Sep 17 00:00:00 2001 From: wisdom08 Date: Sat, 3 Dec 2022 12:12:43 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EB=8F=84?= =?UTF-8?q?=EB=A9=94=EC=9D=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/community/dev/domain/reply/Reply.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/com/community/dev/domain/reply/Reply.java diff --git a/src/main/java/com/community/dev/domain/reply/Reply.java b/src/main/java/com/community/dev/domain/reply/Reply.java new file mode 100644 index 0000000..a42a432 --- /dev/null +++ b/src/main/java/com/community/dev/domain/reply/Reply.java @@ -0,0 +1,36 @@ +package com.community.dev.domain.reply; + +import com.community.dev.domain.post.Post; +import lombok.Getter; +import lombok.Setter; + +import javax.persistence.*; + +@Getter +@Entity +public class Reply { + + @Setter + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + + @ManyToOne + private Post post; + + private String writer; + private String contents; + + protected Reply() {} + + public Reply(Post post, String writer, String contents) { + this.post = post; + this.writer = writer; + this.contents = contents; + } + + public static Reply createReply(Post post, String writer, String contents) { + return new Reply(post, writer, contents); + } +} From 7d3b06d8b03d6088df175d05a92433ecd3bc83fb Mon Sep 17 00:00:00 2001 From: wisdom08 Date: Sat, 3 Dec 2022 12:26:32 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=EA=B3=BC=20=EC=A1=B0=ED=9A=8C=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../community/dev/domain/reply/ReplyRepo.java | 9 ++++ .../dev/service/reply/ReplyService.java | 29 +++++++++++++ .../com/community/dev/web/PostController.java | 6 ++- .../community/dev/web/ReplyController.java | 41 +++++++++++++++++++ .../dev/domain/reply/ReplyRepoTest.java | 38 +++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/community/dev/domain/reply/ReplyRepo.java create mode 100644 src/main/java/com/community/dev/service/reply/ReplyService.java create mode 100644 src/main/java/com/community/dev/web/ReplyController.java create mode 100644 src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java diff --git a/src/main/java/com/community/dev/domain/reply/ReplyRepo.java b/src/main/java/com/community/dev/domain/reply/ReplyRepo.java new file mode 100644 index 0000000..3b019d1 --- /dev/null +++ b/src/main/java/com/community/dev/domain/reply/ReplyRepo.java @@ -0,0 +1,9 @@ +package com.community.dev.domain.reply; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface ReplyRepo extends JpaRepository { + List findByPostId(long postId); +} diff --git a/src/main/java/com/community/dev/service/reply/ReplyService.java b/src/main/java/com/community/dev/service/reply/ReplyService.java new file mode 100644 index 0000000..e2916bf --- /dev/null +++ b/src/main/java/com/community/dev/service/reply/ReplyService.java @@ -0,0 +1,29 @@ +package com.community.dev.service.reply; + +import com.community.dev.domain.reply.Reply; +import com.community.dev.domain.reply.ReplyRepo; +import com.community.dev.service.post.PostService; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class ReplyService { + + private final ReplyRepo replyRepo; + private final PostService postService; + + public ReplyService(ReplyRepo replyRepo, PostService postService) { + this.replyRepo = replyRepo; + this.postService = postService; + } + + public void createReply(Long postId, Reply reply) { + postService.getPost(postId); + replyRepo.save(Reply.createReply(postService.exist(postId), reply.getWriter(), reply.getContents())); + } + + public List getReplies(long id) { + return replyRepo.findByPostId(id); + } +} diff --git a/src/main/java/com/community/dev/web/PostController.java b/src/main/java/com/community/dev/web/PostController.java index 2f83e79..d91dcae 100644 --- a/src/main/java/com/community/dev/web/PostController.java +++ b/src/main/java/com/community/dev/web/PostController.java @@ -2,6 +2,7 @@ import com.community.dev.domain.post.Post; import com.community.dev.service.post.PostService; +import com.community.dev.service.reply.ReplyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -17,10 +18,12 @@ public class PostController { private final PostService postService; + private final ReplyService replyService; @Autowired - public PostController(PostService postService) { + public PostController(PostService postService, ReplyService replyService) { this.postService = postService; + this.replyService = replyService; } @GetMapping("write") @@ -46,6 +49,7 @@ public String writePost(@Valid Post post, BindingResult result, Model model) { @GetMapping("/{id}") public String showDetailForm(@PathVariable("id") long id, Model model) { model.addAttribute("post", postService.getPost(id)); + model.addAttribute("reply", replyService.getReplies(id)); return "post-detail"; } diff --git a/src/main/java/com/community/dev/web/ReplyController.java b/src/main/java/com/community/dev/web/ReplyController.java new file mode 100644 index 0000000..40503e6 --- /dev/null +++ b/src/main/java/com/community/dev/web/ReplyController.java @@ -0,0 +1,41 @@ +package com.community.dev.web; + +import com.community.dev.domain.reply.Reply; +import com.community.dev.service.post.PostService; +import com.community.dev.service.reply.ReplyService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.validation.Valid; + +@Controller +@RequestMapping("/{id}/replies/") +public class ReplyController { + + private final PostService postService; + private final ReplyService replyService; + + @Autowired + public ReplyController(PostService postService, ReplyService replyService) { + this.postService = postService; + this.replyService = replyService; + } + + + @PostMapping("/write") + public String writeReply(@PathVariable("id") long id, @Valid Reply reply, BindingResult result, Model model) { + if (result.hasErrors()) { + return "reply-add"; + } + replyService.createReply(id, reply); + + model.addAttribute("post", postService.getPost(id)); + model.addAttribute("reply", replyService.getReplies(id)); + return "post-detail"; + } +} \ No newline at end of file diff --git a/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java b/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java new file mode 100644 index 0000000..822cf6c --- /dev/null +++ b/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java @@ -0,0 +1,38 @@ +package com.community.dev.domain.reply; + +import com.community.dev.domain.post.Post; +import com.community.dev.domain.post.PostRepo; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@DataJpaTest +class ReplyRepoTest { + + private Post post; + + @Autowired + private ReplyRepo replyRepo; + + @Autowired + private PostRepo postRepo; + + @BeforeEach + void setUp() { + post = Post.createPost("writer", "title,", "contents", "1234"); + + postRepo.save(post); + replyRepo.save(Reply.createReply(this.post, "replyWriter", "replyContents")); + replyRepo.save(Reply.createReply(this.post, "replyWriter2", "replyContents2")); + } + + @Test + void 하나의_게시글에_작성된_댓글_개수_조회() { + assertThat(replyRepo.findByPostId(post.getId()).size()).isEqualTo(2); + } +} \ No newline at end of file From f5f4afc9ab5cc9e2d42ffa79d20e54f8032eb7d1 Mon Sep 17 00:00:00 2001 From: wisdom08 Date: Sat, 3 Dec 2022 12:38:07 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EB=8C=93=EA=B8=80=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/community/dev/domain/reply/ReplyRepoTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java b/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java index 822cf6c..4ecf705 100644 --- a/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java +++ b/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java @@ -27,8 +27,14 @@ void setUp() { post = Post.createPost("writer", "title,", "contents", "1234"); postRepo.save(post); - replyRepo.save(Reply.createReply(this.post, "replyWriter", "replyContents")); - replyRepo.save(Reply.createReply(this.post, "replyWriter2", "replyContents2")); + replyRepo.save(Reply.createReply(post, "replyWriter", "replyContents")); + replyRepo.save(Reply.createReply(post, "replyWriter2", "replyContents2")); + } + + @Test + void 댓글_저장() { + Reply reply = Reply.createReply(this.post, "replyWriter3", "replyContents3"); + assertThat(replyRepo.save(reply).getContents()).isEqualTo("replyContents3"); } @Test