-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (66 loc) · 1.93 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
const express = require("express");
const db = require("./db/db.js");
const app = express();
app.use(express.json());
app.post('/order', async (req,res) => {
const order = req.body;
const [isValid, valmsg] = isOrderValid(order);
if(!isValid){
const err = "ERROR: " + valmsg;
console.log(err);
res.status(400).send(err);
return;
}
try {
await db.newOrder(order);
const resBody = await db.getOrderById(order.id);
res.status(201).send(resBody);
console.log("New order created");
}
catch(err) {
res.status(400).send(err);
console.log(err);
}
});
app.get('/orderbook', async (req,res) => {
try {
const result = await db.getOrderBook();
res.status(200).send(result);
console.log("Orderbook sent");
}
catch(err) {
res.status(400).send(err);
console.log(err);
}
});
app.get('/order/:id', async (req,res) => {
try {
const id = req.params.id;
const order = await db.getOrderById(id);
res.status(200).send(order);
}
catch(err){
res.status(400).send(err + "");
}
});
app.delete('/order/all', async (req,res) => {
try {
await db.deleteAll();
console.log("Deleting all data...");
res.status(200).send("Deleted all data");
}
catch(err) {
console.error(err);
}
});
db.configureTables().then(() => {
app.listen(3000, () => {
console.log('Listening on port 3000...');
});
});
const isOrderValid = (order) => {
if(order.currencyPair != "BTCUSD") return [false,"Currency pair is invalid.\nValid currencies are: BTCUSD"];
if(order.type !== "BUY" && order.type !== "SELL") return [false, "Order type is invalid.\nValid types are: BUY,SELL"];
if(order.price < 0 || order.quantity < 0) return [false, "Order price and order quantity musn't be negative"];
return [true, "Order is valid"];
}