From 2fd135169c7b0ac4759c7fc563a236445d7b8251 Mon Sep 17 00:00:00 2001 From: Pavan Gandhi Date: Fri, 14 Aug 2020 09:50:21 +0530 Subject: [PATCH 1/2] md-ml:64 fix --- routes/api/cart.js | 4 +- routes/api/market.js | 6 +- routes/api/portfolio.js | 4 +- views/market.ejs | 2 +- views/partials/_nav.ejs | 136 +++++++++++++++++++++++++++++----------- views/portfolio.ejs | 2 +- 6 files changed, 112 insertions(+), 42 deletions(-) diff --git a/routes/api/cart.js b/routes/api/cart.js index b2cde8a..a9bc5b0 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -17,7 +17,9 @@ const getPrice = require("../../helpers/getPrice"); 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' }); + res + .status(200) + .render("cart", { layout: "layouts/app", symbol, price, href: "/market" }); }); // @desc To buy diff --git a/routes/api/market.js b/routes/api/market.js index a59308d..63b9a32 100644 --- a/routes/api/market.js +++ b/routes/api/market.js @@ -15,7 +15,9 @@ const { ensureAuth, ensureGuest } = require("../../middleware/auth"); // @route GET /Market // @access Private router.get("/", ensureAuth, async (req, res) => { - res.status(200).render("market", { layout: "layouts/app", jsonData, href: '/market' }); + res + .status(200) + .render("market", { layout: "layouts/app", jsonData, href: "/market" }); }); - + module.exports = router; diff --git a/routes/api/portfolio.js b/routes/api/portfolio.js index 8aaaab7..1d28251 100644 --- a/routes/api/portfolio.js +++ b/routes/api/portfolio.js @@ -9,7 +9,9 @@ const { ensureAuth, ensureGuest } = require("../../middleware/auth"); // @access Private router.get("/", ensureAuth, (req, res) => { let avatar = req.user.image; - res.status(200).render("portfolio", { layout: "layouts/app", avatar, href: '/portfolio' }); + res + .status(200) + .render("portfolio", { layout: "layouts/app", avatar, href: "/portfolio" }); }); module.exports = router; diff --git a/views/market.ejs b/views/market.ejs index 5b8e1e4..25803b9 100644 --- a/views/market.ejs +++ b/views/market.ejs @@ -1,4 +1,4 @@ -
+
<% for(var i=0; i < jsonData.length; i++) { %> diff --git a/views/partials/_nav.ejs b/views/partials/_nav.ejs index d297ee6..39e059b 100644 --- a/views/partials/_nav.ejs +++ b/views/partials/_nav.ejs @@ -1,52 +1,116 @@ - -
- diff --git a/views/portfolio.ejs b/views/portfolio.ejs index 70da172..4eb6946 100644 --- a/views/portfolio.ejs +++ b/views/portfolio.ejs @@ -1,4 +1,4 @@ -
+
From 8ac717aac9afd44a0b53df33c10f591766f6b270 Mon Sep 17 00:00:00 2001 From: Somyaranjan Rout Date: Sun, 16 Aug 2020 03:41:49 +0530 Subject: [PATCH 2/2] Feature Added: Add Balance --- routes/api/addBalance.js | 30 ++++++++- routes/api/cart.js | 1 - views/addBalance.ejs | 19 +++--- views/partials/_nav.ejs | 139 +++++++++++---------------------------- 4 files changed, 77 insertions(+), 112 deletions(-) diff --git a/routes/api/addBalance.js b/routes/api/addBalance.js index ca73207..389214b 100644 --- a/routes/api/addBalance.js +++ b/routes/api/addBalance.js @@ -3,17 +3,41 @@ const express = require("express"); const router = express.Router(); const { ensureAuth, ensureGuest } = require("../../middleware/auth"); +const User = require("../../models/User"); // @desc // Add Balance page // @route GET / // @access Private router.get("/", ensureAuth, (req, res) => { - res.status(200).render("addBalance"); + let user = req.user + res.status(200).render("addBalance"), { + user, + } + }); // TODO -router.post("/", ensureGuest, (req, res) => { - console.log("added"); +router.post("/", ensureAuth, async (req, res) => { // why ensureGuest here? + let amount = Number(req.body.addAmount); // type cast amount to number as body parser take it as string + let finalAmont = amount + req.user.balance; + + try { + req.body.user = req.user.id; + const updateBalance = await User.findOneAndUpdate( + { _id: req.user.id }, + { balance: finalAmont}, + { + new: true, // it will create a new one, if it doesn't exist + runValidators: true, // it check weather the fields are valid or not + } + ) + console.log(updateBalance); + res.redirect("/"); + + } catch (err) { + console.error(err) + res.render('error/500') + } }); module.exports = router; diff --git a/routes/api/cart.js b/routes/api/cart.js index a9bc5b0..2b7322e 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -27,7 +27,6 @@ router.get("/:symbol", ensureAuth, async (req, res) => { router.post("/buy", ensureAuth, async (req, res) => { let data = req.body; let user = req.user; - let companySymbol = req.body.companySymbol; let stockPrice = req.body.stockPrice; let noOfStock = req.body.noOfStock; let totalAmount = stockPrice * noOfStock; diff --git a/views/addBalance.ejs b/views/addBalance.ejs index fb42ea1..9f05175 100644 --- a/views/addBalance.ejs +++ b/views/addBalance.ejs @@ -1,13 +1,14 @@

ADD BALANCE

- -
+
+

Your Current Balance: <%= user.balance%>

+
+
+
- - -
-
- ADD + + +
diff --git a/views/partials/_nav.ejs b/views/partials/_nav.ejs index 39e059b..36564ba 100644 --- a/views/partials/_nav.ejs +++ b/views/partials/_nav.ejs @@ -1,116 +1,57 @@ - -