Skip to content

Commit fa15632

Browse files
committed
Write Comment API
1 parent 56daa20 commit fa15632

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

Node_Express/Chapter06/board/app.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,34 @@ app.delete("/delete", async (req, res) => {
120120
}
121121
});
122122

123+
app.post("/write-comment", async (req, res) => {
124+
const { id, name, password, comment } = req.body;
125+
const post = await postService.getPostById(collection, id);
126+
127+
if (post.comments) {
128+
post.comments.push({
129+
idx: post.comments.length + 1,
130+
name,
131+
password,
132+
comment,
133+
createdDt: new Date().toISOString(),
134+
});
135+
} else {
136+
post.comments = [
137+
{
138+
idx: 1,
139+
name,
140+
password,
141+
comment,
142+
createdDt: new Date().toISOString(),
143+
},
144+
];
145+
}
146+
147+
postService.updatePost(collection, id, post);
148+
return res.redirect(`/detail/${id}`);
149+
});
150+
123151
let collection;
124152
app.listen(3000, async () => {
125153
console.log("SERVER START");

Node_Express/Chapter06/board/views/detail.handlebars

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
</div>
2424
<section>
2525
<div>
26-
<h3>3개의 댓글이 있습니다.</h3>
26+
<h3>{{lengthOfList comments}}개의 댓글이 있습니다.</h3>
2727
</div>
2828
<form method="post" action="/write-comment">
29+
<input type="hidden" name="id" value="{{_id}}"/>
2930
<div>
3031
<div>
3132
<input type="text" name="name" placeholder="이름" />
@@ -50,19 +51,20 @@
5051
</section>
5152

5253
<section>
54+
{{#each comments}}
5355
<div>
5456
<div>
55-
작성자:
56-
<b>댓글 작성자</b>
57+
작성자: <b>{{name}}</b>
5758
</div>
5859
<div>
59-
작성일시: 2025-09-12 16:18:00
60-
<button onclick="deleteComment('1')">삭제</button>
60+
작성일시: {{dateString createdDt}}
61+
<button onclick="deleteComment('{{idx}}')">삭제</button>
6162
</div>
6263
<div>
6364
<pre>{{comment}}</pre>
6465
</div>
6566
</div>
67+
{{/each}}
6668
</section>
6769

6870
{{/with}}

0 commit comments

Comments
 (0)