Skip to content

Commit

Permalink
create middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-na committed Jul 10, 2024
1 parent 6319c02 commit 5ff2c07
Show file tree
Hide file tree
Showing 17 changed files with 460 additions and 132 deletions.
146 changes: 68 additions & 78 deletions src/controllers/expenseController.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,91 @@
const expenseService = require('../services/expenseService');
const userService = require('../services/userService');

module.exports = {
async getAll(req, res) {
const { userId, categories, from, to } = req.query;
const numberUserId = parseInt(userId);

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

res.status(200).send(expenses.map(expenseService.format));
try {
const { userId, categories, from, to } = req.query;
const numberUserId = +userId;
const validCategories = categories ? [categories].flat() : null;

const expenses = await expenseService.getAllFiltered({
userId: numberUserId,
categories: validCategories,
from,
to,
});

res.status(200).send(expenses.map(expenseService.format));
} catch (error) {
res.status(500).send('Server error');
}
},
async getOne(req, res) {
const id = parseInt(req.params.id);
try {
const id = +req.params.id;

const expense = await expenseService.getById(id);
const expense = await expenseService.getById(id);

if (!expense) {
res.sendStatus(404);
if (!expense) {
res.status(404).send('No expenses found');

return;
}
return;
}

res.status(200).send(expenseService.format(expense));
res.status(200).send(expenseService.format(expense));
} catch (error) {
res.status(500).send('Server error');
}
},
async create(req, res) {
const { userId, spentAt, title, amount, category, note } = req.body;

const foundUser = await userService.getById(userId);

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

return;
try {
const { userId, spentAt, title, amount, category, note } = req.body;

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

res.status(201).send(expenseService.format(expense));
} catch (error) {
res.status(500).send('Server error');
}

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

res.status(201).send(expenseService.format(expense));
},
async update(req, res) {
const currentId = parseInt(req.params.id);
const { id, userId, spentAt, title, amount, category, note } = req.body;

const foundExpense = await expenseService.getById(currentId);

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

return;
}

if (!id && !userId && !spentAt && !title && !amount && !category && !note) {
res.sendStatus(400);

return;
try {
const currentId = +req.params.id;
const { id, userId, spentAt, title, amount, category, note } = req.body;

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

const updatedExpense = await expenseService.getById(id ?? currentId);

res.status(200).send(expenseService.format(updatedExpense));
} catch (error) {
res.status(500).send('Server error');
}

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

const updatedExpense = await expenseService.getById(id ?? currentId);

res.status(200).send(expenseService.format(updatedExpense));
},
async remove(req, res) {
const id = parseInt(req.params.id);

const foundExpense = await expenseService.getById(id);
try {
const id = +req.params.id;

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

return;
res.sendStatus(204);
} catch (error) {
res.status(500).send('Server error');
}

await expenseService.remove(id);

res.sendStatus(204);
},
};
82 changes: 37 additions & 45 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,66 @@ const userService = require('../services/userService');

module.exports = {
async getAll(_req, res) {
const users = await userService.getAll();
try {
const users = await userService.getAll();

res.status(200).send(users);
res.status(200).send(users);
} catch (error) {
res.status(500).send('Server error');
}
},
async getOne(req, res) {
const id = parseInt(req.params.id);
try {
const id = +req.params.id;

const user = await userService.getById(id);
const user = await userService.getById(id);

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

return;
}
return;
}

res.status(200).send(user);
res.status(200).send(user);
} catch (error) {
res.status(500).send('Server error');
}
},
async create(req, res) {
const { name } = req.body;
try {
const { name } = req.body;

if (!name) {
res.sendStatus(400);
const user = await userService.create({ name });

return;
res.status(201).send(user);
} catch (error) {
res.status(500).send('Server error');
}

const user = await userService.create({ name });

res.status(201).send(user);
},
async update(req, res) {
const currentId = parseInt(req.params.id);

const { name, id } = req.body;

if (!name && !id) {
res.sendStatus(400);
try {
const currentId = +req.params.id;

return;
}
const { name, id } = req.body;

const foundUser = await userService.getById(currentId);
await userService.update({ currentId, id, name });

if (!foundUser) {
res.sendStatus(404);
const updatedUser = await userService.getById(id ?? currentId);

return;
res.status(200).send(updatedUser);
} catch (error) {
res.status(500).send('Server error');
}

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

const updatedUser = await userService.getById(id ?? currentId);

res.status(200).send(updatedUser);
},
async remove(req, res) {
const id = parseInt(req.params.id);
try {
const id = +req.params.id;

const foundUser = await userService.getById(id);
await userService.remove(id);

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

return;
res.sendStatus(204);
} catch (error) {
res.status(500).send('Server error');
}

await userService.remove(id);

res.sendStatus(204);
},
};
9 changes: 9 additions & 0 deletions src/middleware/expense/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { validateCreateBody } = require('./validateCreateBody');
const { validateGetAllQuery } = require('./validateGetAllQuery');
const { validateUpdateBody } = require('./validateUpdateBody');

module.exports = {
validateGetAllQuery,
validateCreateBody,
validateUpdateBody,
};
42 changes: 42 additions & 0 deletions src/middleware/expense/validateCreateBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const {
isValidId,
isValidDate,
isValidString,
} = require('../../utils/validators');

module.exports.validateCreateBody = (req, res, next) => {
const { userId, spentAt, title, amount, category, note } = req.body;
const errors = [];

if (!isValidId(userId)) {
errors.push('Invalid property userId in the request body');
}

if (!isValidDate(new Date(spentAt))) {
errors.push('Invalid property spentAt in the request body');
}

if (!isValidString(title)) {
errors.push('Invalid property title in the request body');
}

if (isNaN(+amount)) {
errors.push('Invalid property amount in the request body');
}

if (!isValidString(category)) {
errors.push('Invalid property category in the request body');
}

if (note && !isValidString(note)) {
errors.push('Invalid property note in the request body');
}

if (errors.length) {
res.status(400).send(errors);

return;
}

next();
};
40 changes: 40 additions & 0 deletions src/middleware/expense/validateGetAllQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const {
isValidId,
isValidDate,
isValidString,
} = require('../../utils/validators');

module.exports.validateGetAllQuery = (req, res, next) => {
const { userId, categories, from, to } = req.query;
const errors = [];

if (userId && !isValidId(userId)) {
errors.push('Invalid "userId" query');
}

if (from && !isValidDate(new Date(from))) {
errors.push('Invalid "from" query');
}

if (from && !isValidDate(new Date(to))) {
errors.push('Invalid "to" query');
}

if (categories) {
const isValid = Array.isArray(categories)
? categories.every(isValidString)
: isValidString(categories);

if (!isValid) {
errors.push('Invalid "categories" query');
}
}

if (errors.length) {
res.status(400).send(errors);

return;
}

next();
};
Loading

0 comments on commit 5ff2c07

Please sign in to comment.