From aea224df28c95c1ccc672ba5d17ef3021b9af8c5 Mon Sep 17 00:00:00 2001 From: ViktoriiaRepo Date: Mon, 3 Jun 2024 23:26:03 +0300 Subject: [PATCH] Change after checking --- src/controllers/expenses.controllers.js | 32 +++++++------------------ src/controllers/user.controllers.js | 17 ++++++------- src/helpers/statusCode.js | 10 ++++++++ 3 files changed, 27 insertions(+), 32 deletions(-) create mode 100644 src/helpers/statusCode.js diff --git a/src/controllers/expenses.controllers.js b/src/controllers/expenses.controllers.js index bbd31679..bb9b8332 100644 --- a/src/controllers/expenses.controllers.js +++ b/src/controllers/expenses.controllers.js @@ -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; @@ -26,7 +18,7 @@ const getOne = async (req, res) => { return; } - res.statusCode = statusCode; + res.statusCode = 200; res.send(expense); }; @@ -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) => { @@ -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 = { diff --git a/src/controllers/user.controllers.js b/src/controllers/user.controllers.js index 71e24945..00273c03 100644 --- a/src/controllers/user.controllers.js +++ b/src/controllers/user.controllers.js @@ -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(); @@ -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) => { @@ -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) => { @@ -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; } diff --git a/src/helpers/statusCode.js b/src/helpers/statusCode.js new file mode 100644 index 00000000..6ba4d51a --- /dev/null +++ b/src/helpers/statusCode.js @@ -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 };