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 0ede514..5f5f3ee 100644 --- a/routes/api/cart.js +++ b/routes/api/cart.js @@ -18,6 +18,7 @@ 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 }); + }); // @desc To buy @@ -25,7 +26,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/routes/api/market.js b/routes/api/market.js index 7af6e05..11cc195 100644 --- a/routes/api/market.js +++ b/routes/api/market.js @@ -17,5 +17,5 @@ const { ensureAuth, ensureGuest } = require("../../middleware/auth"); router.get("/", ensureAuth, async (req, res) => { res.status(200).render("market", { layout: "layouts/app", jsonData, href: '/market', avatar: req.user.image }); }); - + 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/addBalance.ejs b/views/addBalance.ejs index fb42ea1..9f05175 100644 --- a/views/addBalance.ejs +++ b/views/addBalance.ejs @@ -1,13 +1,14 @@