-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from wisdom08/reply
댓글 api 추가
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/com/community/dev/service/reply/ReplyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/test/java/com/community/dev/domain/reply/ReplyRepoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |