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 solution #241

Open
wants to merge 2 commits 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
84 changes: 84 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// const { User } = require('../models/User.model');
const expensesService = require('../services/expenses.service');
const { getById } = require('../services/users.service');
const HTTP_STATUS_CODES = require('../variables/httpStatusCodes');

const getAll = async (req, res) => {
const { query } = req;

const expenses = await expensesService.getAll(query);

res.send(expenses);
};

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

const expense = await expensesService.getById(id);

if (!expense) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

res.statusCode = HTTP_STATUS_CODES.OK;
res.send(expense);
};

const create = async (req, res) => {
const body = req.body;
const user = await getById(body.userId);

if (!user) {
res.sendStatus(HTTP_STATUS_CODES.BAD_REQUEST);

return;
}

const expense = await expensesService.create(body);

res.statusCode = HTTP_STATUS_CODES.CREATED;
res.send(expense);
};

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

const expense = await expensesService.getById(id);

if (!expense) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

const updatedExpense = await expensesService.update(id, body);

res.statusCode = HTTP_STATUS_CODES.OK;

res.send(updatedExpense);
};

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

if (!expense) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

await expensesService.remove(id);
res.sendStatus(HTTP_STATUS_CODES.NO_CONTENT);
};

module.exports = {
getAll,
getOne,
create,
remove,
update,
};
82 changes: 82 additions & 0 deletions src/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const usersService = require('../services/users.service');
const HTTP_STATUS_CODES = require('../variables/httpStatusCodes');

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

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

const user = await usersService.getById(id);

if (!user) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

res.send(user);
};

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

if (!name) {
res.sendStatus(HTTP_STATUS_CODES.BAD_REQUEST);

return;
}

const user = await usersService.create(name);

res.statusCode = HTTP_STATUS_CODES.CREATED;
res.send(user);
};

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

const user = await usersService.getById(id);

if (!user) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

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

return;
}

const updatedUser = await usersService.update(id, name);

res.statusCode = HTTP_STATUS_CODES.OK;

res.send(updatedUser);
};

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

if (!user) {
res.sendStatus(HTTP_STATUS_CODES.NOT_FOUND);

return;
}

await usersService.remove(id);
res.sendStatus(HTTP_STATUS_CODES.NO_CONTENT);
};

module.exports = {
getAll,
getOne,
create,
remove,
update,
};
18 changes: 15 additions & 3 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
'use strict';

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

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

app.use(cors());

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

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 || '56789',
});

module.exports = {
Expand Down
39 changes: 38 additions & 1 deletion src/models/Expense.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
'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',
updatedAt: false,
createdAt: 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,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'users',
updatedAt: false,
createdAt: false,
},
);

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

const router = express.Router();

router.get('/', expensesController.getAll);
router.get('/:id', expensesController.getOne);
router.post('/', expensesController.create);
router.delete('/:id', expensesController.remove);
router.patch('/:id', expensesController.update);

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

const router = express.Router();

router.get('/', usersController.getAll);
router.get('/:id', usersController.getOne);
router.post('/', usersController.create);
router.delete('/:id', usersController.remove);
router.patch('/:id', usersController.update);

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

const getAll = async (query) => {
const filterParam = getFilteredExpenses(query);

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

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 update = async (id, body) => {
await Expense.update({ ...body }, { where: { id } });

return Expense.findByPk(id);
};

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

module.exports = {
getAll,
getById,
create,
update,
remove,
};
33 changes: 33 additions & 0 deletions src/services/users.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { User } = require('../models/User.model');

const getAll = async () => {
const result = await User.findAll();

return result;
};

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

const create = async (name) => {
return User.create({ name });
};

const update = async (id, name) => {
await User.update({ name }, { where: { id } });

return User.findByPk(id);
};

const remove = async (id) => {
return User.destroy({ where: { id } });
};

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