Skip to content
Open
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
11 changes: 7 additions & 4 deletions src/main/java/com/community/dev/domain/post/Post.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.community.dev.domain.post;

import com.community.dev.domain.reply.Reply;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Getter
@Entity
Expand All @@ -23,6 +23,9 @@ public class Post {
private String contents;
private String password;

@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE, orphanRemoval = true)
private final List<Reply> replies = new ArrayList<>();

protected Post() {}

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.community.dev.domain.reply.Reply;
import com.community.dev.domain.reply.ReplyRepo;
import com.community.dev.service.post.PostService;
import com.community.dev.web.dto.ReplyResponseDto;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReplyService {
Expand All @@ -23,7 +25,8 @@ public void createReply(Long postId, Reply reply) {
replyRepo.save(Reply.createReply(postService.exist(postId), reply.getWriter(), reply.getContents()));
}

public List<Reply> getReplies(long id) {
return replyRepo.findByPostId(id);
public List<ReplyResponseDto> getReplies(long id) {
List<Reply> replies = replyRepo.findByPostId(id);
return replies.stream().map(ReplyResponseDto::from).collect(Collectors.toList());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/community/dev/web/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +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));
model.addAttribute("replies", replyService.getReplies(id));
return "post-detail";
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/community/dev/web/ReplyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import javax.validation.Valid;

@Controller
@RequestMapping("/{id}/replies/")
@RequestMapping("/posts/{id}/replies/")
public class ReplyController {

private final PostService postService;
Expand All @@ -34,8 +34,7 @@ public String writeReply(@PathVariable("id") long id, @Valid Reply reply, Bindin
}
replyService.createReply(id, reply);

model.addAttribute("post", postService.getPost(id));
model.addAttribute("reply", replyService.getReplies(id));
return "post-detail";
model.addAttribute("reply", reply);
return "reply";
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/community/dev/web/dto/ReplyResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.community.dev.web.dto;


import com.community.dev.domain.reply.Reply;
import lombok.Getter;

@Getter
public class ReplyResponseDto {

private String writer;
private String contents;

protected ReplyResponseDto() {}

private ReplyResponseDto(String contents, String writer) {
this.contents = contents;
this.writer = writer;
}

public static ReplyResponseDto from(Reply entity) {
return new ReplyResponseDto(entity.getContents(), entity.getWriter());
}
}
44 changes: 42 additions & 2 deletions src/main/resources/templates/post-detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<link rel="stylesheet" href="https://unpkg.com/[email protected]/css/bulma.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<div th:replace="/nav :: nav"></div>
<body>
<div class="container is-max-desktop">
<div class="container is-max-desktop" id="post-and-replies">
<div th:replace="/nav :: nav"></div>
<div class="notification is-primary">
<form action="#" th:action="@{/posts/update/{id}(id=${post.id})}" th:object="${post}" method="post">
<div class="field">
Expand Down Expand Up @@ -64,7 +64,47 @@
<button> <a th:href="@{/posts/edit/{id}(id=${post.id})}">수정</a></button>
<button> <a th:href="@{/posts/delete/{id}(id=${post.id})}">삭제</a></button>
</div>
<div class="col justify-content-center my-1 mx-0">
<input type="hidden" name="post_id" id="post_id" th:value="${post.id}">
작성자: <input type="text" id="reply_writer" name="reply_writer">
내용: <input type="text" id="reply_contents" name="reply_contents">
<input type="button" onclick="return createReply()" value="댓글작성">
</div>

<div id="replies">
<div th:each="reply:${replies}">
<form method="POST">
작성자: <p th:text="${reply.writer}"></p>
댓글: <p th:text="${reply.contents}"></p>
</form>
<hr>
</div>
<div id="reply">

</div>
</div>

</div>

</body>

<script>
function createReply() {
const postId = $("#post_id").val();
const reply = {
writer: $("#reply_writer").val(),
contents: $("#reply_contents").val(),
};
$.ajax({
url: postId+"/replies/write",
type: "POST",
data: reply,
})
.done(function (fragment) {
$("#replies").append(fragment);
$("#reply_writer").val("");
$("#reply_contents").val("");
});
}
</script>
</html>
9 changes: 9 additions & 0 deletions src/main/resources/templates/reply.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="reply">
<div>
<form method="POST">
작성자: <p th:text="${reply.writer}"></p>
댓글: <p th:text="${reply.contents}"></p>
</form>
<hr>
</div>
</div>