-
Notifications
You must be signed in to change notification settings - Fork 0
/
BD1.5_Assignment_2.js
59 lines (52 loc) · 1.67 KB
/
BD1.5_Assignment_2.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
let express = require("express");
let cors = require("cors");
let app = express();
app.use(cors());
let port = 3000;
// Endpoint 1
app.get("/calculate-returns", (req, res) => {
let boughtAt = parseFloat(req.query.boughtAt);
let marketPrice = parseFloat(req.query.marketPrice);
let quantity = parseFloat(req.query.quantity);
let returns = (marketPrice - boughtAt) * quantity;
res.send(returns.toString());
});
// Endpoint 2
app.get("/total-returns", (req, res) => {
let stock1 = parseFloat(req.query.stock1);
let stock2 = parseFloat(req.query.stock2);
let stock3 = parseFloat(req.query.stock3);
let stock4 = parseFloat(req.query.stock4);
let totalStock = stock1 + stock2 + stock3 + stock4;
res.send(totalStock.toString());
});
// Endpoint 3
app.get("/calculate-return-percentage", (req, res) => {
let boughtAt = parseFloat(req.query.boughtAt);
let returns = parseFloat(req.query.returns);
let returnPercentage = (returns / boughtAt) * 100;
res.send(returnPercentage.toString());
});
// Endpoint 4
app.get("/total-return-percentage", (req, res) => {
let stock1 = parseFloat(req.query.stock1);
let stock2 = parseFloat(req.query.stock2);
let stock3 = parseFloat(req.query.stock3);
let stock4 = parseFloat(req.query.stock4);
let totalStock = stock1 + stock2 + stock3 + stock4;
res.send(totalStock.toString());
});
// Endpoint 5
app.get("/status", (req, res) => {
let returnPercentage = parseFloat(req.query.returnPercentage);
let result = "";
if (returnPercentage > 0) {
result = "profit";
} else {
result = "loss";
}
res.send(result.toString());
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});