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

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
115 changes: 115 additions & 0 deletions src/controllers/expenses.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const { STATUS } = require('../utils/statusCodes');
const expensesService = require('../services/expenses.service');
const usersService = require('../services/users.service');

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

res.status(STATUS.OK).send(expenses);
};

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

if (isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

const expense = await expensesService.getOneById(id);

if (!expense) {
res
.status(STATUS.NOT_FOUND)
.send(`The expense with ${id} ID does not exist.`);

return;
}

res.status(STATUS.OK).send(expense);
};

const create = async (req, res) => {
const { userId, spentAt, title, amount } = req.body;
const user = await usersService.getOneById(userId);

if (
!user ||
typeof spentAt !== 'string' ||
typeof title !== 'string' ||
typeof amount !== 'number'
) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

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

res.status(STATUS.CREATED).send(expense);
};

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

if (isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

const expense = await expensesService.getOneById(id);

if (!expense) {
res
.status(STATUS.NOT_FOUND)
.send(`The expense with ${id} ID does not exist.`);

return;
}

await expensesService.remove(id);

res
.status(STATUS.NO_CONTENT)
.send(`The expense with ${id} ID was successfully deleted.`);
};

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

if (isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

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

if (!expense) {
res
.status(STATUS.NOT_FOUND)
.send(`The expense with ${id} ID does not exist.`);

return;
}

res.status(STATUS.OK).send(expense);
};

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

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

res.status(STATUS.OK).send(normaliseUsers);
};

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

if (isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

const user = await usersService.getOneById(id);

if (!user) {
res.status(STATUS.NOT_FOUND).send('The user with this ID does not exist.');

return;
}

res.status(STATUS.OK).send(usersService.normalise(user));
};

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

if (!name) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

const newUser = await usersService.create(name);

res.status(STATUS.CREATED).send(newUser);
};

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

if (isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters.');

return;
}

const user = await usersService.getOneById(id);

if (!user) {
res.status(STATUS.NOT_FOUND).send('The user with this ID does not exist.');

return;
}

await usersService.remove(id);

res
.status(STATUS.NO_CONTENT)
.send(`The user with ${id} ID was successfully deleted.`);
};

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

if (typeof name !== 'string' || isNaN(Number(id))) {
res
.status(STATUS.BAD_REQUEST)
.send('Incorrect or missing request parameters or data.');

return;
}

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

if (!user) {
res.status(STATUS.NOT_FOUND).send('The user with this ID does not exist.');

return;
}

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

module.exports = {
getAll,
getOne,
create,
remove,
update,
};
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 usersRoute = require('./routes/users.route');
const expensesRoute = require('./routes/expenses.route');

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

app.use(express.json());
app.use('/users', usersRoute.route);
app.use('/expenses', expensesRoute.route);

return app;
}

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

module.exports = {
Expand Down
15 changes: 14 additions & 1 deletion src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
'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.TEXT,
allowNull: false,
},
},
{ tableName: 'users', updatedAt: false, createdAt: false },
);

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

const route = express.Router();

route.get('/', expensesController.getAll);

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

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

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

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

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

const route = express.Router();

route.get('/', usersController.getAll);

route.get('/:id', usersController.getOne);

route.post('/', usersController.create);

route.delete('/:id', usersController.remove);

route.patch('/:id', usersController.update);

module.exports = {
route,
};
Loading
Loading