Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add task solution #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
99 changes: 99 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const expensesServise = require('../services/expenses.service');
const { getById: getUserById } = require('../services/users.service');

const getAll = async (req, res) => {
const expenses = await expensesServise.getAll(req.query);

res.json(expenses.map(expensesServise.normalize));
};

const create = async (req, res) => {
const { userId, spentAt, title, amount, category, note } = req.body;

const user = await getUserById(userId);

if (!user || !userId || !spentAt || !title || !amount || isNaN(+amount)) {
return res.sendStatus(400);
}

const newExpense = await expensesServise.create({
userId,
spentAt,
title,
amount: +amount,
category,
note,
});

res.status(201).json(expensesServise.normalize(newExpense));
};

const getById = async (req, res) => {
const { id } = req.params;
const numId = +id;

if (!id || isNaN(numId)) {
return res.sendStatus(400);
}

const expense = await expensesServise.getById(numId);

if (!expense) {
return res.sendStatus(404);
}

res.json(expensesServise.normalize(expense));
};

const deleteById = async (req, res) => {
const { id } = req.params;
const numId = +id;

if (!id || isNaN(numId)) {
return res.sendStatus(400);
}

const expense = await expensesServise.getById(numId);

if (!expense) {
return res.sendStatus(404);
}

await expensesServise.deleteById(numId);

res.status(204).json(expensesServise.normalize(expense));
};

const updateById = async (req, res) => {
const { id } = req.params;
const { spentAt, title, amount, category, note } = req.body;
const numId = +id;

const expense = await expensesServise.getById(numId);

if (!expense) {
return res.sendStatus(404);
}

if (!spentAt && !title && !amount && !category && !note) {
return res.sendStatus(400);
}

const updatedExpense = await expensesServise.updateById(id, {
spentAt: spentAt || expense.spentAt,
title: title || expense.title,
amount: amount || expense.amount,
category: category || expense.category,
note: note || expense.note,
});

res.json(expensesServise.normalize(updatedExpense));
};

module.exports = {
getAll,
create,
getById,
deleteById,
updateById,
};
83 changes: 83 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const usersService = require('../services/users.service');

const getAll = async (req, res) => {
const users = await usersService.getAll();

res.json(users.map(usersService.normalize));
};

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

if (!name) {
return res.sendStatus(400);
}

const newUser = await usersService.create(name);

res.status(201).json(usersService.normalize(newUser));
};

const getById = async (req, res) => {
const { id } = req.params;
const numId = +id;

if (!id || isNaN(numId)) {
return res.sendStatus(400);
}

const user = await usersService.getById(numId);

if (!user) {
return res.sendStatus(404);
}

res.json(usersService.normalize(user));
};

const deleteById = async (req, res) => {
const { id } = req.params;
const numId = +id;

if (!id || isNaN(numId)) {
return res.sendStatus(400);
}

const user = await usersService.getById(numId);

if (!user) {
return res.sendStatus(404);
}

await usersService.deleteById(numId);

res.status(204).json(usersService.normalize(user));
};

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

if (!id || !name || isNaN(numId)) {
return res.sendStatus(400);
}

const user = await usersService.getById(numId);

if (!user) {
return res.sendStatus(404);
}

const updatedUser = await usersService.updateById(numId, { name });

res.json(usersService.normalize(updatedUser));
};

module.exports = {
getAll,
create,
getById,
deleteById,
updateById,
};
19 changes: 16 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
'use strict';

const createServer = () => {
// your code goes here
};
const express = require('express');
const cors = require('cors');
const { usersRouter } = require('./routers/users.router.js');
const { expensesRouter } = require('./routers/expenses.router.js');

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

app.use(express.json());
app.use(cors());

app.use('/users', usersRouter);
app.use('/expenses', expensesRouter);

return app;
}

module.exports = {
createServer,
Expand Down
35 changes: 34 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
'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.STRING,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
},
{
tableName: 'expenses',
},
);

module.exports = {
Expand Down
17 changes: 16 additions & 1 deletion src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
'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',
},
);

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

const expensesRouter = Router();

expensesRouter.get('/', expensesController.getAll);
expensesRouter.post('/', expensesController.create);
expensesRouter.get('/:id', expensesController.getById);
expensesRouter.delete('/:id', expensesController.deleteById);
expensesRouter.patch('/:id', expensesController.updateById);

module.exports = { expensesRouter };
12 changes: 12 additions & 0 deletions src/routers/users.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Router } = require('express');
const usersController = require('../controllers/users.controller');

const usersRouter = Router();

usersRouter.get('/', usersController.getAll);
usersRouter.post('/', usersController.create);
usersRouter.get('/:id', usersController.getById);
usersRouter.delete('/:id', usersController.deleteById);
usersRouter.patch('/:id', usersController.updateById);

module.exports = { usersRouter };
90 changes: 90 additions & 0 deletions src/services/expenses.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const { Expense } = require('../models/Expense.model');

const normalize = ({ id, userId, spentAt, title, amount, category, note }) => {
return {
id,
userId,
spentAt,
title,
amount,
category,
note,
};
};

const getAll = async ({ userId, from, to, categories }) => {
const result = await Expense.findAll();

const filteredExpenses = result.filter((expense) => {
if (userId && expense.userId !== +userId) {
return false;
}

if (from && new Date(expense.spentAt) < new Date(from)) {
return false;
}

if (to && new Date(expense.spentAt) > new Date(to)) {
return false;
}

if (categories && !categories.includes(expense.category)) {
return false;
}

return true;
});

return filteredExpenses;
};

const getById = async (id) => {
const numId = +id;

if (isNaN(numId)) {
return;
}

return Expense.findByPk(numId);
};

const create = async ({ userId, spentAt, title, amount, category, note }) => {
return Expense.create({
userId,
spentAt,
title,
amount,
category,
note,
});
};

const deleteById = async (id) => {
await Expense.destroy({
where: { id },
});
};

const updateById = async (id, { spentAt, title, amount, category, note }) => {
await Expense.update(
{
spentAt,
title,
amount,
category,
note,
},
{ where: { id } },
);

return Expense.findByPk(id);
};

module.exports = {
getAll,
getById,
create,
deleteById,
updateById,
normalize,
};
Loading
Loading