-
Notifications
You must be signed in to change notification settings - Fork 0
/
memes.js
48 lines (34 loc) · 763 Bytes
/
memes.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
const data = require('./data')
const createNewMeme = (userId, date) => {
const memes = data.get().memes
return {
id: memes.length ? memes[memes.length - 1].id + 1 : 1,
userId,
date,
messages: [],
votes: []
}
}
const saveNewMeme = (meme, callback) => {
data.get().memes.push(meme)
data.save(callback)
}
const getMemeById = (memeId) => {
return data.get().memes.find(m => m.id === memeId)
}
const processUserVote = (meme, user, callback) => {
const iUser = meme.votes.indexOf(user.id)
if (iUser < 0) {
meme.votes.push(user.id)
} else {
meme.votes.splice(iUser, 1);
}
data.save(callback)
return meme.votes.length
}
module.exports = {
createNewMeme,
saveNewMeme,
getMemeById,
processUserVote
}