forked from Salmandabbakuti/qldb-essentials
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest-server.js
82 lines (71 loc) · 2.38 KB
/
rest-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
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const qldb_config = require('./config.js');
const qldb = require('qldb').default;
const ionize = require('qldb').ionize;
const QuantumClient = new qldb({
accessKey:qldb_config.ACCESS_KEY_ID,
secretKey:qldb_config.SECRET_KEY,
region:qldb_config.REGION,
ledger:qldb_config.LEDGER_NAME
});
app.get('/api/readAll/', async function (req, res) {
try{
const response = await QuantumClient.execute('SELECT * FROM Movies');
res.send(response)
}catch (err) {
res.status(500).send(err)
}
})
app.get('/api/search/', async function (req, res) {
try{
const response = await QuantumClient.execute(`SELECT * FROM Movies AS m WHERE m.${req.query.searchField} = '${req.query.value}'`);
res.send(response)
}catch (err) {
res.status(500).send(err)
}
})
app.get('/api/searchById/', async function (req, res) {
try{
const response = await QuantumClient.execute(`SELECT * FROM Movies AS r BY rid WHERE rid = '${req.query.id}'`);
res.send(response)
}catch (err) {
res.status(500).send(err)
}
})
app.post('/api/create/', async function (req, res) {
try{
const movie = req.body;
const response = await QuantumClient.execute(`INSERT INTO Movies ${ionize(movie)}`);
res.send(`Document Created.. ${response}`)
}catch (err) {
res.status(500).send(err)
}
})
app.put('/api/update/', async function (req, res) {
try{
const response = await QuantumClient.execute(`UPDATE Movies AS m SET m.${req.body.field} = '${req.body.value}' WHERE m.${req.body.filterField}= '${req.body.filterValue}'`);
res.send(`Document Updated`)
}catch (err) {
res.status(500).send(err)
}
})
app.delete('/api/delete/', async function (req, res) {
try{
const response = await QuantumClient.execute(`DELETE FROM Movies AS m WHERE m[${req.body.filterField}] = ${req.body.filterValue}`);
res.status(200).send("Deleted Document Successfully.")
}catch (err) {
res.status(500).send(err)
}
})
app.get('/api/getHistory/', async function (req, res) {
try{
const response = await QuantumClient.execute(`SELECT * FROM history(Movies) AS m WHERE m.data.${req.query.filterField} = '${req.query.filterValue}'`);
res.send(response)
}catch (err) {
res.status(500).send(err)
}
})
app.listen(3000, () => { console.log('Server is running at 3000..') });