Skip to content

Commit

Permalink
code update after review
Browse files Browse the repository at this point in the history
  • Loading branch information
eduvinagre committed Jul 29, 2024
1 parent eb79a61 commit 8e1b27f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
14 changes: 2 additions & 12 deletions src/controllers/expenseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 15 additions & 6 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
Expand All @@ -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 {
Expand Down
76 changes: 34 additions & 42 deletions src/services/expenseService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8e1b27f

Please sign in to comment.