-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.js
83 lines (63 loc) · 2.11 KB
/
api.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
const express = require('express');
const uuid = require('uuid');
const { getConsumer, getProducer } = require('./kafka');
const { getCostForOrder } = require('./services/moneyService');
const Customer = require('./models/customer');
const Driver = require('./models/driver');
const Order = require('./models/order');
const Redis = require('ioredis');
const redis = new Redis(6379);
const router = express.Router();
// Create a customer (user)
router.post('/customer', async (req, res, next) => {
const { customer } = req.body;
customer.id = uuid.v4();
customer.balance = 100;
try {
const createdCustomer = await Customer.create(customer);
res.status(200).json({ customer: createdCustomer });
} catch (e) {
res.status(500).json({ error: e.message })
}
});
// Place an order
router.post('/customer/order', async (req, res, next) => {
const { fromLocation, toLocation, customerId, restaurant, orderDetails } = req.body;
// 1) TODO: Check if the cost of the order is in the Redis cache
// If not, calculate the cost and add it to the cache
const totalCost = 0;
const orderRequest = {
id: uuid.v4(),
fromLocation,
toLocation,
customerId: customerId,
driverId: null,
restaurant,
cost: totalCost,
orderDetails,
orderStatus: 'Pending',
};
const producer = await getProducer();
// 1) TODO: Publish the order to the 'orders' topic with the order data
console.log("Sent message to orders message queue");
res.status(201).json({ order: orderRequest });
});
// Create a driver
router.post('/driver', async (req, res, next) => {
const { driver } = req.body;
driver.id = uuid.v4();
try {
const createdDriver = await Driver.create(driver);
res.status(200).json({ driver: createdDriver });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// Driver accepts an order
router.post('/driver/order', async (req, res, next) => {
const { driverId } = req.body;
const consumer = await getConsumer();
// TODO: subscribe to the 'orders' topic
// complete logic for what happens with the order is consumed
});
module.exports = router;