-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathserver.js
304 lines (285 loc) · 8.77 KB
/
server.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"use strict";
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const crate = require('node-crate');
const uniqid = require('uniqid');
const sha1 = require('sha1');
const request = require('request');
const app = express();
app.use(cors());
app.use(bodyParser.json({
limit: '50mb'
}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true,
parameterLimit: 1000000
}));
crate.connect('localhost', 4200);
app.listen(8080, () => console.log('Example app listening on port 8080!'));
app.get('/', (req, response) => response.json({
message: 'Welcome to the node.js example for CrateDB',
state: 'Server up and running'
}));
// Get all posts
app.get('/posts', (req, response) => {
crate.execute("SELECT p.*, c.name as country, c.geometry as area "
+ "FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], c.geometry) "
+ "ORDER BY p.created DESC").then((res) => {
response.status(200).json(res.json);
});
});
// Get a post by id
app.get('/post/:id', (req, response) => {
crate.execute("SELECT p.*, c.name as country, c.geometry as area "
+ "FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], c.geometry) "
+ "AND p.id = ?", [req.params.id]).then((res) => {
if (res.json && res.json.length) {
response.status(200).json(res.json[0]);
} else {
response.status(404).json({
error: `Post with id=\"${req.params.id}\" not found`,
status: 404,
});
}
});
});
// Insert new post
app.post('/posts', (req, response) => {
if (!req.body) {
return response.status(400).json({
error: 'Please pass a body to the request',
status: 400,
});
}
if (!req.body.user || !req.body.user.name) {
return response.status(400).json({
error: 'Argument "name" is required',
status: 400,
});
}
if (!req.body.user.location || req.body.user.location.length === 0) {
return response.status(400).json({
error: 'Argument "location" is required',
status: 400,
});
}
let id = uniqid();
crate.execute("INSERT INTO guestbook.posts "
+ "(id, \"user\", text, created, image_ref, like_count) "
+ "VALUES (?, ?, ?, CURRENT_TIMESTAMP, ?, ?)", [
id,
req.body.user,
req.body.text,
req.body.image_ref,
0
]).then((res) => {
if (res.rowcount === 0) {
return response.status(500).json({
error: 'DB error',
status: 500,
});
}
crate.execute("REFRESH TABLE guestbook.posts").then(() => {
crate.execute("SELECT p.*, c.name as country, c.geometry as area "
+ "FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], c.geometry) "
+ "AND p.id = ?", [id]).then((res) => {
response.status(201).json(res.json);
});
});
});
});
// Change the text of a post
app.put('/post/:id', (req, response) => {
if (!req.body) {
return response.status(400).json({
error: 'Please pass a body to the req',
status: 400,
});
}
if (!req.body.text) {
return response.status(400).json({
error: 'Argument "text" is required',
status: 400,
});
}
crate.execute("UPDATE guestbook.posts SET text=? WHERE id=?",
[req.body.text, req.params.id]).then((res) => {
if (res.rowcount === 0) {
return response.status(404).json({
error: `Post with id="${req.params.id}" not found`,
status: 404,
});
}
crate.execute("REFRESH TABLE guestbook.posts").then(() => {
crate.execute("SELECT p.*, c.name as country, c.geometry as area "
+ "FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], c.geometry) "
+ "AND p.id = ?", [req.params.id]).then((res) => {
return response.status(200).json(res.json[0]);
});
});
});
});
// Increments the number of likes for a given post
app.put('/post/:id/like', (req, response) => {
crate.execute("SELECT * FROM guestbook.posts WHERE id=?",
[req.params.id]).then((res) => {
if (res.rowcount === 0) {
return response.status(404).json({
error: `Post with id="${req.params.id}" not found`,
status: 404,
});
}
crate.execute("UPDATE guestbook.posts SET like_count = like_count + 1 "
+ "WHERE id=?", [req.params.id]).then((res) => {
if (res.rowcount === 0) {
return response.status(500).json({
error: 'Update statement went wrong',
status: 500,
});
}
crate.execute("REFRESH TABLE guestbook.posts").then(() => {
crate.execute("SELECT p.*, c.name as country, c.geometry as area "
+ "FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], c.geometry) "
+ "AND p.id = ?", [req.params.id]).then((res) => {
return response.status(200).json(res.json[0]);
});
});
});
});
});
// Delete a post
app.delete('/post/:id', (req, response) => {
crate.execute('SELECT * FROM guestbook.posts WHERE id = ?',
[req.params.id]).then((res) => {
if (res.rowcount === 0) {
return response.status(404).json({
error: `Post with id="${req.params.id}" not found`,
status: 404,
});
}
crate.execute('DELETE FROM guestbook.posts WHERE id=?',
[req.params.id]).then((res2) => {
return response.status(204).json({
message: 'Post deleted successfully',
status: 204,
});
});
});
});
// Get all images
app.get('/images', (req, response) => {
crate.execute("SELECT digest, last_modified FROM blob.guestbook_images")
.then((res) => {
response.status(200).json(res.json);
});
});
// Get image by id
app.get('/image/:digest', (req, response) => {
let url = 'http://localhost:4200/_blobs/guestbook_images/'
+ req.params.digest;
request({
url: url,
encoding: null
}, (error, res, body) => {
if (body.length === 0) {
return response.status(404).json({
error: `Image with digest="${req.params.digest}" not found`,
status: 404,
});
}
response.writeHead(200, {
'Content-Type': 'image/gif',
'Content-Length': body.length
});
response.end(new Buffer.from(body));
});
});
// Delete image by id
app.delete('/image/:digest', (req, response) => {
let url = 'http://localhost:4200/_blobs/guestbook_images/'
+ req.params.digest;
request({
url: url,
method: 'DELETE'
}, (error, res, body) => {
if (res.statusCode === 204) {
response.status(204).json({
error: 'Image deleted',
status: 204,
});
} else if (res.statusCode === 404) {
response.status(404).json({
error: `Image with digest="${req.params.digest}" not found`,
status: 404,
});
} else {
response.status(500).json({
error: 'Image deletion failed',
status: 500,
});
}
});
});
// Upload image
app.post('/images', (req, response) => {
if (!req.body.blob || req.body.blob === undefined) {
return response.status(400).json({
error: 'Argument "blob" is required',
status: 400,
});
}
let data = Buffer.from(req.body.blob, 'base64');
let digest = sha1(data);
let url = 'http://localhost:4200/_blobs/guestbook_images/' + digest;
request({
headers: {
'Content-Type': 'image/gif'
},
url: url,
method: 'PUT',
body: data
}, (error, res, body) => {
if (res.statusCode === 201 || res.statusCode === 409) {
response.status(res.statusCode).json({
url: '/image/' + digest,
digest: digest
});
} else {
console.error("Uploading data to CrateDB failed:", error);
response.status(500).json({
error: 'Something went wrong with the upload',
status: 500,
});
}
});
});
// Search
app.post('/search', (req, response) => {
if (req.body.query_string === "" || !req.body.query_string) {
return response.status(404).json({
error: 'Argument "query_string" is required',
status: 404,
});
}
crate.execute("SELECT p.*, p._score as _score, c.name as country, c.geometry "
+ "as area FROM guestbook.posts AS p, guestbook.countries AS c "
+ "WHERE within(p.\"user\"['location'], "
+ "c.geometry) AND match(p.text, ?)",
[req.body.query_string]).then((res) => {
response.status(200).json(res.json);
});
});
// 404 Not Found
app.use((req, response, next) => {
response.status(404).json({
error: "Sorry can't find that!",
status: 404,
});
})