-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
163 lines (138 loc) · 4.46 KB
/
app.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
const express = require('express');
const fs = require('fs');
const app = express();
const parser = require('body-parser');
app.use(express.static(`${__dirname}/public/`));
app.use(parser.json());
app.listen(3000, () => {
console.log('Server running on port 3000');
});
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/public/index.html`);
});
app.get('/read', (req, res) => {
res.end(fs.readFileSync('./server/data/posts.json', 'utf8'));
});
app.post('/write', (req, res) => {
fs.writeFileSync('./server/data/posts.json', `${JSON.stringify(req.body)}`);
res.end();
});
app.get('/getPhotoPost', (req, res) => {
const photoPosts = JSON.parse(fs.readFileSync('./server/data/posts.json', 'utf8'));
const post = photoPosts.find(post => String(req.query.id) === post.id);
if (post) {
res.send(post);
res.status(200).end();
} else {
res.status(404).end();
}
});
app.post('/getPhotoPosts', (req, res) => {
const photoPosts = JSON.parse(fs.readFileSync('./server/data/posts.json', 'utf8'));
const filterConfig = req.body;
skip = req.query.skip || 0;
top = req.query.top || 10;
// console.log ("parameters filterConfig: " + filterConfig.author);
photoPosts.forEach((post) => { post.createdAt = new Date(post.createdAt); });
let result = [];
if (filterConfig !== undefined && 'author' in filterConfig) {
photoPosts.forEach((value) => {
if (value.author === filterConfig.author) {
result.push(value);
}
});
} else if (filterConfig !== undefined && 'createdAt' in filterConfig) {
photoPosts.forEach((value) => {
if (value.createdAt > filterConfig.createdAt) {
result.push(value);
}
});
} else {
result = photoPosts.slice();
}
result.sort((a, b) => b.createdAt - a.createdAt);
result = result.slice(skip, top);
if (result) {
res.send(result);
res.status(200).end();
} else {
res.status(404).end();
}
});
app.post('/addPhotoPost', (req, res) => {
const photoPosts = JSON.parse(fs.readFileSync('./server/data/posts.json', 'utf8'));
const newPost = req.body;
newPost.createdAt = new Date(newPost.createdAt);
let flag = true;
photoPosts.forEach((post) => {
if (post.id === newPost.id) {
flag = false;
}
});
if (validatePhotoPost(newPost) && flag) {
photoPosts.push(newPost);
fs.writeFileSync('./server/data/posts.json', JSON.stringify(photoPosts));
res.send('Post was added.');
res.status(200).end();
} else {
res.send("Post wasn't added.");
res.status(404).end();
}
});
let validatePhotoPost = function (post) {
if (post === undefined) {
return false;
}
if (!('id' in post && (typeof post.id === 'string'))) {
return false;
}
if (!('description' in post && (typeof post.description === 'string') && post.description.length < 200)) {
return false;
}
if (!('createdAt' in post && post.createdAt instanceof Date)) {
return false;
}
if (!('author' in post && (typeof post.author === 'string'))) {
return false;
}
if (!('photoLink' in post && (typeof post.author === 'string'))) {
return false;
}
return true;
};
app.put('/editPhotoPost', (req, res) => {
const photoPosts = JSON.parse(fs.readFileSync('./server/data/posts.json', 'utf8'));
const post = req.body;
if (!post) {
res.status(404).end();
}
const result = photoPosts.find(item => item.id === String(req.query.id));
if (result === null) {
res.send(`Don't find post with id = ${post.id}`);
fs.writeFileSync('./server/data/posts.json', JSON.stringify(photoPosts));
res.status(404).end();
} else {
if ('description' in post && (typeof post.description === 'string') && post.description.length < 200) {
result.description = post.description;
}
if ('photoLink' in post && (typeof post.photoLink === 'string')) {
result.photoLink = post.photoLink;
}
res.send('Post successfully changed.');
fs.writeFileSync('./server/data/posts.json', JSON.stringify(photoPosts));
res.status(200).end();
}
});
app.delete('/removePhotoPost', (req, res) => {
const photoPosts = JSON.parse(fs.readFileSync('./server/data/posts.json', 'utf8'));
const result = photoPosts.find(item => item.id === String(req.query.id));
if (result === null) {
res.status(404).end();
res.send("Don't find post");
} else {
photoPosts.splice(photoPosts.indexOf(result), 1);
res.send(result);
res.status(200).end();
}
fs.writeFileSync('./server/data/posts.json', JSON.stringify(photoPosts));
});