Skip to content

Commit

Permalink
Added function to save stockHistory to the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelKaemper committed Oct 9, 2020
1 parent 2345b92 commit 0e2369c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 55 deletions.
42 changes: 42 additions & 0 deletions public/javascripts/functions/stocks/getStockDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fetch = require('node-fetch');
const secret = require('../secret.js');
const getStock = require('./getStock.js');
const query = require('../../database/dbquery')

const addDetails = async(name) => {
let price = await getStock.getStock(name);

// Get existing stockHistory
var existing = await query("SELECT history FROM stockhistory WHERE symbol='" + name + "';");
// Check if history exists
if (existing.length > 0) {
// Append to history
console.log(existing[0].history);
existing = JSON.parse(existing[0].history);
existing[name][new Date().toLocaleString()] = price;
await query("UPDATE stockhistory SET history='" + JSON.stringify(existing) + "' WHERE symbol='" + name + "';");
} else {
// Create if doesn't exist
existing = {};
existing[name] = {};
existing[name][new Date().toLocaleString()] = price;
await query("INSERT INTO stockhistory(symbol, history) VALUES ('" + name + "','" + JSON.stringify(existing) + "');");
}

console.log(price);
console.log(existing);
}

const getDetails = (name) => {
const url = "https://finnhub.io/api/v1/quote?symbol=" + name + "&token=" + secret._API._KEY;
return new Promise((resolve, reject) => {
fetch(url)
.then(res => res.json())
.then(data => resolve(data["c"]));
});
}

module.exports = {
getDetails,
addDetails
};
87 changes: 48 additions & 39 deletions routes/stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,64 @@ const stocks = require('../public/javascripts/functions/stocks/getStock.js');
const buyStock = require('../public/javascripts/functions/stocks/buyStock.js');
const loadStocks = require('../public/javascripts/functions/stocks/loadStocks.js');
const sellStock = require('../public/javascripts/functions/stocks/sellStock.js');
const stockDetails = require('../public/javascripts/functions/stocks/getStockDetails.js');

router.get('/', async(req, res, next) => {
await stdCall(req);
if (req.session.loggedIn) {
var ownedStocks = await loadStocks(req.session.uuid);
}
res.render('stocks/stocks', stdParameter(req, 'Stocks', { money: req.session.money, ownedStocks: ownedStocks, user: await getUserInfo(req) }));
await stdCall(req);
if (req.session.loggedIn) {
var ownedStocks = await loadStocks(req.session.uuid);
}
res.render('stocks/stocks', stdParameter(req, 'Stocks', { money: req.session.money, ownedStocks: ownedStocks, user: await getUserInfo(req) }));
});

router.post("/details", async(req, res, next) => {
await stdCall(req);
stockDetails.addDetails(req.body.symbol);
// res.render("/stock/stocks", stdParameter(req, 'Stocks'))
res.redirect("/")
});

router.post('/getStocks', async(req, res, next) => {
await stdCall(req);
let symbol = (req.body.stockName).toUpperCase();
let price = await stocks.getStock(symbol);
let company = await stocks.getCompanyName(symbol);
console.log(company);
res.render('stocks/stocks', stdParameter(req, 'Stocks',
{ buyable: parseInt(Math.floor(req.session.money / Math.round(price))),
price: parseInt(Math.round(price)),
company: company,
symbol: symbol,
user: await getUserInfo(req) }));
await stdCall(req);
let symbol = (req.body.stockName).toUpperCase();
let price = await stocks.getStock(symbol);
let company = await stocks.getCompanyName(symbol);
console.log(company);
res.render('stocks/stocks', stdParameter(req, 'Stocks', {
buyable: parseInt(Math.floor(req.session.money / Math.round(price))),
price: parseInt(Math.round(price)),
company: company,
symbol: symbol,
user: await getUserInfo(req)
}));
});

router.post('/buystock', async(req, res, next) => {
let count;
req.body.count <= 0 ? count = req.body.buyable : count = req.body.count;
await buyStock(req.session.uuid, req.body.symbol, Math.round(req.body.price), parseInt(count));
res.redirect('/stocks');
let count;
req.body.count <= 0 ? count = req.body.buyable : count = req.body.count;
await buyStock(req.session.uuid, req.body.symbol, Math.round(req.body.price), parseInt(count));
res.redirect('/stocks');
});

router.post('/sellstock', async(req, res, next) => {
if(!req.body.confirmed) {
// If no specific amount is set, sell all
req.body.amount <= 0 ? req.body.amount = req.body.count : req.body.amount = req.body.amount;
// Get the latest price & multiply to get the overall price
let newPrice = await stocks.getStock(req.body.symbol);
newPrice = parseInt(Math.round(newPrice)) * parseInt(req.body.amount);
//Render page with given parameters
res.render('stocks/sellstock', stdParameter(req, 'Sell Stocks', {
price: req.body.spent / req.body.count * req.body.amount,
symbol: req.body.symbol,
count: req.body.amount,
newprice: newPrice,
user: await getUserInfo(req)
}));
} else {
await sellStock(req.session.uuid, req.body.symbol, req.body.count, req.body.worth);
res.redirect('/stocks');
}
if (!req.body.confirmed) {
// If no specific amount is set, sell all
req.body.amount <= 0 ? req.body.amount = req.body.count : req.body.amount = req.body.amount;
// Get the latest price & multiply to get the overall price
let newPrice = await stocks.getStock(req.body.symbol);
newPrice = parseInt(Math.round(newPrice)) * parseInt(req.body.amount);
//Render page with given parameters
res.render('stocks/sellstock', stdParameter(req, 'Sell Stocks', {
price: req.body.spent / req.body.count * req.body.amount,
symbol: req.body.symbol,
count: req.body.amount,
newprice: newPrice,
user: await getUserInfo(req)
}));
} else {
await sellStock(req.session.uuid, req.body.symbol, req.body.count, req.body.worth);
res.redirect('/stocks');
}
});

module.exports = router;
module.exports = router;
40 changes: 24 additions & 16 deletions views/stocks/stocks.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,38 @@
<th>Spent</th>
</tr>
{{#each ownedStocks}}
<form method="post" action="stocks/sellstock">
<tr>
<form method="post" action="stocks/sellstock">
<td>
<input type="hidden" name="symbol" value="{{@key}}">
{{@key}}
</td>
<td>
<input type="hidden" name="count" value="{{this.count}}">
{{this.count}}
</td>
<td>
<input type="hidden" name="spent" value="{{this.price}}">
${{this.price}}
</td>
<td>
<input type="text" size="2" name="amount">
<input type="submit" value="Sell">
</td>
</form>
<td>
<input type="hidden" name="symbol" value="{{@key}}">
{{@key}}
</td>
<td>
<input type="hidden" name="count" value="{{this.count}}">
{{this.count}}
</td>
<td>
<input type="hidden" name="spent" value="{{this.price}}">
${{this.price}}
</td>
<td>
<input type="text" size="2" name="amount">
<input type="submit" value="Sell">
<form method="POST" action="stocks/details">
<input type="hidden" name="symbol" value="{{@key}}">
<input type="submit" value="Details">
</form>
</td>
</tr>
</form>
{{/each}}
</table>
</center>
{{/if}}
{{#if stockDetails}}
{{/if}}
{{else}}
You must be logged in to see this page!
{{/if}}

0 comments on commit 0e2369c

Please sign in to comment.