diff --git a/src/controllers/expenses.controller.js b/src/controllers/expenses.controller.js index 4296639d..8712c666 100644 --- a/src/controllers/expenses.controller.js +++ b/src/controllers/expenses.controller.js @@ -26,6 +26,7 @@ const create = async(req, res) => { || typeof spentAt !== 'string' || !title || !amount + // || typeof category !== 'string' || !user ) { return res.sendStatus(400); @@ -45,12 +46,13 @@ const create = async(req, res) => { const getOne = async(req, res) => { const id = parseInt(req.params.id); - const product = await expensesService.getOne(id); if (typeof id !== 'number' || !id) { return res.sendStatus(400); } + const product = await expensesService.getOne(id); + if (!product) { return res.sendStatus(404); } @@ -60,6 +62,11 @@ const getOne = async(req, res) => { const remove = async(req, res) => { const id = parseInt(req.params.id); + + if (typeof id !== 'number' || !id) { + return res.sendStatus(400); + } + const product = await expensesService.getOne(id); if (!product) { @@ -74,6 +81,11 @@ const remove = async(req, res) => { const update = async(req, res) => { const id = parseInt(req.params.id); const { ...args } = req.body; + + if (typeof id !== 'number' || !id) { + return res.sendStatus(400); + } + const product = await expensesService.getOne(id); if (!product) { diff --git a/src/controllers/user.controller.js b/src/controllers/user.controller.js index a93d52b6..3729e795 100644 --- a/src/controllers/user.controller.js +++ b/src/controllers/user.controller.js @@ -20,6 +20,11 @@ const create = async(req, res) => { const getOne = async(req, res) => { const id = parseInt(req.params.id); + + if (typeof id !== 'number' || !id) { + return res.sendStatus(400); + } + const neededUser = await userService.getOne(id); if (!neededUser) { @@ -31,6 +36,11 @@ const getOne = async(req, res) => { const remove = async(req, res) => { const id = parseInt(req.params.id); + + if (typeof id !== 'number' || !id) { + return res.sendStatus(400); + } + const user = await userService.getOne(id); if (!user) { @@ -45,12 +55,18 @@ const remove = async(req, res) => { const update = async(req, res) => { const id = parseInt(req.params.id); const { name } = req.body; - const user = await userService.getOne(id); - if (typeof name !== 'string' || !name.length) { + if ( + typeof name !== 'string' + || typeof id !== 'number' + || !name.length + || !id + ) { return res.sendStatus(400); } + const user = await userService.getOne(id); + if (!user) { return res.sendStatus(404); } diff --git a/src/services/expenses.service.js b/src/services/expenses.service.js index 082d8336..fc970456 100644 --- a/src/services/expenses.service.js +++ b/src/services/expenses.service.js @@ -14,7 +14,7 @@ const create = async( category = 'uncategorized', note = null, ) => { - const expense = await Expense.create({ + return Expense.create({ userId, spentAt, title, @@ -22,8 +22,6 @@ const create = async( category, note, }); - - return expense; }; const update = async(id, args) => { diff --git a/src/services/user.service.js b/src/services/user.service.js index 85536bc2..ca6c2d80 100644 --- a/src/services/user.service.js +++ b/src/services/user.service.js @@ -7,11 +7,9 @@ const getAll = async() => User.findAll(); const remove = async(id) => User.destroy({ where: { id } }); const create = async(name) => { - const user = await User.create({ + return User.create({ name, }); - - return user; }; const update = (id, name) => {