Skip to content

Commit

Permalink
Add task
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktoriiaRepo committed May 20, 2024
1 parent 3a2abbf commit 4061f05
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 6 deletions.
75 changes: 75 additions & 0 deletions src/controllers/expenses.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const expensesService = require('../services/expenses.services');

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(404);

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(400);

return;
}

const expense = await expensesService.add(body);

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

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

try {
await expensesService.remove(id);

res.sendStatus(204);
} catch (error) {
res.sendStatus(404);
}
};

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

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

return;
}

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

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

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

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(404);

return;
}

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

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

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

return;
}

const user = await userService.create(name);

res.statusCode = 201;

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

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

const user = await userService.getUserById(id);

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

return;
}

userService.remove(id);

res.sendStatus(204);
};

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

const user = userService.getUserById(id);

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

return;
}

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

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
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,
};
42 changes: 42 additions & 0 deletions src/services/user.services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { User } = require('../models/User.model');

const getAll = () => {
return User.findAll();
};

const getUserById = (id) => {
return User.findByPk(id);
};

const create = (name) => {
return User.create({ name });
};

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

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

return getUserById(id);
};

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

0 comments on commit 4061f05

Please sign in to comment.