-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nishant Rai
committed
Dec 21, 2024
0 parents
commit 086429d
Showing
10 changed files
with
1,699 additions
and
0 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,2 @@ | ||
.env | ||
node_modules |
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,13 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
|
||
exports.connectTODB = async () => { | ||
try { | ||
const connection = await mongoose.connect(process.env.DB_URL); | ||
|
||
console.log("Connection to database was successfull"); | ||
|
||
} catch (e) { | ||
console.log(e); | ||
} | ||
} |
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,54 @@ | ||
const axios = require("axios"); | ||
const SharePrice = require("../model/SharePrice.model"); | ||
|
||
exports.sharePriceGetController = async (req, res) => { | ||
try { | ||
|
||
setInterval(async () => { | ||
try { | ||
|
||
const response = await axios.get(`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${req.query.company}&interval=5min&apikey=demo`); | ||
const company = req.query.company; | ||
const data = response.data["Time Series (5min)"]; | ||
|
||
let filteredData = []; | ||
for (val in data) { | ||
data[val].companyName = company; | ||
data[val]["1. time"] = String(val); | ||
filteredData.push(data[val]); | ||
} | ||
|
||
filteredData = filteredData.map((priceObj) => { | ||
const newPriceObj = {}; | ||
for (val in priceObj) { | ||
key = val.split(" ")[1]; | ||
newPriceObj[key] = priceObj[val]; | ||
} | ||
newPriceObj["companyName"] = company; | ||
delete newPriceObj[undefined]; | ||
return newPriceObj; | ||
}); | ||
const result = await SharePrice.insertMany(filteredData); | ||
|
||
} catch (error) { | ||
console.log("GET AXIOS FAILED", error); | ||
} | ||
|
||
}, 60000) | ||
|
||
return res.status(200).json( | ||
{ | ||
"message": "Price is being fetched every 1 minutes", | ||
}, | ||
); | ||
|
||
} catch (e) { | ||
console.log(["GET SHARE PRICE"], e); | ||
|
||
return res.status(500).json( | ||
{ | ||
"message": "Something went wrong" | ||
} | ||
); | ||
} | ||
} |
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,25 @@ | ||
const express = require("express"); | ||
const { connectTODB } = require("./config/db"); | ||
const mainRouter = require("./routes"); | ||
require("dotenv").config(); | ||
|
||
const PORT = process.env.PORT || 5000; | ||
const app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: true })); | ||
|
||
app.use("/api", mainRouter); | ||
|
||
app.listen(PORT, () => { | ||
console.log("server is running on port 5000"); | ||
connectTODB(); | ||
}); | ||
|
||
app.get("/", (req, res) => { | ||
return res.status(200).json( | ||
{ | ||
"status": "OK" | ||
} | ||
) | ||
}); |
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,36 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const SharePriceSchema = new mongoose.Schema( | ||
{ | ||
companyName: { | ||
type: String, | ||
required: true | ||
}, | ||
open: { | ||
type: Number, required: true | ||
}, | ||
high: { | ||
type: Number, required: true | ||
}, | ||
low: { | ||
type: Number, required: true | ||
}, | ||
close: { | ||
type: Number, required: true | ||
}, | ||
time: { | ||
type: String, | ||
required: true, | ||
}, | ||
volume: { | ||
type: Number, required: true | ||
} | ||
}, | ||
{ | ||
timestamps: true | ||
} | ||
); | ||
|
||
const SharePrice = mongoose.model("SharePrice", SharePriceSchema); | ||
|
||
module.exports = SharePrice; |
Oops, something went wrong.