Skip to content

Commit 87f81ad

Browse files
authored
Added comment counts to posts (udacity#21)
* Added comment counts to posts - adding or removing a comment increments the comment count for that post - cleaned up ununsed packages in package.json - added starting comment counts to the default post data - updated the server-api README with notes about commentCounts
1 parent 5fab3ee commit 87f81ad

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

Diff for: api-server/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ fetch(
2020
)
2121
```
2222

23+
### Comment Counts
24+
Posts retrieved in a list or individually now contain comment counts in the format `post: { commentCount: 0 }`. This should make it easier to display the number of comments a post has without having to call the comments endpoint for each post. This count is updated whenever a comment is added or deleted via the `POST /comments` or `DELETE /comments/:id` endpoints.
25+
2326
### API Endpoint
2427

2528
The following endpoints are available:

Diff for: api-server/comments.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const clone = require('clone')
2+
const posts = require('./posts')
23

34
let db = {}
45

@@ -68,6 +69,7 @@ function add (token, comment) {
6869
parentDeleted: false
6970
}
7071

72+
posts.incrementCommentCounter(token, comment.parentId, 1)
7173
res(comments[comment.id])
7274
})
7375
}
@@ -104,6 +106,7 @@ function disable (token, id) {
104106
return new Promise((res) => {
105107
let comments = getData(token)
106108
comments[id].deleted = true
109+
posts.incrementCommentCounter(token, comments[id].parentId, -1)
107110
res(comments[id])
108111
})
109112
}

Diff for: api-server/package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
"cors": "^2.8.3",
1717
"dotenv": "^4.0.0",
1818
"express": "^4.15.2",
19-
"clone": "^2.1.1",
20-
"redux":"^3.7.2",
21-
"react-redux":"^5.0.5",
22-
"react-router":"^4.1.1",
23-
"react-router-dom":"4.1.1"
19+
"clone": "^2.1.1"
2420
}
2521
}

Diff for: api-server/posts.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const defaultData = {
1111
author: 'thingtwo',
1212
category: 'react',
1313
voteScore: 6,
14-
deleted: false
14+
deleted: false,
15+
commentCount: 2
1516
},
1617
"6ni6ok3ym7mf1p33lnez": {
1718
id: '6ni6ok3ym7mf1p33lnez',
@@ -21,7 +22,8 @@ const defaultData = {
2122
author: 'thingone',
2223
category: 'redux',
2324
voteScore: -5,
24-
deleted: false
25+
deleted: false,
26+
commentCount: 0
2527
}
2628
}
2729

@@ -74,7 +76,8 @@ function add (token, post) {
7476
author: post.author,
7577
category: post.category,
7678
voteScore: 1,
77-
deleted: false
79+
deleted: false,
80+
commentCount: 0
7881
}
7982

8083
res(posts[post.id])
@@ -117,6 +120,13 @@ function edit (token, id, post) {
117120
})
118121
}
119122

123+
function incrementCommentCounter(token, id, count) {
124+
const data = getData(token)
125+
if (data[id]) {
126+
data[id].commentCount += count
127+
}
128+
}
129+
120130
module.exports = {
121131
get,
122132
getAll,
@@ -125,5 +135,6 @@ module.exports = {
125135
vote,
126136
disable,
127137
edit,
128-
getAll
138+
getAll,
139+
incrementCommentCounter
129140
}

0 commit comments

Comments
 (0)