-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogStorage.js
59 lines (53 loc) · 1.12 KB
/
BlogStorage.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
'use strict'
class BlogStorage {
constructor () {
this.posts = []
this.postCount = 0
}
addPost (post) {
post.id = this.postCount
post.comments = []
this.posts.unshift(post)
this.postCount += 1
return post
}
addComment (id, comment) {
id = id * 1
const post = this.posts.find(p => p.id === id)
if (!post) {
return null
}
comment.postId = post.id
post.comments.push(comment)
return comment
}
getPost (id) {
id = id * 1
const post = this.posts.find(p => p.id === id)
if (!post) {
return null
}
return post
}
getPosts (page = 0, size = 3) {
const response = { }
page = page * 1
size = size * 1
response.posts = this.posts
.slice(page * size, (page + 1) * size)
.map(
(post) => {
return {
id: post.id,
userEmail: post.userEmail,
title: post.title,
date: post.date,
content: post.content
}
}
)
response.lastPage = (page + 1) * size >= this.posts.length
return response
}
}
module.exports = BlogStorage