Skip to content

Commit

Permalink
Change after checking
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktoriiaRepo committed Jun 3, 2024
1 parent ef85966 commit aea224d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 32 deletions.
32 changes: 8 additions & 24 deletions src/controllers/expenses.controllers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
const expensesService = require('../services/expenses.services');

const statusCode = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
};
const { statusCode } = require('../helpers/statusCode');

const getAll = async (req, res) => {
const query = req.query;
Expand All @@ -26,7 +18,7 @@ const getOne = async (req, res) => {
return;
}

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

Expand All @@ -40,14 +32,10 @@ const add = async (req, res) => {
return;
}

try {
const expense = await expensesService.add(body);
const expense = await expensesService.add(body);

res.statusCode = statusCode.CREATED;
res.send(expense);
} catch (error) {
res.status(statusCode.INTERNAL_SERVER_ERROR).send(error.message);
}
res.statusCode = statusCode.CREATED;
res.send(expense);
};

const remove = async (req, res) => {
Expand All @@ -73,14 +61,10 @@ const update = async (req, res) => {
return;
}

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

res.statusCode = statusCode.OK;
res.send(updatedExpense);
} catch (error) {
res.status(statusCode.INTERNAL_SERVER_ERROR).send(error.message);
}
res.statusCode = statusCode.OK;
res.send(updatedExpense);
};

module.exports = {
Expand Down
17 changes: 9 additions & 8 deletions src/controllers/user.controllers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const userService = require('../services/user.services');
const { statusCode } = require('../helpers/statusCode');

const getAll = async (req, res) => {
const users = await userService.getAll();
Expand All @@ -12,26 +13,26 @@ const getOne = async (req, res) => {
const user = await userService.getUserById(id);

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

return;
}

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

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

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

return;
}

const user = await userService.create(name);

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

const remove = async (req, res) => {
Expand All @@ -40,14 +41,14 @@ const remove = async (req, res) => {
const user = await userService.getUserById(id);

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

return;
}

userService.remove(id);

res.sendStatus(204);
res.sendStatus(statusCode.NO_CONTENT);
};

const update = async (req, res) => {
Expand All @@ -57,13 +58,13 @@ const update = async (req, res) => {
const user = userService.getUserById(id);

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

return;
}

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

return;
}
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/statusCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const statusCode = {
OK: 200,
CREATED: 201,
NO_CONTENT: 204,
BAD_REQUEST: 400,
NOT_FOUND: 404,
UNPROCESSABLE_ENTITY: 422,
};

module.exports = { statusCode };

0 comments on commit aea224d

Please sign in to comment.