From 9fb5d84b8ae58519d2a4e8ed67df305f02a147de Mon Sep 17 00:00:00 2001 From: fabcodingzest <48706652+fabcodingzest@users.noreply.github.com> Date: Mon, 17 Aug 2020 22:49:06 +0530 Subject: [PATCH 1/3] Improved Cart UI & managed some backend calcs --- routes/api/cart.js | 13 ++-- views/cart.ejs | 172 ++++++++++++++++++--------------------------- 2 files changed, 72 insertions(+), 113 deletions(-) diff --git a/routes/api/cart.js b/routes/api/cart.js index 5f5f3ee..0c61fdc 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -16,8 +16,8 @@ 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); + res.status(200).render("cart", { layout: "layouts/app", symbol, latestPrice, href: '/market', avatar: req.user.image }); }); @@ -26,14 +26,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..d2bd99c 100644 --- a/views/cart.ejs +++ b/views/cart.ejs @@ -1,106 +1,68 @@ -
-

Buy Stock

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

Earphone

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

Buy Stock

+
+
+
+
+
+ Company Name +
+
+ +
+ + +
+ 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 From e105ea14a86b85b7ffa2ae81710d3f9f44ce0763 Mon Sep 17 00:00:00 2001 From: fabcodingzest <48706652+fabcodingzest@users.noreply.github.com> Date: Tue, 18 Aug 2020 03:00:02 +0530 Subject: [PATCH 2/3] Improved Cart UI with Company logos --- helpers/getCompanyNameAndLogo.js | 21 +++++++++++++++++ routes/api/cart.js | 7 +++--- views/cart.ejs | 40 ++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 helpers/getCompanyNameAndLogo.js 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 0c61fdc..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 {latestPrice} = await getPrice(symbol); - res.status(200).render("cart", { layout: "layouts/app", symbol, latestPrice, 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 diff --git a/views/cart.ejs b/views/cart.ejs index d2bd99c..83acf08 100644 --- a/views/cart.ejs +++ b/views/cart.ejs @@ -1,47 +1,52 @@ -
+

Buy Stock

-
+ class="new grid grid-flow-col grid-cols-3 md:grid-cols-4 grid-rows-2 grig-gap-0 md:gap-4 bg-white shadow-md p-2 lg:p-6 text-center"> +
Company Name
-
- +
+
+ +

<%= companyName %>

+
-