-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.js
More file actions
140 lines (126 loc) · 3.07 KB
/
posts.js
File metadata and controls
140 lines (126 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const clone = require('clone')
let db = {}
const defaultData = {
"8xf0y6ziyjabvozdd253nd": {
id: '8xf0y6ziyjabvozdd253nd',
timestamp: 1467166872634,
title: 'Udacity is the best place to learn React',
body: 'Everyone says so after all.',
author: 'thingtwo',
category: 'react',
voteScore: 6,
deleted: false,
commentCount: 2
},
"6ni6ok3ym7mf1p33lnez": {
id: '6ni6ok3ym7mf1p33lnez',
timestamp: 1468479767190,
title: 'Learn Redux in 10 minutes!',
body: 'Just kidding. It takes more than 10 minutes to learn technology.',
author: 'thingone',
category: 'redux',
voteScore: -5,
deleted: false,
commentCount: 0
}
}
function getData (token) {
let data = db[token]
if (data == null) {
data = db[token] = clone(defaultData)
}
return data
}
function getByCategory (token, category) {
return new Promise((res) => {
let posts = getData(token)
let keys = Object.keys(posts)
let filtered_keys = keys.filter(key => posts[key].category === category && !posts[key].deleted)
res(filtered_keys.map(key => posts[key]))
})
}
function get (token, id) {
return new Promise((res) => {
const posts = getData(token)
res(
posts[id].deleted
? {}
: posts[id]
)
})
}
function getAll (token) {
return new Promise((res) => {
const posts = getData(token)
let keys = Object.keys(posts)
let filtered_keys = keys.filter(key => !posts[key].deleted)
res(filtered_keys.map(key => posts[key]))
})
}
function add (token, post) {
return new Promise((res) => {
let posts = getData(token)
posts[post.id] = {
id: post.id,
timestamp: post.timestamp,
title: post.title,
body: post.body,
author: post.author,
category: post.category,
voteScore: 1,
deleted: false,
commentCount: 0
}
res(posts[post.id])
})
}
function vote (token, id, option) {
return new Promise((res) => {
let posts = getData(token)
post = posts[id]
switch(option) {
case "UP_VOTE":
post.voteScore = post.voteScore + 1
break
case "DOWN_VOTE":
post.voteScore = post.voteScore - 1
break
default:
console.log(`posts.vote received incorrect parameter: ${option}`)
}
res(post)
})
}
function disable (token, id) {
return new Promise((res) => {
let posts = getData(token)
posts[id].deleted = true
res(posts[id])
})
}
function edit (token, id, post) {
return new Promise((res) => {
let posts = getData(token)
for (prop in post) {
posts[id][prop] = post[prop]
}
res(posts[id])
})
}
function incrementCommentCounter(token, id, count) {
const data = getData(token)
if (data[id]) {
data[id].commentCount += count
}
}
module.exports = {
get,
getAll,
getByCategory,
add,
vote,
disable,
edit,
getAll,
incrementCommentCounter
}