From 98da11b940c194f5e4bd423cb57d252df6185715 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Wed, 7 Oct 2020 08:45:32 -0500 Subject: Add budget goals --- src/server.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/server.js') diff --git a/src/server.js b/src/server.js index 53db084..69d3968 100644 --- a/src/server.js +++ b/src/server.js @@ -14,7 +14,7 @@ server.use(bodyParser.json()); //server.use(bodyParser.urlencoded({ extended: true })); function listen(port) { - server.listen(port, () => console.info(`Listening on port ${port}!`)); + server.listen(port, () => console.info(`Listening: http://localhost:${port} `)); } function hashWithSalt(password, salt){ @@ -36,7 +36,6 @@ function setUpRoutes(models, jwtFunctions, database) { try { const decryptedUserId = jwtFunctions.verify(cookie); var user = await models.users.findOne({ where: { username: decryptedUserId } }); - // .then((user, error) => { if (user) { res.locals.user = user.get({ plain: true }); } else { @@ -44,7 +43,6 @@ function setUpRoutes(models, jwtFunctions, database) { res.redirect('/login'); return; } - // }); } catch (e) { res.status(400).send(e.message); } @@ -128,6 +126,43 @@ function setUpRoutes(models, jwtFunctions, database) { res.status(400).send(e.message); } }) + server.get(`/goals`, async (req, res, next) => { + try { + var result = await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + next(); + } catch (e) { + console.log(e) + res.status(400).send(e.message); + } + }) + server.post(`/goals`, async (req, res, next) => { + try { + let item = req.body; + console.log(item); + item.username = res.locals.user.username + await models.goals.create(item); + var result = await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + } catch (e) { + console.log(e); + res.status(400).send(e.message); + } + }) + server.post(`/allocate`, async (req, res, next) => { + try { + let name = req.body.name; + let amount = req.body.amount; + var toUpdate = await models.goals.findOne({ where: { name: name, username:res.locals.user.username } }); + var update = {amount: toUpdate.amount + amount} + await toUpdate.update(update); + var result = await await database.query("SELECT * FROM goals WHERE username = '" + res.locals.user.username + "' ORDER BY `name` DESC", { type: database.QueryTypes.SELECT }) + res.status(200).send(result); + } catch (e) { + console.log(e); + res.status(400).send(e.message); + } + }) server.get(`/summary`, async (req, res, next) => { try { res.status(200).send({ -- cgit v1.2.3