Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: live check on stock-price-checker #543

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 52 additions & 52 deletions apps/stock-price-checker/routes/api.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
/*
*
*
* Complete the API routing below
*
*
*/
*
*
* Complete the API routing below
*
*
*/

'use strict';

var StockHandler = require('../controllers/stockHandler.js');

module.exports = function (app) {

var stockPrices = new StockHandler();

app.route('/api/stock-prices')
.get(function (req, res){
var stock = req.query.stock;
var like = req.query.like || false;
var requestIP = req.get('x-forwarded-for') || req.ip;
var reqIP = requestIP.split(",")[0];
reqIP = reqIP.replace(/\.\d*$/, '.0'); // ipv4
reqIP = reqIP.replace(/[\da-f]*:[\da-f]*$/i, '0:0'); // ipv6
var stockData = null;
var likeData = null;
var multiple = false;
if (Array.isArray(stock)) {
multiple = true;
stockData = [];
likeData = [];
app.route('/status/ping').get(function (req, res) {
res.send({ msg: 'pong' }).status(200);
});

app.route('/api/stock-prices').get(function (req, res) {
var stock = req.query.stock;
var like = req.query.like || false;
var requestIP = req.get('x-forwarded-for') || req.ip;
var reqIP = requestIP.split(',')[0];
reqIP = reqIP.replace(/\.\d*$/, '.0'); // ipv4
reqIP = reqIP.replace(/[\da-f]*:[\da-f]*$/i, '0:0'); // ipv6
var stockData = null;
var likeData = null;
var multiple = false;
if (Array.isArray(stock)) {
multiple = true;
stockData = [];
likeData = [];
}
function sync(finished, data) {
if (finished == 'stockData') {
multiple ? stockData.push(data) : (stockData = data);
} else {
multiple ? likeData.push(data) : (likeData = data);
}
function sync(finished, data) {
if (finished == 'stockData') {
(multiple) ? stockData.push(data) : stockData = data;

if (!multiple && stockData && likeData !== null) {
stockData.likes = likeData.likes;
res.json({ stockData });
} else if (multiple && stockData.length == 2 && likeData.length == 2) {
if (stockData[0].stock == likeData[0].stock) {
stockData[0].rel_likes = likeData[0].likes - likeData[1].likes;
stockData[1].rel_likes = likeData[1].likes - likeData[0].likes;
} else {
(multiple) ? likeData.push(data) : likeData = data;
stockData[0].rel_likes = likeData[1].likes - likeData[0].likes;
stockData[1].rel_likes = likeData[0].likes - likeData[1].likes;
}

if (!multiple && stockData && likeData !== null) {
stockData.likes = likeData.likes;
res.json({stockData});
} else if (multiple && stockData.length == 2 && likeData.length == 2) {
if (stockData[0].stock == likeData[0].stock) {
stockData[0].rel_likes = likeData[0].likes - likeData[1].likes;
stockData[1].rel_likes = likeData[1].likes - likeData[0].likes;
} else {
stockData[0].rel_likes = likeData[1].likes - likeData[0].likes;
stockData[1].rel_likes = likeData[0].likes - likeData[1].likes;
}
res.json({stockData});
}
}
if (multiple) {
stockPrices.getData(stock[0], sync);
stockPrices.loadLikes(stock[0], like, reqIP, sync);
stockPrices.getData(stock[1], sync);
stockPrices.loadLikes(stock[1], like, reqIP, sync);
} else {
stockPrices.getData(stock, sync);
stockPrices.loadLikes(stock, like, reqIP, sync);
res.json({ stockData });
}

});

}
if (multiple) {
stockPrices.getData(stock[0], sync);
stockPrices.loadLikes(stock[0], like, reqIP, sync);
stockPrices.getData(stock[1], sync);
stockPrices.loadLikes(stock[1], like, reqIP, sync);
} else {
stockPrices.getData(stock, sync);
stockPrices.loadLikes(stock, like, reqIP, sync);
}
});
};