-
Notifications
You must be signed in to change notification settings - Fork 0
/
BD1.5_Assignment_1.js
67 lines (59 loc) · 1.74 KB
/
BD1.5_Assignment_1.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
let express = require("express");
let core = require("cors");
let app = express();
app.use(core());
let port = 3000;
// Endpoint 1
app.get("/cart-total", (req, res) => {
let newItemPrice = parseFloat(req.query.newItemPrice);
let cartTotal = parseFloat(req.query.cartTotal);
let cart = newItemPrice + cartTotal;
res.send(cart.toString());
});
// Endpoint 2
app.get("/membership-discount", (req, res) => {
let cartTotal = parseFloat(req.query.cartTotal);
let isMember = req.query.isMember === "true";
if (isMember) {
let discount = cartTotal - cartTotal * 0.1;
res.send(discount.toString());
} else {
res.send(cartTotal.toString());
}
});
// Endpoint 3
app.get("/calculate-tax", (req, res) => {
let cartTotal = parseFloat(req.query.cartTotal);
let tax = cartTotal * 0.05;
res.send(tax.toString());
});
// Endpoint 4
app.get("/estimate-delivery", (req, res) => {
let shippingMethod = req.query.shippingMethod;
let distance = parseFloat(req.query.distance);
let result = "";
if (shippingMethod === "normal") {
result = distance * 0.02;
} else if (shippingMethod === "express") {
result = distance * 0.01;
} else {
result = "Invali shipping method";
}
res.send(result.toString());
});
// Endpoint 5
app.get("/shipping-cost", (req, res) => {
let weight = parseFloat(req.query.weight);
let distance = parseFloat(req.query.distance);
let shippingMethod = weight * distance * 0.1;
res.send(shippingMethod.toString());
});
// Endpoint 6
app.get("/loyalty-points", (req, res) => {
let purchaseAmount = parseFloat(req.query.purchaseAmount);
let loyaltyPoints = purchaseAmount * 2;
res.send(loyaltyPoints.toString());
});
app.listen(port, () => {
console.log("Server is running on port: " + port);
});