-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapi.ts
99 lines (72 loc) · 2.19 KB
/
api.ts
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
// import e from 'express';
import express from 'express';
import * as uuid from 'uuid';
import { Order, User, Driver } from './types';
const router = express.Router();
// Our database, for now!
const users = new Map<string, User>();
const orders = new Map<string, Order>();
const drivers = new Map<string, Driver>();
// Manually add a driver, user, and order
const driverOne: Driver = {
id: '3a9bf929-824a-4864-9c45-effb2bc49d2d',
username: 'awesomeDriver',
name: 'Nikhil Dange',
car: 'Toyota Camry',
}
drivers.set(driverOne.id, driverOne);
const userOne: User = {
id: 'e1414613-f671-4ad1-85d7-fb5227f856eb',
username: 'lebronjames123',
name: 'Lebron James',
card: 'Discover Student',
balance: 100,
}
users.set(userOne.id, userOne);
const testOrder: Order = {
id: '407d7b6b-08d0-4e29-b1fd-e9832cec8c54',
name: "Chicken Pad Thai",
restaurant: "Blue Pepper",
quantity: 1,
instructions: 'Leave on front porch.',
cost: 15,
owner: 'e1414613-f671-4ad1-85d7-fb5227f856eb',
status: 'Placed'
}
orders.set(testOrder.id, testOrder);
// GET all existing orders
router.get('/orders', (req, res) => {
const currentOrders = Array.from(orders.values());
return res.status(200).json({ orders: currentOrders });
});
// GET a specific order
router.get('/order/:id', (req, res) => {
const { id } = req.params;
const order = orders.get(id);
// Check the database to ensure the order exists
if (order != null) {
return res.status(200).json({ order });
}
else {
return res.status(404).json({ error: "Oops! That order can't be found 🤪"});
}
});
// POST a new order
router.post('/order', (req, res) => {
// TODO
const order: Order = req.body.order;
// 1. Obtain the user's balance
// 2. Validate if the balance is too low
// 3. Create a uuid for the order, and set the order as placed
// 4. Update user balance and database
});
router.post('/order/:id/accept', (req, res) => {
// TODO
// 1. Obtain the order from our database
// 2. Check if the order status is valid for acceptance
// 3. Mark the status as accepted and update the database
});
router.post('/user', (req, res) => {
// TODO
});
export default router;