From 8ac717aac9afd44a0b53df33c10f591766f6b270 Mon Sep 17 00:00:00 2001 From: Somyaranjan Rout Date: Sun, 16 Aug 2020 03:41:49 +0530 Subject: [PATCH] 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 @@ - -
+ \ No newline at end of file