-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrollers.js
106 lines (88 loc) · 2.16 KB
/
controllers.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
// Dependencies
const crypto = require('crypto');
const { dataService } = require('./helpers');
// Get EITs Controller
exports.getEITs = (req, res) => {
dataService.getEITs((code, err, data) => {
if (err) {
if (code) {
return res.status(code).json(err);
}
return res.status(500).json(err);
}
res.json(data);
});
}
// Add an EIT Controller
exports.addEIT = (req, res) => {
const { firstName, lastName, age, country } = req.body;
if (!firstName) {
return res.status(400).json({
reason: "firstName not found in request body"
});
}
if (!lastName) {
return res.status(400).json({
reason: "lastName not found in request body"
});
}
if (!age) {
return res.status(400).json({
reason: "age not found in request body"
});
}
if (!country) {
return res.status(400).json({
reason: "country not found in request body"
});
}
dataService.addEIT(req.body, (code, err, data) => {
if (err) {
if (code) {
return res.status(code).json(err);
}
return res.status(500).json(err);
}
res.status(201).json(data);
});
}
// Get a Single EIT
exports.getEIT = (req, res) => {
dataService.getEIT(req.params.id, (code, err, data) => {
if (err) {
if (code) {
return res.status(code).json(err);
}
return res.status(500).json(err);
}
res.json(data);
});
}
exports.updateEIT = (req, res) => {
dataService.updateEIT(req.params.id, req.body, (code, err, data) => {
if (err) {
if (code) {
return res.status(code).json(err);
}
return res.status(500).json(err);
}
res.json(data);
});
}
exports.deleteEIT = (req, res) => {
dataService.deleteEit(req.params.id, (code, err, data) => {
if (err) {
if (code) {
return res.status(code).json(err);
}
return res.status(500).json(err);
}
res.json(data);
})
}
exports.ping = (req, res) => {
const secret = 'aasdjakdbajsavjxvasgtcvascacscsacasdsavsav';
const data = Date.now().toString();
const hash = crypto.createHash('sha256').update(data + secret).digest('hex').substr(0, 24);
res.json({ hash });
}