-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
217 lines (191 loc) · 6.03 KB
/
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
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
import bodyParser from 'body-parser';
import express from 'express';
import path from 'path';
import { Event, Resource, ResourceGroup } from './models/index.js';
import { localDateTimeISOString } from './utils.js';
global.__dirname = path.resolve();
const port = 1337;
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.get('/api/events', async(req, res) => {
try {
const events = await Event.findAll();
const dataValues = events.map((event) => event.dataValues);
const data = dataValues.map((event) => ({
...event,
start : localDateTimeISOString(event.start),
end : localDateTimeISOString(event.end)
}));
res.status(200).json(data);
}
catch (e) {
console.error(e);
res.status(500).json({ message : 'There was an error fetching the events' });
}
});
app.get('/api/resources', async(req, res) => {
try {
const resourcesPromise = Resource.findAll({ order : [['ordinal', 'ASC']] });
const resource_groupsPromise = ResourceGroup.findAll({
order : [['ordinal', 'ASC']]
});
const [resources, resource_groups] = await Promise.all([
resourcesPromise,
resource_groupsPromise
]);
const resourcesData = resource_groups.map((g) => {
const newG = { ...g.dataValues };
(newG.expanded = true),
(newG.children = resources.filter((r) => r.group_id === g.id));
return newG;
});
resource_groups.forEach((g) => {
g.expanded = true;
g.children = resources.filter((r) => r.group_id === g.id);
});
res.status(200).json(resourcesData);
}
catch (e) {
console.error(e);
res
.status(500)
.json({ message : 'There was an error fetching the resources' });
}
});
app.post('/api/createEvent', async(req, res) => {
try {
const data = await Event.create(req.body);
const resData = {
...data.dataValues,
start : localDateTimeISOString(data.dataValues.start),
end : localDateTimeISOString(data.dataValues.end)
};
res.status(200).json(resData);
}
catch (e) {
console.error(e);
res.status(500).json({ message : 'There was an error creating the event' });
}
});
app.post('/api/createGroup', async(req, res) => {
try {
const maxOrdinal = await ResourceGroup.max('ordinal');
const modBody = { ...req.body, ordinal : maxOrdinal + 1 };
const data = await ResourceGroup.create(modBody);
res.status(200).json(data.dataValues);
}
catch (e) {
console.error(e);
res
.status(500)
.json({ message : 'There was an error creating the resource group' });
}
});
app.post('/api/createResource', async(req, res) => {
try {
const maxOrdinal = await ResourceGroup.max('ordinal');
const modBody = { ...req.body, ordinal : maxOrdinal + 1 };
const data = await Resource.create(modBody);
res.status(200).json(data.dataValues);
}
catch (e) {
console.error(e);
res
.status(500)
.json({ message : 'There was an error creating the resource' });
}
});
app.post('/api/deleteGroup', async(req, res) => {
const { id } = req.body;
try {
await ResourceGroup.destroy({
where : {
id : id
}
});
res.status(200).json({});
}
catch (e) {
console.error(e);
res.status(500).json('There was an error deleting the resource group');
}
});
app.post('/api/deleteResource', async(req, res) => {
const { id } = req.body;
try {
await Resource.destroy({
where : {
id : id
}
});
res.status(200).json({});
}
catch (e) {
console.error(e);
res.status(500).json('There was an error deleting the resource');
}
});
app.post('/api/deleteEvent', async(req, res) => {
const { id } = req.body;
try {
await Event.destroy({
where : {
id : id
}
});
res.status(200).json({});
}
catch (e) {
console.error(e);
res.status(500).json('There was an error deleting the event');
}
});
app.post('/api/updateResource', async(req, res) => {
const { id, name } = req.body;
try {
await Resource.update({ name : name }, { where : { id } });
res.status(200).json({ success : true });
}
catch (e) {
console.error(e);
res.status(500).json('There was an error updating the resource');
}
});
app.post('/api/updateGroup', async(req, res) => {
const { id, name } = req.body;
try {
await ResourceGroup.update({ name : name }, { where : { id } });
res.status(200).json({ success : true });
}
catch (e) {
console.error(e);
res.status(500).json('There was an error updating the resource group');
}
});
app.post('/api/moveEvent', async(req, res) => {
const { id, ...data } = req.body;
try {
await Event.update(data, { where : { id } });
res.status(200).json({ success : true });
}
catch (e) {
console.error(e);
res.status(500).json('There was an error moving the event');
}
});
app.post('/api/updateEvent', async(req, res) => {
const { id, text } = req.body;
try {
await Event.update({ text : text }, { where : { id } });
res.status(200).json({ success : true });
}
catch (e) {
console.error(e);
res.status(500).json('There was an error updating the event');
}
});
// Start server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});