-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added function to save stockHistory to the DB
- Loading branch information
1 parent
2345b92
commit 0e2369c
Showing
3 changed files
with
114 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters