Skip to content

Commit

Permalink
add solution for app with db
Browse files Browse the repository at this point in the history
  • Loading branch information
PavloMykhalov committed Feb 19, 2024
1 parent 6828bc3 commit e355d8d
Show file tree
Hide file tree
Showing 10 changed files with 406 additions and 7 deletions.
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 @@
'use strict';

const expenseService = require('../services/expense.service');
const usersService = require('../services/user.service');

const getAll = async(req, res) => {
const { categories, userId, from, to } = req.query;

const expenses = await expenseService.get({
categories,
userId,
from,
to,
});

res.send(expenses);
};

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

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

return;
}

const expense = await expenseService.getById(id);

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

return;
}

res.send(expense);
};

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

const user = await usersService.getById(userId);

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

return;
}

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

return;
}

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

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

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

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

return;
}

await expenseService.update(+id, body);

res.send(await expenseService.getById(+id));
};

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

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

return;
}

await expenseService.remove(expense.id);

res.sendStatus(204);
};

module.exports = {
getAll,
getOne,
create,
update,
remove,
};
81 changes: 81 additions & 0 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

const usersService = require('../services/user.service');

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

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

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

return;
}

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

return;
}

res.send(user);
};

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

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

return;
}

const user = await usersService.create(name);

res.statusCode = 201;
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(404);

return;
}

await usersService.update(+id, name);

res.send(await usersService.getById(+id));
};

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

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

return;
}

await usersService.remove(user.id);

res.sendStatus(204);
};

module.exports = {
get,
getOne,
create,
update,
remove,
};
11 changes: 10 additions & 1 deletion src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict';

const express = require('express');
const userRouter = require('./routes/user.route');
const expenseRouter = require('./routes/expense.route');

const createServer = () => {
// your code goes here
const app = express();

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

return app;
};

module.exports = {
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 || '123456789',
});

module.exports = {
Expand Down
38 changes: 36 additions & 2 deletions 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
const Expense = sequelize.define('Expense', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
userId: {
type: DataTypes.INTEGER,
allowNull: false,
},
spentAt: {
type: DataTypes.DATE,
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
19 changes: 16 additions & 3 deletions src/models/User.model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
'use strict';

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

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

module.exports = {
User,
Expand Down
18 changes: 18 additions & 0 deletions src/routes/expense.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const express = require('express');
const expensesContoller = require('../controllers/expense.controller');

const router = express.Router();

router.get('/', expensesContoller.getAll);

router.post('/', express.json(), expensesContoller.create);

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

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

router.patch('/:id', express.json(), expensesContoller.update);

module.exports = router;
18 changes: 18 additions & 0 deletions src/routes/user.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const express = require('express');
const usersContoller = require('../controllers/user.controller');

const router = express.Router();

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

router.post('/', express.json(), usersContoller.create);

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

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

router.patch('/:id', express.json(), usersContoller.update);

module.exports = router;
Loading

0 comments on commit e355d8d

Please sign in to comment.