-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (128 loc) · 4.7 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
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
// Imports
const express = require('express');
const { JsonDB } = require('node-json-db');
const { Config } = require('node-json-db/dist/lib/JsonDBConfig');
// DB file name, auto save after push, db human readable format, separator
var db = new JsonDB(new Config("myDataBase", true, false, '~'));
// Initialise the app
const app = express();
// middlewares..
// security checks..
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Sends message for default URL
app.get('/', (req, res) => res.json({ message: 'Hola!' }));
// CRUD employee
app.get('/v1/employees/', (req, res) => {
let result = "";
try {
result = db.getData("~employees");
} catch (error) {
res.json({ message: "No Data Found!", error: true });
return;
}
res.json({ data: result });
});
app.get('/v1/employees/:id', (req, res) => {
const id = req.params.id;
if (!id) {
res.json({ message: "ID required!", error: true });
return;
}
try {
const employees = db.getData("~employees");
let result = employees.find((emp) => emp.id == id);
if (result != undefined) {
res.json({ data: result });
} else {
res.json({ message: `Employee Not found!`, error: true });
}
} catch (error) {
console.log(error);
res.json({ message: "Employee not found!", error: true });
return;
}
});
app.post('/v1/employees/', (req, res) => {
//console.log(JSON.stringify(req.body));
const name = req.body.name;
const role = req.body.role;
const age = req.body.age;
const gender = req.body.gender;
if (name != undefined && role != undefined && age != undefined && gender != undefined) {
try {
const employees = db.getData("~employees");
const idx = employees.findIndex((emp) => emp.name == name);
if (idx == -1) {
const emp_object = {
id: (employees.length) + 1, name: name, role: role, age: age, gender: gender
};
db.push("~employees[]", emp_object, true);
res.json({ data: emp_object });
} else {
res.json({ message: "Employee already exists!", error: true });
}
} catch (error) {
res.json({ message: "DB operation failed!", error: true });
}
} else res.json({ message: "Required fields are missing", error: true });
});
app.put('/v1/employees/:id', (req, res) => {
const id = req.params.id;
const name = req.body.name;
const role = req.body.role;
const age = req.body.age;
const gender = req.body.gender;
if (id != undefined && name != undefined && role != undefined && age != undefined && gender != undefined) {
try {
const employees = db.getData("~employees");
const idx = employees.findIndex((emp) => emp.id == id);
if (idx != -1) {
const emp_object = {
id: id, name: name, role: role, age: age, gender: gender
};
db.push("~employees[" + idx + "]", emp_object, true);
res.json({ data: emp_object });
} else {
res.json({ message: "Employee not found!", error: true });
}
} catch (error) {
res.json({ message: "DB operation failed!", error: true });
}
} else res.json({ message: "Required fields are missing", error: true });
});
app.patch('/v1/employees/:id', (req, res) => {
const id = req.params.id;
// pending...
});
app.delete('/v1/employees/:id', (req, res) => {
const id = req.params.id;
if (!id) {
res.json({ message: "ID required!", error: true });
return;
}
try {
const employees = db.getData("~employees");
const idx = employees.findIndex((emp) => emp.id == id);
if (idx != -1) {
db.delete('~employees[' + idx + ']');
res.json({ message: "Success", error: false })
} else {
res.json({ message: `Employee Not found!`, error: true });
}
} catch (error) {
res.json({ message: "DB operation failed!", error: true });
}
});
app.all('/', function (req, res) {
//Create an error and pass it to the next function
var err = new Error("Something went wrong");
next(err);
});
//An error handling middleware
app.use(function (err, req, res, next) {
res.status(500);
res.send("Oops, something went wrong.")
});
// Launch app to listen to specified port
app.listen(8666, () => console.log("Running on port 8666"));