forked from webix-hub/gantt-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
235 lines (204 loc) · 4.8 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
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
const cors = require("cors");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(cors());
// parsing application/json
app.use(bodyParser.json());
// parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
const port = process.env.APP_PORT || 3000;
app.listen(port, function () {
console.log("Server is running on port " + port + "...");
console.log(`Open http://localhost:${port} in browser`);
});
const Datastore = require('nedb-promises');
const tasks = new Datastore();
tasks.insert(require("./tasks.json"));
const links = new Datastore();
links.insert(require("./links.json"));
const resources = new Datastore();
resources.insert(require("./resources.json"));
const categories = new Datastore();
categories.insert(require("./categories.json"));
const assignments = new Datastore();
assignments.insert(require("./assignments.json"));
// client side expects obj.id while DB provides obj._id
function fixID(a){
a.id = a._id;
delete a._id;
return a;
}
const taskAttributes = {
start_date: 1, end_date: 1, text: 1, progress:2, duration: 1, parent:1, details:1, opened:2, type:1, position:2
};
function safeTaskAttributes(update, obj){
for (const key in obj){
const type = taskAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
const linkAttributes= {
target:1, source:1, type:2
};
function safeLinkAttributes(update, obj){
for (const key in obj){
const type = linkAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
const assignmentAttributes= {
task:1, resource:1, value:2
};
function safeAssignmentAttributes(update, obj){
for (const key in obj){
const type = assignmentAttributes[key];
if (type === 1) update[key] = obj[key];
else if (type === 2) update[key] = obj[key]*1;
}
return update;
}
async function deleteTask(id){
const data = await tasks.find({ parent:id });
await Promise.all(data.map(a => deleteTask(a._id)));
await links.remove({ $or: [
{ source: id },
{ target: id }
]},{
multi:true
});
await assignments.remove({ task: id },{
multi:true
});
await tasks.remove({ _id: id });
}
app.get("/tasks", async (req, res, next) => {
try {
const data = await tasks.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/tasks/:id", async (req, res, next) => {
try {
await tasks.update(
{ _id: req.params.id },
{ $set: safeTaskAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.delete("/tasks/:id", async (req, res, next) => {
try {
await deleteTask(req.params.id);
res.send({});
} catch(e){
next(err);
}
});
app.post("/tasks", async (req, res, next) => {
try {
const data = await tasks.insert(safeTaskAttributes({
progress:0, parent:0, text:"New Task"
},req.body));
res.send({ id: fixID(data).id });
} catch(e){
next(err);
}
});
app.get("/links", async (req, res, next) => {
try {
const data = await links.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/links/:id", async (req, res, next) => {
try {
await links.update(
{ _id: req.params.id },
{ $set: safeLinkAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.delete("/links/:id", async (req, res, next) => {
try {
await links.remove({ _id: req.params.id });
res.send({});
} catch (err) {
next(err);
}
});
app.post("/links", async (req, res, next) => {
try {
const data = await links.insert(safeLinkAttributes({}, req.body));
res.send({ id: fixID(data).id });
} catch (err) {
next(err);
}
});
app.get("/resources", async (req, res, next) => {
try {
const data = await resources.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.get("/categories", async (req, res, next) => {
try {
const data = await categories.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.get("/assignments", async (req, res, next) => {
try {
const data = await assignments.find({});
res.send(data.map(fixID));
} catch (err) {
next(err);
}
});
app.put("/assignments/:id", async (req, res, next) => {
try {
await assignments.update(
{ _id: req.params.id },
{ $set: safeAssignmentAttributes({}, req.body) },
{}
);
res.send({});
} catch (err) {
next(err);
}
});
app.delete("/assignments/:id", async (req, res, next) => {
try {
await assignments.remove({ _id: req.params.id });
res.send({});
} catch (err) {
next(err);
}
});
app.post("/assignments", async (req, res, next) => {
try {
const data = await assignments.insert(safeAssignmentAttributes({}, req.body));
res.send({ id: fixID(data).id });
} catch (err) {
next(err);
}
});