-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice.js
51 lines (36 loc) · 1.22 KB
/
service.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
var express = require("express");
var cors = require("cors");
var app = express();
let rabbitMQ = require('./rabbitMQ');
let stockData = require('./stockData');
app.use(cors({
exposedHeaders: ['Content-Length', 'Content-Type'],
}));
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.get("/getstockData", function (req, res) {
//res.send(stockData.data);
res.send(stockData.data.sort((a, b) => a.name.localeCompare(b.name)))
})
app.post('/updatestockData', async (req, res) => {
try {
var updateStock = req.body;
findAndUpdateStock(updateStock);
console.log("StockData:"+JSON.stringify(stockData.data));
console.log("Stock:"+JSON.stringify(updateStock));
rabbitMQ("updateStock", JSON.stringify(updateStock));
return res.status(200).json({ status: "succesfully update" });
} catch (error) {
res.status(500).send(error);
}
})
function findAndUpdateStock(updateStock)
{
findStockIndex = stockData.data.findIndex(o => o.name === updateStock.name);
stockData.data.splice(findStockIndex,1);
stockData.data.push(updateStock);
}
app.listen(9480);