Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

댓글 api 추가 #43

Merged
merged 3 commits into from
Dec 10, 2022
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
36 changes: 36 additions & 0 deletions src/main/java/com/community/dev/domain/reply/Reply.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/community/dev/domain/reply/ReplyRepo.java
Original file line number Diff line number Diff line change
@@ -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<Reply, Long> {
List<Reply> findByPostId(long postId);
}
29 changes: 29 additions & 0 deletions src/main/java/com/community/dev/service/reply/ReplyService.java
Original file line number Diff line number Diff line change
@@ -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<Reply> getReplies(long id) {
return replyRepo.findByPostId(id);
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/community/dev/web/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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";
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/community/dev/web/ReplyController.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
44 changes: 44 additions & 0 deletions src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}