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

VB solution #286

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
103 changes: 103 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const expenseService = require('../services/expenses.service');
const userService = require('../services/users.service');

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

res.statusCode = 200;
res.send(expenses);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

try {
const expense = await expenseService.getOne(id);

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

return;
}
res.statusCode = 200;
res.send(expense);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

try {
const user = await userService.getOne(userId);

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

return;
}

const expense = await expenseService.create(req.body);

res.statusCode = 201;
res.send(expense);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

try {
const expense = await expenseService.getOne(id);

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

return;
}

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

res.send(updatedExpense);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

if (!(await expenseService.getOne(id))) {
res.sendStatus(404);

return;
}

try {
await expenseService.remove(id);

res.sendStatus(204);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

res.send(users);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

return;
}

res.send(user);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

const create = async (req, res) => {
if (!req.body.name) {
res.sendStatus(400);

return;
}

try {
const user = await userService.create(req.body);

res.statusCode = 201;
res.send(user);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

try {
const user = await userService.getOne(id);

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

return;
}

const updatedUser = await userService.update(id, req.body);

res.send(updatedUser);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

try {
const user = await userService.getOne(id);

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

return;
}

await userService.remove(id);
res.sendStatus(204);
} catch {
res.statusCode = 500;
res.send('Something went wrong');
}
};

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

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

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

app.use(express.json());

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

return app;
}

module.exports = {
createServer,
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,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
},
note: {
type: DataTypes.STRING,
},
},
{
tableName: 'expenses',
createdAt: false,
updatedAt: false,
},
);

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

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

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

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

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

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

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

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

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, to] };
}

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

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

return Expense.findByPk(id);
};

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

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