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 #235

Open
wants to merge 3 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
76 changes: 76 additions & 0 deletions src/controllers/expenses.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const expensesService = require('../services/expenses.services');
const { statusCode } = require('../helpers/statusCode');

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

res.send(expenses);
};

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

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

return;
}

res.statusCode = 200;
res.send(expense);
};

const add = async (req, res) => {
const body = req.body;
const userId = parseInt(body.userId);

if (!userId) {
res.sendStatus(statusCode.BAD_REQUEST);

return;
}

const expense = await expensesService.add(body);

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

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

try {
await expensesService.remove(id);

res.sendStatus(statusCode.NO_CONTENT);
} catch (error) {
res.sendStatus(statusCode.NOT_FOUND);
}
};

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

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

return;
}

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

res.statusCode = statusCode.OK;
res.send(updatedExpense);
};

module.exports = {
getAll,
add,
getOne,
remove,
update,
};
83 changes: 83 additions & 0 deletions src/controllers/user.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const userService = require('../services/user.services');
const { statusCode } = require('../helpers/statusCode');

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

res.send(users);
};

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

const user = await userService.getUserById(id);

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

return;
}

res.status(statusCode.OK).send(user);
};

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

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

return;
}

const user = await userService.create(name);

res.status(statusCode.CREATED).send(user);
};

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

const user = await userService.getUserById(id);

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

return;
}

userService.remove(id);

res.sendStatus(statusCode.NO_CONTENT);
};

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

const user = userService.getUserById(id);

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

return;
}

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

return;
}

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

res.send(updatedUser);
};

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

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

const { userRouter } = require('./routes/user.routes');
const { expensesRouter } = require('./routes/expenses.routes');

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

app.use(cors());

app.use(express.json());

app.use('/users', userRouter);
app.use('/expenses', 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,
});

module.exports = {
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/statusCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const statusCode = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_FOUND: 404,
UNPROCESSABLE_ENTITY: 422,
};

module.exports = { statusCode };
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 { sequelize } = require('../db.js');
const { DataTypes } = require('sequelize');

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

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

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

const User = sequelize.define(
// your code goes here
'User',
{
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
tableName: 'users',
createdAt: false,
updatedAt: false,
},
);

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

const expenseConroller = require('../controllers/expenses.controllers');

const expensesRouter = express.Router();

expensesRouter.get('/', expenseConroller.getAll);

expensesRouter.get('/:id', expenseConroller.getOne);

expensesRouter.post('/', expenseConroller.add);

expensesRouter.delete('/:id', expenseConroller.remove);

expensesRouter.patch('/:id', expenseConroller.update);

module.exports = {
expensesRouter,
};
19 changes: 19 additions & 0 deletions src/routes/user.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');

const userController = require('../controllers/user.controllers');

const userRouter = express.Router();

userRouter.get('/', userController.getAll);

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

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

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

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

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

const get = async (query) => {
const filteredExpenses = getFilteredExpenses(query);

return Expense.findAll({
where: filteredExpenses,
});
};

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

const add = async (data) => {
return Expense.create(data);
};

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

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

return Expense.findByPk(id);
};

module.exports = {
get,
add,
getById,
remove,
update,
};
Loading
Loading