Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/controllers/categories.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require('express');

const {
getAllCategories,
getCategoryById,
deleteCategory,
createCategory,
updateCategory,
} = require('../routers/categories.router.js');
Comment on lines +3 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job separating concerns! Just a note on naming conventions: typically, this file, which defines the routes, would be named categories.router.js. The file containing the request handler logic (like getAllCategories) would be the controller. You currently have this logic in ../routers/categories.router.js. Swapping the file names or their contents would align your project better with common Express.js patterns and make it more intuitive for other developers.


const categoriesRoute = express.Router();

categoriesRoute.get('/', getAllCategories);

categoriesRoute.get('/:id', getCategoryById);

categoriesRoute.post('/', createCategory);

categoriesRoute.delete('/:id', deleteCategory);

categoriesRoute.patch('/:id', updateCategory);

module.exports = { categoriesRoute };
23 changes: 23 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require('express');

const {
getAllExpenses,
getExpenseById,
deleteExpense,
createExpense,
updateExpense,
} = require('../routers/expenses.router.js');

const expensesRoute = express.Router();

expensesRoute.get('/', getAllExpenses);

expensesRoute.get('/:id', getExpenseById);

expensesRoute.post('/', createExpense);

expensesRoute.delete('/:id', deleteExpense);

expensesRoute.patch('/:id', updateExpense);

module.exports = { expensesRoute };
Comment on lines +1 to +23
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core task is to implement CRUD functionality for categories. This file handles expenses, but the new functionality for categories seems to be missing entirely from the project. You'll need to add a new set of files (Category.model.js, categories.service.js, categories.router.js, categories.controller.js) and register the new route in createServer.js.

23 changes: 23 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const express = require('express');

const {
getAllUsers,
getUsersById,
deleteUser,
createUser,
updateUser,
} = require('../routers/users.router.js');

const usersRoute = express.Router();

usersRoute.get('/', getAllUsers);

usersRoute.get('/:id', getUsersById);

usersRoute.post('/', createUser);

usersRoute.delete('/:id', deleteUser);

usersRoute.patch('/:id', updateUser);

module.exports = { usersRoute };
23 changes: 20 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
'use strict';

const createServer = () => {
// your code goes here
};
const express = require('express');
const cors = require('cors');

const { expensesRoute } = require('./controllers/expenses.controller.js');
const { usersRoute } = require('./controllers/users.controller.js');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you've missed importing the router for categories. To manage categories, you'll need a dedicated controller and router, similar to what you have for expenses and users.

const { categoriesRoute } = require('./controllers/categories.controller.js');

function createServer() {
const app = express();

app.use(cors());

app.use(express.json());

app.use('/expenses', expensesRoute);
app.use('/users', usersRoute);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server is missing the endpoint for categories. You should mount the categories route here using app.use('/categories', categoriesRoute); after you've created and imported it.

app.use('/categories', categoriesRoute);

return app;
}

module.exports = {
createServer,
Expand Down
2 changes: 1 addition & 1 deletion src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const sequelize = new Sequelize({
host: POSTGRES_HOST || 'localhost',
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || '12345',
});

module.exports = {
Expand Down
27 changes: 27 additions & 0 deletions src/models/Category.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

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

const Category = sequelize.define(
'Category',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'categories',
timestamps: false,
},
);

module.exports = {
Category,
};
37 changes: 36 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
'use strict';

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

const Expense = sequelize.define(
// your code goes here
'Expense',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
},
{
tableName: 'expenses',
timestamps: false,
},
);

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
'use strict';

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

const User = sequelize.define(
// your code goes here
'User',
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'users',
timestamps: false,
},
);

module.exports = {
Expand Down
102 changes: 102 additions & 0 deletions src/routers/categories.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const {
getAll,
getById,
create,
update,
remove,
} = require('../services/categories.service.js');

const getAllCategories = async (req, res) => {
const categories = await getAll();

res.send(categories);
};

const getCategoryById = async (req, res) => {
const { id } = req.params;

const idNum = Number(id);

const category = await getById(idNum);

if (!category) {
res.status(404).send({ message: 'Not found' });

return;
}

res.send(category);
};

const createCategory = async (req, res) => {
const { name } = req.body;

if (typeof name !== 'string') {
res.status(400).send({ message: 'Invalid field' });

return;
}
Comment on lines +34 to +38
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of validation checks here can lead to a less specific error message. If name is missing (undefined), the typeof name !== 'string' check will trigger first, sending 'Invalid field'. It's better to check for missing fields before checking their types. Consider swapping this if block with the one below.


if (name === undefined) {
res.status(400).send({ message: 'Missing required field' });

return;
}

const category = await create({ name });

res.status(201).send(category);
};

const deleteCategory = async (req, res) => {
const { id } = req.params;

const idNum = Number(id);

const category = await remove(idNum);

if (!category) {
res.status(404).send({ message: 'Not found' });

return;
}

res.status(204).send();
};

const updateCategory = async (req, res) => {
const { id } = req.params;
const { name } = req.body;

const idNum = Number(id);

if (typeof name !== 'string') {
res.status(400).send({ message: 'Invalid field' });

return;
}
Comment on lines +73 to +77
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the createCategory function, the validation order here should be reversed. Checking for name === undefined before checking its type will ensure a more accurate error message is returned when the field is missing.


if (name === undefined) {
res.status(400).send({ message: 'Missing required field' });

return;
}

const category = await update({ id: idNum, name });

if (!category) {
res.status(404).send({ message: 'Not found' });

return;
}

res.send(category);
};

module.exports = {
getAllCategories,
getCategoryById,
createCategory,
deleteCategory,
updateCategory,
};
Loading
Loading