-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.85 KB
/
index.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
const util = require("util");
const jsonBody = util.promisify(require("body/json"));
const Sequelize = require("sequelize");
const Storage = require("./src/storage/");
const { store: opt } = require("./configs/");
let store = null;
/* if you open the initializer feature, please implement the initializer function, as below: */
module.exports.initializer = async (context, callback) => {
const sequelize = new Sequelize(opt.name, opt.user, opt.pass, opt);
await sequelize.query("SET time_zone='+0:00'");
store = new Storage(sequelize);
await store.init();
callback();
};
module.exports.item = async (req, resp, context) => {
resp.setHeader("Content-Type", "Application/json");
const id = req.queries.id;
try {
const log = await store.item(id);
if (!log) {
resp.setStatusCode(404);
resp.send(
JSON.stringify({
code: "ResourceNotFound",
message: "Resource not found."
})
);
} else {
resp.setStatusCode(200);
resp.send(JSON.stringify(log));
}
} catch (e) {
resp.setStatusCode(500);
resp.send(e.message);
}
};
module.exports.list = async (req, resp, context) => {
resp.setHeader("Content-Type", "Application/json");
const uid = req.headers["x-auth-uuid"];
try {
const ls = await store.list(uid, req.queries);
resp.setStatusCode(200);
resp.send(JSON.stringify(ls));
} catch (e) {
resp.setStatusCode(500);
resp.send(e.message);
}
};
module.exports.save = async (req, resp, context) => {
resp.setHeader("Content-Type", "Application/json");
const uid = req.headers["x-auth-uuid"];
const { code, red, green, blue } = await jsonBody(req);
try {
const log = await store.add(red, green, blue, code, uid);
resp.setStatusCode(201);
resp.send(JSON.stringify(log));
} catch (e) {
resp.setStatusCode(500);
resp.send(e.message);
}
};