-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
193 lines (153 loc) · 3.99 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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
// Schema
require('./models/Order');
require('./models/Item');
require('./models/Drink');
// MongoDB connection
mongoose.connect('mongodb://localhost/feast');
mongoose.connection
.once('open', () => console.log('Good to go!'))
.on('error', error => {
console.warn('Warning', error);
});
const Order = mongoose.model('orders');
const Item = mongoose.model('items');
const Drink = mongoose.model('drinks');
const app = express();
app.use(bodyParser.json());
// Routes
// Order routes
app.get('/api', (req, res) => {
res.send({ hi: 'there' });
});
app.get('/api/orders', async (req, res) => {
const orders = await Order.find({});
res.send(orders);
});
app.get('/api/orders/:orderId', async (req, res) => {
const { orderId } = req.params;
const order = await Order.findById(orderId);
res.send(order);
});
app.post('/api/orders', async (req, res) => {
const { name, notes, order } = req.body;
const items = [];
for (const key in order) {
if (order[key].count > 0) {
items.push({
name: key,
count: order[key].count,
price: order[key].price
});
}
}
const newOrder = new Order({
name,
items,
notes: notes || null,
created: new Date(),
updated: new Date()
});
const savedNewOrder = await newOrder.save();
res.send(savedNewOrder);
});
app.put('/api/orders/:orderId', async (req, res) => {
const { method } = req.body;
const { orderId } = req.params;
let newStatus;
const foundOrder = await Order.findById(orderId);
if (foundOrder.status < 4) {
Order.findByIdAndUpdate(
orderId,
{
$inc: { status: 1 },
updated: new Date()
},
{ new: true },
(err, updatedOrder) => {
res.send(updatedOrder);
}
);
}
});
// Item routes
app.get('/api/items', async (req, res) => {
const items = await Item.find({});
res.send(items);
});
app.post('/api/items', async (req, res) => {
const { name, price, type, soup } = req.body;
const newItem = new Item({
name,
price,
type
});
const item = await newItem.save();
res.send(item);
});
app.put('/api/items/:itemId', async (req, res) => {
const { newPrice } = req.body;
const { itemId } = req.params;
const newItem = await Item.findByIdAndUpdate(itemId, { price: newPrice });
res.send(newItem);
});
// Drinks routes
app.get('/api/drinks', async (req, res) => {
const drinks = await Drink.find({});
res.send(drinks);
});
app.post('/api/drinks', async (req, res) => {
const { name, price } = req.body;
const drink = new Drink({
name,
price
});
const newDrink = await drink.save();
res.send(newDrink);
});
// Statistics Routes
app.get('/api/stats', (req, res) => {
const stats = {};
Order.find({}).then(orders => {
orders.forEach(order => {
const weekday = new Array(7);
weekday[0] = 'Monday';
weekday[1] = 'Tuesday';
weekday[2] = 'Wednesday';
weekday[3] = 'Thursday';
weekday[4] = 'Friday';
weekday[5] = 'Saturday';
weekday[6] = 'Sunday';
const date = new Date(order.created);
const day = weekday[date.getDay()];
if (!stats[day]) {
stats[day] = {};
}
order.items.forEach(item => {
if (stats[day][item[0].name]) {
stats[day][item[0].name].count += item[0].count;
} else {
stats[day][item[0].name] = {
name: item[0].name,
count: 1,
price: item[0].price
};
}
});
const itemKeys = Object.keys(stats[day]);
stats[day].total = 0;
itemKeys.forEach(itemKey => {
console.log(itemKey);
const itemTotal = stats[day][itemKey].price * stats[day][itemKey].count;
if (itemKey !== 'total') {
stats[day].total += itemTotal;
}
});
});
res.send(stats);
});
});
// PORT
app.listen(5000, () => console.log('Listening on port 5000'));