From 8e1b27ff12388fd32b0aae752dea5b05b8852145 Mon Sep 17 00:00:00 2001 From: eduvinagre Date: Mon, 29 Jul 2024 16:50:08 -0300 Subject: [PATCH] code update after review --- src/controllers/expenseController.js | 14 +---- src/controllers/userController.js | 21 +++++--- src/services/expenseService.js | 76 +++++++++++++--------------- src/services/userService.js | 11 +--- 4 files changed, 52 insertions(+), 70 deletions(-) diff --git a/src/controllers/expenseController.js b/src/controllers/expenseController.js index ec059e09..3c2c0585 100644 --- a/src/controllers/expenseController.js +++ b/src/controllers/expenseController.js @@ -37,19 +37,9 @@ async function createExpense(req, res) { async function getAllExpenses(req, res) { try { - const userId = req.query.userId; - const category = req.query.categories; - const filters = {}; + const { userId, categories: category } = req.query; - if (userId) { - filters.userId = userId; - } - - if (category) { - filters.category = category; - } - - const expenses = await expenseService.getAllExpenses(filters); + const expenses = await expenseService.getAllExpenses({ userId, category }); res.json(expenses); } catch (error) { diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 759c145e..d42c735f 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -2,11 +2,21 @@ const userService = require('../services/userService'); +function validateName(name, res) { + if (!name) { + res.status(400).send('Name is required'); + + return false; + } + + return true; +} + async function createUser(req, res) { const { name } = req.body; - if (!name) { - return res.status(400).send('Name and are required'); + if (!validateName(name, res)) { + return; } try { @@ -30,10 +40,9 @@ async function getAllUsers(req, res) { async function getUser(req, res) { try { - const users = await userService.getAllUsers({ + const [user] = await userService.getAllUsers({ id: parseInt(req.params.id), }); - const user = users[0]; if (!user) { return res.status(404).send('User not found'); @@ -48,8 +57,8 @@ async function getUser(req, res) { async function updateUser(req, res) { const { name } = req.body; - if (!name) { - return res.status(400).send('Name and are required'); + if (!validateName(name, res)) { + return; } try { diff --git a/src/services/expenseService.js b/src/services/expenseService.js index 913849af..958430fc 100644 --- a/src/services/expenseService.js +++ b/src/services/expenseService.js @@ -4,60 +4,52 @@ const { models } = require('../models/models'); class ExpenseService { async createExpense(userId, spentAt, title, amount, category, note) { - try { - const newExpense = await models.Expense.create({ - userId, - spentAt, - title, - amount, - category, - note, - }); - - return newExpense; - } catch (error) { - throw error; - } + const newExpense = await models.Expense.create({ + userId, + spentAt, + title, + amount, + category, + note, + }); + + return newExpense; } - async getAllExpenses(filters = {}) { - try { - const expenses = await models.Expense.findAll({ - where: filters, - }); + async getAllExpenses({ userId, category }) { + const where = {}; - return expenses; - } catch (error) { - throw error; + if (userId) { + where.userId = userId; } + + if (category) { + where.category = category; + } + + const expenses = await models.Expense.findAll({ where }); + + return expenses; } async updateExpense(id, newExpense) { - try { - const [updatedRowsCount, [updatedExpense]] = await models.Expense.update( - newExpense, - { - where: { id }, - returning: true, - }, - ); + const [updatedRowsCount, [updatedExpense]] = await models.Expense.update( + newExpense, + { + where: { id }, + returning: true, + }, + ); - return updatedRowsCount ? updatedExpense : null; - } catch (error) { - throw error; - } + return updatedRowsCount ? updatedExpense : null; } async deleteExpense(id) { - try { - const deletedRowsCount = await models.Expense.destroy({ - where: { id }, - }); + const deletedRowsCount = await models.Expense.destroy({ + where: { id }, + }); - return deletedRowsCount > 0; - } catch (error) { - throw error; - } + return deletedRowsCount > 0; } } diff --git a/src/services/userService.js b/src/services/userService.js index 0768ef2d..96ffed62 100644 --- a/src/services/userService.js +++ b/src/services/userService.js @@ -10,16 +10,7 @@ const userService = { }, async getAllUsers(filters = {}) { - const where = {}; - - if (filters.id) { - where.id = filters.id; - } - - if (filters.name) { - where.name = filters.name; - } - + const where = { ...filters }; const users = await User.findAll({ where }); return users;