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); + } +} 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..4ecf705 --- /dev/null +++ b/src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java @@ -0,0 +1,44 @@ +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(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 + void 하나의_게시글에_작성된_댓글_개수_조회() { + assertThat(replyRepo.findByPostId(post.getId()).size()).isEqualTo(2); + } +} \ No newline at end of file