Skip to content

Commit

Permalink
backend assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishant Rai committed Dec 21, 2024
0 parents commit 086429d
Show file tree
Hide file tree
Showing 10 changed files with 1,699 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
13 changes: 13 additions & 0 deletions config/db.js
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);
}
}
54 changes: 54 additions & 0 deletions controllers/sharePriceController.js
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"
}
);
}
}
25 changes: 25 additions & 0 deletions index.js
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"
}
)
});
36 changes: 36 additions & 0 deletions model/SharePrice.model.js
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;
Loading

0 comments on commit 086429d

Please sign in to comment.