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

Solution #287

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

const get = async (req, res) => {
if (Object.keys(req.query).length === 0) {
res.send(await expenseService.getAll());

return;
}

const { userId, categories, from, to } = req.query;

const expenses = await expenseService.getFiltered({
userId,
categories: categories ? categories.split(',') : [],
from,
to,
});

res.send(expenses);
};

const create = async (req, res) => {
const { userId, spentAt, title, amount, category, note } = req.body;

if (
!userId ||
!spentAt ||
!title ||
!amount ||
!userService.getById(userId)
) {
res.sendStatus(400);

return;
}

const expense = await expenseService.create({
userId,
spentAt,
title,
amount,
category,
note,
});

res.status(201).send(expense);
};

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

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

return;
}

const expense = await expenseService.getById(id);

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

return;
}

res.send(expense);
};

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

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

return;
}

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

const update = async (req, res) => {
const id = +req.params.id;
const { spentAt, title, amount, category, note } = req.body;

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

return;
}

await expenseService.update({
id,
spentAt,
title,
amount,
category,
note,
});

const updatedExpense = await expenseService.getById(id);

res.send(updatedExpense);
};

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

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

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

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

return;
}

const user = await userService.create(name);

res.status(201).send(user);
};

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

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

return;
}

const user = await userService.getById(id);

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

return;
}

res.send(user);
};

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

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

return;
}

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

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

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

return;
}

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

return;
}

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

const updatedUser = await userService.getById(id);

res.send(updatedUser);
};

module.exports = {
get,
create,
getOne,
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 { userRouter } = require('./routes/user.router');
const { expenseRouter } = require('./routes/expense.router');

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

app.use(express.json());

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

return app;
}

module.exports = {
createServer,
Expand Down
8 changes: 1 addition & 7 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { Sequelize } = require('sequelize');
const utils = require('util');

// Needed for testing purposes, do not remove
require('dotenv').config();
global.TextEncoder = utils.TextEncoder;

Expand All @@ -15,18 +14,13 @@ const {
POSTGRES_DB,
} = process.env;

/*
All credentials setted to default values (exsept password - it is exapmle)
replace if needed with your own
*/

const sequelize = new Sequelize({
database: POSTGRES_DB || 'postgres',
username: POSTGRES_USER || 'postgres',
host: POSTGRES_HOST || 'localhost',
dialect: 'postgres',
port: POSTGRES_PORT || 5432,
password: POSTGRES_PASSWORD || '123',
password: POSTGRES_PASSWORD || '2223',
});

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

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

const Expense = sequelize.define(
// your code goes here
'Expense',
{
title: {
type: DataTypes.STRING,
allowNull: false,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'user_id',
},
spentAt: {
type: DataTypes.DATE,
allowNull: false,
field: 'spent_at',
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
category: {
type: DataTypes.STRING,
allowNull: true,
},
note: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
tableName: 'expenses',
updatedAt: false,
createdAt: 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 { DataTypes } = require('sequelize');
const { sequelize } = require('../db.js');

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

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

const router = express.Router();

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

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

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

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

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

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

const router = express.Router();

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

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

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

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

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

module.exports = {
userRouter: router,
};
Loading
Loading