-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-service.js
124 lines (114 loc) · 3.3 KB
/
blog-service.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
const fs = require("fs");
const { resolve } = require("path");
let posts = [];
let categories = [];
// open file and read data for posts and categories
module.exports.initialize = function() {
return new Promise((resolve, reject) => {
fs.readFile("./data/posts.json", (err, data) => {
if (err) {
throw err;
} else {
posts = JSON.parse(data);
resolve();
}
});
fs.readFile("./data/categories.json", (err, data) => {
if (err) {
reject("unable to read file");
} else {
categories = JSON.parse(data);
resolve();
}
});
});
};
// see all data for posts
module.exports.getAllPosts = function() {
return new Promise((resolve, reject) => {
if (posts.length == 0) {
reject("no results returned");
} else {
resolve(posts);
}
});
};
// see published
module.exports.getPublishedPosts = function() {
return new Promise((resolve, reject) => {
let publishedPosts = [];
for (let i = 0; i < posts.length; i++) {
if (posts[i].published == true) {
publishedPosts.push(posts[i]);
}
}
if (publishedPosts.length == 0) {
reject("no results returned");
} else {
resolve(publishedPosts);
}
});
};
// see all data for categories
module.exports.getCategories = function() {
return new Promise((resolve, reject) => {
if (categories.length == 0) {
reject("no results returned");
} else {
resolve(categories);
}
});
};
module.exports.addPost = function(postData) {
return new Promise((resolve, reject) => {
postData.id = posts.length + 1;
postData.published = postData.published ? true : false;
posts.push(postData);
resolve();
});
};
module.exports.getPostsByCategory = function(category) {
return new Promise((resolve, reject) => {
let postForCategory = [];
for (var i = 0; i < posts.length; i++) {
if (posts[i].category == category) {
postForCategory.push(posts[i]);
}
}
if (postForCategory.length == 0) {
reject("No Posts to be displayed");
} else {
resolve(postForCategory);
}
});
};
module.exports.getPostsByMinDate = function(minDateStr) {
return new Promise((resolve, reject) => {
let MinDatePosts = [];
for (var i = 0; i < posts.length; i++) {
if (new Date(posts[i].postDate) >= new Date(minDateStr)) {
MinDatePosts.push(posts[i]);
}
}
if (MinDatePosts.length == 0) {
reject("No Posts to be displayed");
} else {
resolve(MinDatePosts);
}
});
};
module.exports.getPostById = function(id) {
return new Promise((resolve, reject) => {
let postForId = [];
for (var i = 0; i < posts.length; i++) {
if (posts[i].id == id) {
postForId.push(posts[i]);
}
}
if (postForId.length == 0) {
reject("No Posts to be displayed");
} else {
resolve(postForId);
}
});
};