Skip to content

Commit 9e36c9f

Browse files
committed
post count
1 parent e52e233 commit 9e36c9f

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,33 @@ app.get("/category/:category/new", (req, res) => {
103103
});
104104

105105
app.get("/topic/:topic", (req, res) => {
106-
db.get("SELECT * FROM topics WHERE id = ?", [req.params.topic], (err, row) => {
106+
db.get("SELECT * FROM topics WHERE id = ?", [req.params.topic], (err, topic) => {
107107
db.all("SELECT * FROM posts WHERE topic = ?", [req.params.topic], async (err, posts) => {
108108
let get_reactions = (post) => new Promise((resolve) => {
109109
db.all("SELECT * FROM reactions WHERE post = ?", [post.id], function(err, reactions) {
110110
resolve(reactions);
111111
});
112112
});
113-
114113
let post_reactions = await Promise.all(posts.map(post => get_reactions(post)));
115114

115+
let get_post_counts = (post) => new Promise((resolve) => {
116+
db.get("SELECT COUNT(*) FROM posts WHERE author = ?", [post.author], (err, count) => {
117+
if (!err) {
118+
resolve(count["COUNT(*)"]);
119+
}
120+
});
121+
});
122+
let post_count = await Promise.all(posts.map(post => get_post_counts(post)));
123+
124+
console.log(post_count);
125+
116126
res.render("topic", {
117-
topic: row,
127+
topic: topic,
118128
posts: posts,
119-
reactions: post_reactions
129+
reactions: post_reactions,
130+
post_count: post_count
120131
});
132+
121133
});
122134
});
123135
});

views/topic.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<span class="post-tag" title="Original Poster">OP</span>
3434
<% } %>
3535
<div class="post-user-role"></div>
36+
<div class="post-user-post-count"><%= post_count[i] %> posts</div>
3637
</div>
3738
</section>
3839
<div class="post-content-container">

0 commit comments

Comments
 (0)