-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.js
176 lines (163 loc) · 3.73 KB
/
posts.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const clone = require('clone')
let db = {}
const defaultData = {
"1": {
id: '1',
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,
edited: false
},
"2": {
id: '2',
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: 1,
deleted: false,
edited: false
},
"3": {
id: '3',
timestamp: 1507907893799,
title: 'Should the reviewer approve my project?',
body: "Well i think you should. I worked pretty hard on this, and even added an auto crop at 75 chars.",
author: 'Student',
category: 'udacity',
voteScore: 12,
deleted: false,
edited: true
},
"4": {
id: '4',
timestamp: 1500000000000,
title: "I think you should approve Reinaldo's project",
body: "He worked pretty hard.",
author: "Friend",
category: 'udacity',
voteScore: 10,
deleted: false,
edited: true
},
"5": {
id: '5',
timestamp: 1507907000000,
title: 'Should we use styled-components?',
body: "Yes we should, it's way better to maintain",
author: 'Student',
category: 'udacity',
voteScore: 8,
deleted: false,
edited: true
},
"6": {
id: '6',
timestamp: 1507900000000,
title: 'Just another post',
body: 'To test out the category changer',
author: 'Student',
category: 'react',
voteScore: 9,
deleted: false,
edited: true
}
}
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,
edited: false
}
res(posts[post.id])
})
}
function vote (token, id, option) {
return new Promise((res) => {
let posts = getData(token)
post = posts[id]
switch(option) {
case "upVote":
post.voteScore = post.voteScore + 1
break
case "downVote":
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])
})
}
module.exports = {
get,
getAll,
getByCategory,
add,
vote,
disable,
edit,
getAll
}