-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
91 lines (83 loc) · 2.7 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
import express from 'express';
import path from 'path';
import {
KendoUIAppointment,
KendoUIResource
} from './models/index.js';
global.__dirname = path.resolve();
const port = 1337;
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// Middleware to parse application/x-www-form-urlencoded data
app.use(express.urlencoded({ extended : true }));
app.get('/api/appointments/get', async(req, res) => {
try {
const appointments = await KendoUIAppointment.findAll();
res.status(200).json(appointments);
}
catch (e) {
console.error(e);
res
.status(500)
.json({ message : 'There was an error fetching the appointments' });
}
});
app.post('/api/appointments/sync', async(req, res) => {
const { created, updated, destroyed } = req.body;
const returnData = { created : [], updated : [] };
try {
if (created) {
for (const appointment of created) {
const { meetingID, ...data } = appointment;
const newAppointment = await KendoUIAppointment.create(data);
returnData.created.push(newAppointment);
}
}
if (updated) {
for (const appointment of updated) {
const { meetingID, roomId, isAllDay, ...data } = appointment;
await KendoUIAppointment.update(
{ ...data, roomId : parseInt(roomId), isAllDay : isAllDay === 'true' },
{
where : {
meetingID : parseInt(meetingID)
}
}
);
const newAppointment = await KendoUIAppointment.findByPk(meetingID);
returnData.created.push(newAppointment);
}
}
if (destroyed) {
for (const appointment of destroyed) {
await KendoUIAppointment.destroy({
where : {
meetingID : appointment.meetingID
}
});
}
}
res.status(200).json(returnData);
}
catch (e) {
console.error(e);
res.status(500).json('There was an error syncing the appointment changes');
}
});
app.get('/api/resources/get', async(req, res) => {
try {
const resources = await KendoUIResource.findAll();
res.status(200).json(resources);
}
catch (e) {
console.error(e);
res
.status(500)
.json({ message : 'There was an error fetching the resources' });
}
});
// Start server
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});