-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
155 lines (134 loc) · 3.85 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
const { v4: uuidv4 } = require('uuid');
const express = require('express');
const cors = require('cors');
const app = express();
const port = 3000;
app.use(cors());
app.use(express.json());
let appState = {
calls: {},
menu: {
items: {},
},
};
app.get('/', (req, res) => {
console.log("root");
console.log("root");
res.json({success: 'OK'});
});
app.get('/menu', (req, res) => {
console.log("getting info for the menu");
console.log('Menu: ',appState.menu);
res.json(Object.values(appState.menu.items));
});
app.post('/menu/items', (req, res) => {
console.log("adding item to menu");
console.log(req.body);
const item = req.body;
appState.menu.items[item.name] = item;
res.send("successfully added item to menu");
});
app.delete('/menu/items', (req, res) => {
console.log("clearing menu");
appState.menu.items = [];
res.send("successfully cleared the menu");
});
app.post('/calls', (req, res) => {
console.log("creating a call");
const id = uuidv4();
appState.calls[id] = {
id,
order: {
items: [],
}
};
console.log(id);
res.send(id);
});
app.get('/calls/:id', (req, res) => {
const id = req.params.id;
console.log("getting info for a call: ", id);
const call = appState.calls[id]
if (call) {
console.log(call);
res.json(call);
} else {
res.status(404).send('Call not found');
}
});
app.get('/calls/:id/order', (req, res) => {
const id = req.params.id;
console.log("getting info for a call order: ", id);
const call = appState.calls[id];
if (call) {
console.log(call.order);
res.json(call.order);
} else {
res.status(404).send('Call not found');
}
});
app.post('/calls/:id/order/items', (req, res) => {
const id = req.params.id;
const itemRequest = req.body.item;
console.log("updating order (adding item) to call ", id, itemRequest);
console.log(req.body);
console.log('state:', appState.calls)
if (appState.calls[id]) {
const call = appState.calls[id];
const menu = appState.menu;
if (!menu.items[itemRequest]) {
console.log('Item not on menu:', menu.items);
res.send("We were unable to submit this order as there were items requested that were not on the menu.");
} else {
const newItem = menu.items[itemRequest];
if (call.order) {
call.order.items.push(newItem);
} else {
call.order = {
items: [newItem],
};
}
console.log('Item added to order');
res.send("We were able to successfully add the item to the order!");
}
} else {
res.send("We were unable to add the item to the order as the specified call id does not exist.");
}
});
app.delete('/calls/:id/order/items', (req, res) => {
console.log("updating order (removing item)");
console.log(req.body);
const id = req.params.id;
const itemRequest = req.body.item;
if (appState.calls[id]) {
const call = appState.calls[id];
if (call.order) {
const index = call.order.items.findIndex(item => item.name === itemRequest);
if (index !== -1) {
call.order.items.splice(index, 1);
res.send("We were able to successfully remove the item from the order!");
} else {
res.send("That item was not in the order.");
}
} else {
res.send("No order exists for this call.");
}
} else {
res.send("We were unable to remove the item from the order as the specified call id does not exist.");
}
});
app.delete('/calls/:id/order', (req, res) => {
console.log("clearing a call order");
const id = req.params.id;
if (appState.calls[id]) {
const call = appState.calls[id];
call.order = null;
res.send("successfully cleared the call's order");
} else {
res.status(404).send('Call not found');
}
});
const server = app.listen(port, () => {
console.log(`Server is running on http://0.0.0.0:${port}`);
});
module.exports = { app, server };