Skip to content

Commit

Permalink
fix: code refactor for models, db, controllers, services and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
eduvinagre committed Aug 24, 2024
1 parent 5e965c4 commit 3e28b2a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 42 deletions.
20 changes: 18 additions & 2 deletions src/controllers/expenseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const { ExpenseService } = require('../services/expenseService');
const expenseService = new ExpenseService();
const { Op } = require('sequelize');
const userService = require('../services/userService');

async function createExpense(req, res) {
Expand Down Expand Up @@ -37,9 +38,24 @@ async function createExpense(req, res) {

async function getAllExpenses(req, res) {
try {
const { userId, categories: category } = req.query;
const { userId, categories: category, from, to } = req.query;
const where = {};

const expenses = await expenseService.getAllExpenses({ userId, category });
if (userId) {
where.userId = userId;
}

if (category) {
where.category = category;
}

if (from && to) {
where.spentAt = {
[Op.between]: [new Date(from), new Date(to)],
};
}

const expenses = await expenseService.getAllExpenses(where);

res.json(expenses);
} catch (error) {
Expand Down
21 changes: 1 addition & 20 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@

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 (!validateName(name, res)) {
return;
}

try {
const { name } = req.body;
const newUser = await userService.createUser({ name });

res.status(201).json(newUser);
Expand Down Expand Up @@ -57,10 +42,6 @@ async function getUser(req, res) {
async function updateUser(req, res) {
const { name } = req.body;

if (!validateName(name, res)) {
return;
}

try {
const user = await userService.updateUser(parseInt(req.params.id), {
name,
Expand Down
1 change: 1 addition & 0 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ const sequelize = new Sequelize({

module.exports = {
sequelize,
Sequelize,
};
4 changes: 2 additions & 2 deletions src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db.js');
const { sequelize, Sequelize } = require('../db.js');
const { DataTypes } = Sequelize;

const Expense = sequelize.define(
'Expense',
Expand Down
4 changes: 2 additions & 2 deletions src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { DataTypes } = require('sequelize');
const { sequelize } = require('../db.js');
const { sequelize, Sequelize } = require('../db.js');
const { DataTypes } = Sequelize;

const User = sequelize.define(
'User',
Expand Down
5 changes: 3 additions & 2 deletions src/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

const express = require('express');
const userController = require('../controllers/userController');
const { validateName } = require('../validators/validateName');

const router = express.Router();

router.post('/', userController.createUser);
router.post('/', validateName, userController.createUser);
router.get('/', userController.getAllUsers);
router.get('/:id', userController.getUser);
router.patch('/:id', userController.updateUser);
router.patch('/:id', validateName, userController.updateUser);
router.delete('/:id', userController.deleteUser);

module.exports = router;
20 changes: 8 additions & 12 deletions src/services/expenseService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict';

const { models } = require('../models/models');
Expand All @@ -16,20 +17,15 @@ class ExpenseService {
return newExpense;
}

async getAllExpenses({ userId, category }) {
const where = {};
async getAllExpenses(where = {}) {
try {
const expenses = await models.Expense.findAll({ where });

if (userId) {
where.userId = userId;
return expenses;
} catch (error) {
console.error('Error in getAllExpenses:', error);
throw error;
}

if (category) {
where.category = category;
}

const expenses = await models.Expense.findAll({ where });

return expenses;
}

async updateExpense(id, newExpense) {
Expand Down
3 changes: 1 addition & 2 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const userService = {
return newUser;
},

async getAllUsers(filters = {}) {
const where = { ...filters };
async getAllUsers(where = {}) {
const users = await User.findAll({ where });

return users;
Expand Down
13 changes: 13 additions & 0 deletions src/validators/validateName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function validateName(req, res, next) {
const { name } = req.body;

if (!name) {
return res.status(400).send('Name is required');
}

next();
}

module.exports = {
validateName,
};

0 comments on commit 3e28b2a

Please sign in to comment.