diff --git a/helpers/getCompanyNameAndLogo.js b/helpers/getCompanyNameAndLogo.js new file mode 100644 index 0000000..cd01c71 --- /dev/null +++ b/helpers/getCompanyNameAndLogo.js @@ -0,0 +1,21 @@ +// Get Stock Company symbol Function +const axios = require("axios"); + +module.exports = async function getCompanyNameAndLogo(symbol) { + + let data = await axios + .get( + `https://www.alphavantage.co/query?function=OVERVIEW&symbol=${symbol}&apikey=${process.env.ALPHA_VANTAGE_KEY}` + ) + .then(async (resp) => { + let companyName = resp.data.Name; + console.log(companyName); + let logoSrc = await axios.get(`https://autocomplete.clearbit.com/v1/companies/suggest?query=:${companyName.split(' ')[0]}`).then((resp) => resp.data[0].logo).catch(err => console.log(err)); + return { companyName, logoSrc }; + }) + .catch((err) => { + console.log(err); + }); + + return data; +}; diff --git a/routes/api/cart.js b/routes/api/cart.js index 5f5f3ee..3324d45 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -5,6 +5,7 @@ const { ensureAuth } = require("../../middleware/auth"); const User = require("../../models/User"); const getPrice = require("../../helpers/getPrice"); +const getCompanyNameAndLogo = require("../../helpers/getCompanyNameAndLogo"); // TODO // Stocks Cart Buy/Sell @@ -16,9 +17,9 @@ const getPrice = require("../../helpers/getPrice"); // @route GET /cart/:symbol router.get("/:symbol", ensureAuth, async (req, res) => { const symbol = req.params.symbol; - const price = await getPrice(symbol); - res.status(200).render("cart", { layout: "layouts/app", symbol, price, href: '/market', avatar: req.user.image }); - + const { latestPrice } = await getPrice(symbol); + const { companyName, logoSrc } = await getCompanyNameAndLogo(symbol); + res.status(200).render("cart", { layout: "layouts/app", symbol, latestPrice, logoSrc, companyName, href: '/market', avatar: req.user.image }); }); // @desc To buy @@ -26,14 +27,11 @@ router.get("/:symbol", ensureAuth, async (req, res) => { router.post("/buy", ensureAuth, async (req, res) => { let data = req.body; let user = req.user; - let stockPrice = req.body.stockPrice; - let noOfStock = req.body.noOfStock; - let totalAmount = stockPrice * noOfStock; + let stockPrice = data.stockPrice; + let noOfStock = data.noOfStock; + let totalAmount = parseFloat(stockPrice * noOfStock).toFixed(4); try { - console.log(req.user); - console.log(req.body); - // req.body.user = req.user.id if (totalAmount > req.user.balance) { let ExtraBalance = totalAmount - req.user.balance; res.render("transaction", { diff --git a/views/cart.ejs b/views/cart.ejs index 3be7e79..87780ae 100644 --- a/views/cart.ejs +++ b/views/cart.ejs @@ -1,106 +1,73 @@ -
-

Buy Stock

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Product - Qtd - - Total price
- -

Earphone

-
- -
-
-
-
-
- -
+
+
+

Buy Stock

+
+
+
+
+
+ Company Name +
+
+
+ +

<%= companyName %>

+
+
+ + +
+ Qty +
+ +
+
+
+
-
- - 20.00€ - -
-

Tesla Model 3

- - - -
-
-
- -
-
-
- - 148,800.03€ - -
-

Bic 4 colour pen

-
- -
-
-
-
- -
-
- -
- - 7.50€ - -
-
\ No newline at end of file +
+ +
+ Total Price +
+
+

+ + <%= latestPrice %> + $ +

+
+ +
+ Back + to Market + +
+ + + + \ No newline at end of file diff --git a/views/market.ejs b/views/market.ejs index c091258..c7aaa19 100644 --- a/views/market.ejs +++ b/views/market.ejs @@ -1,9 +1,9 @@ -
+

Favorite Stocks

-
+
<% for(var i=0; i < compactData.length; i++) { %> @@ -40,17 +40,17 @@ -
+

Trending Stocks

-
+
<% for(var i=min; i < max; i++) { %> -
-
-
+
+
+
@@ -59,8 +59,8 @@

<%- totalData[i]["Company Name"] %>

-
- diff --git a/views/partials/_nav.ejs b/views/partials/_nav.ejs index 43b01f1..2303413 100644 --- a/views/partials/_nav.ejs +++ b/views/partials/_nav.ejs @@ -1,57 +1,61 @@
\ No newline at end of file diff --git a/views/portfolio.ejs b/views/portfolio.ejs index 7ac5b86..ea86d89 100644 --- a/views/portfolio.ejs +++ b/views/portfolio.ejs @@ -1,5 +1,6 @@ -
+
+
@@ -518,7 +519,7 @@
Load more
- +
\ No newline at end of file