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

done #246

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

done #246

Show file tree
Hide file tree
Changes from 1 commit
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
79 changes: 79 additions & 0 deletions src/controllers/expense.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const expensesService = require('../services/expense.service.js');
const userService = require('../services/user.service.js');

const get = async (req, res) => {
res.send(await expensesService.getAll(req.query));
};

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

if (!(await userService.getById(expense.userId))) {
res.sendStatus(400);

return;
}

const newExpense = await expensesService.create(expense);

res.statusCode = 201;
res.send(newExpense);
};

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

const expense = await expensesService.getById(id);

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

return;
}

res.send(expense);
};

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

if (!(await expensesService.getById(id))) {
res.sendStatus(404);

return;
}

await expensesService.remove(id);
res.sendStatus(204);
};

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

const expense = await expensesService.getById(id);

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

return;
}

if (!data) {
res.sendStatus(400);

return;
}

const updatedExpense = await expensesService.update(expense, data);

res.send(updatedExpense);
};

module.exports = {
get,
create,
getOne,
remove,
update,
};
75 changes: 75 additions & 0 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const userServices = require('../services/user.service.js');

const get = async (req, res) => {
res.send(await userServices.getAll());
};

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

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

const user = await userServices.create(name);

res.statusCode = 201;
res.send(user);
};

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

Choose a reason for hiding this comment

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

check if id was passed

const user = await userServices.getById(id);

Choose a reason for hiding this comment

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

also would be great to handle errors on this statge

Choose a reason for hiding this comment

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

applicable to all service requests


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

return;
}

res.send(user);
};

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

if (!(await userServices.getById(id))) {
res.sendStatus(404);

return;
}

await userServices.remove(id);
res.sendStatus(204);
};

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

Choose a reason for hiding this comment

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

it is better to validate received data immediately after receiving


const user = await userServices.getById(id);

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

return;
}

if (typeof name !== 'string') {
res.sendStatus(400);

return;
}

const updatedUser = await userServices.update({ id, name });

res.send(updatedUser);
};

module.exports = {
get,
create,
getOne,
remove,
update,
};
18 changes: 17 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
'use strict';

const express = require('express');
const cors = require('cors');
const { router: userRouter } = require('./routes/user.route.js');
const { router: expenseRouter } = require('./routes/expense.route.js');

const createServer = () => {
// your code goes here
const app = express();

app.use(cors());

app.use('/users', express.json(), userRouter);
app.use('/expenses', express.json(), expenseRouter);

app.use((req, res) => {
res.status(404).json({ error: 'Not found' });
});

return app;
};

module.exports = {
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 || 'prosto123',
});

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

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

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

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

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

router.get('/', expensesController.get);

router.post('/', expensesController.create);

router.get('/:id', expensesController.getOne);

router.delete('/:id', expensesController.remove);

router.patch('/:id', expensesController.update);

module.exports = { router };
15 changes: 15 additions & 0 deletions src/routes/user.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express');
const router = express.Router();
const userController = require('../controllers/user.controller.js');

router.get('/', userController.get);

router.post('/', userController.create);

router.get('/:id', userController.getOne);

router.delete('/:id', userController.remove);

router.patch('/:id', userController.update);

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

const getAll = async (userId, categories, from, to) => {
const where = {};

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

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

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

return Expense.findAll({ where });
};

const getById = async (id) => {
return Expense.findByPk(id);
};

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

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

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

if (!expense) {
return null;
}

if (spentAt) {
expense.spentAt = spentAt;
}

if (title) {
expense.title = title;
}

if (amount) {
expense.amount = amount;
}

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

if (note) {
expense.note = note;
}

await expense.save();

return expense;
};

module.exports = {
getAll,
getById,
create,
remove,
update,
};
Loading
Loading