-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (98 loc) · 3.52 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
const express = require('express');
const uuid = require('uuid');
const { showMethodNUrl, checkIdExistence, verifyClientData } = require('./middlewares');
const cors = require('cors');
const port = 3001;
const server = express();
server.use(express.json());
server.use(showMethodNUrl);
server.use(cors());
// orders made
const ordersList = [];
// route to request order
server.post('/order', verifyClientData, (req, res) => {
/* request(body) pattern:
{
"order": "X- Salada, 2 batatas grandes, 1 coca-cola",
"clienteName": "Talita",
"price": 44.50
}
*/
// receive data
const { order, clienteName, price } = req.body;
const registeredOrder = { id: uuid.v4(), order, clienteName, price, status: 'Em preparação' };
// verifying client post data
for (orderData in registeredOrder) {
if (!registeredOrder[orderData]) return res.status(400).json({ error: 'Missing Data' });
}
// trying to add new order to orders list and return
try {
ordersList.push(registeredOrder);
return res.status(201).json(registeredOrder);
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// route to list all orders
server.get('/order', (req, res) => {
try {
return res.json(ordersList);
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// route to get specific order (by id)
server.get('/order/:id', checkIdExistence, (req, res) => {
try {
return res.json({ order: ordersList[req.orderIndex] });
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// route to update an order (by id)
server.put('/order/:id', checkIdExistence, verifyClientData, (req, res) => {
// receiving order data
const { order, clienteName, price } = req.body;
const newData = { order, clienteName, price };
try {
// function to update one or more data from a order
for (let orderData in ordersList[req.orderIndex]) {
for (data in newData) {
if (newData[data] && data === orderData) ordersList[req.orderIndex][data] = newData[data];
}
}
return res.json({ updatedOrder: ordersList[req.orderIndex] });
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// route to update an order status (by id)
server.patch('/order/:id', checkIdExistence, (req, res) => {
try {
ordersList[req.orderIndex].status = 'Pronto';
return res.json({ order: ordersList[req.orderIndex] });
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
// route to delete an order (by id)
server.delete('/order/:id', checkIdExistence, (req, res) => {
try {
// deleting order from array
ordersList.splice(req.orderIndex, 1);
return res.status(204).json();
} catch (err) {
console.error(`Something happened: ${err.message}`);
return res.status(500).json({ error: err.message });
}
});
server.listen(process.env.PORT || port, () => {
console.log(`㊗️ - server started in port: ${port}.`);
});
//exporting orders list to middlewares.js
exports.ordersList = ordersList;