Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand Down
84 changes: 84 additions & 0 deletions src/controlers/categories.controler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const categoryServices = require('../services/categories.services');

const getAll = async (req, res) => {
const categories = await categoryServices.get();

Comment thread
Vlshedevr marked this conversation as resolved.
res.status(200).send(categories);
Comment thread
Vlshedevr marked this conversation as resolved.
};

const getOne = async (req, res) => {
const { id } = req.params;
const normalaizedId = Number(id);

if (isNaN(normalaizedId)) {
return res.status(400).send('Bad request');
}

const category = await categoryServices.getOne(normalaizedId);
Comment thread
Vlshedevr marked this conversation as resolved.

if (!category) {
return res.status(404).send('Not found');
}

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

const add = async (req, res) => {
const { title } = req.body;

if (!title || typeof title !== 'string') {
return res.status(400).send('Bad request');
}

const newCategory = await categoryServices.add(title.trim());

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

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

if (isNaN(normalaizedId)) {
Comment thread
Vlshedevr marked this conversation as resolved.
return res.status(400).send('Bad request');
}

const category = await categoryServices.getOne(normalaizedId);

Comment thread
Vlshedevr marked this conversation as resolved.
if (!category) {
return res.status(404).send('Not found');
}

await categoryServices.remove(normalaizedId);
res.sendStatus(204);
};

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

if (isNaN(normalaizedId) || !title || typeof title !== 'string') {
return res.status(400).send('Bad request');
}

const category = await categoryServices.getOne(normalaizedId);

if (!category) {
return res.status(404).send('Not found');
}

await categoryServices.update({ id: normalaizedId, title: title.trim() });

const updatedCategory = await categoryServices.getOne(normalaizedId);

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

module.exports = {
getAll,
getOne,
add,
remove,
update,
};
223 changes: 223 additions & 0 deletions src/controlers/expenses.controler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
const expenseServices = require('../services/expenses.services');
const userServices = require('../services/user.services');
Comment thread
Vlshedevr marked this conversation as resolved.
const categoryServices = require('../services/categories.services');

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

if (userId) {
userId = Number(userId);

if (isNaN(userId)) {
return res.status(400).send('Bad request');
}
Comment thread
Vlshedevr marked this conversation as resolved.
} else {
userId = null;
}
Comment thread
Vlshedevr marked this conversation as resolved.

if (categoriesId && categoriesId.length > 0) {
categoriesId = categoriesId
.split(',')
.map((category) => Number(category))
.filter((category) => !isNaN(category));
} else {
categoriesId = null;
}

if (from) {
const parsed = Date.parse(from);

if (isNaN(parsed)) {
return res.status(400).send('Bad request');
}
Comment thread
Vlshedevr marked this conversation as resolved.

from = new Date(parsed);
} else {
from = null;
}

if (to) {
const parsed = Date.parse(to);

if (isNaN(parsed)) {
return res.status(400).send('Bad request');
Comment thread
Vlshedevr marked this conversation as resolved.
}

to = new Date(parsed);
} else {
Comment thread
Vlshedevr marked this conversation as resolved.
to = null;
}

const data = await expenseServices.get({
userId,
categoriesId,
from,
to,
});

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

const getOne = async (req, res) => {
const { id } = req.params;
const normalaizedId = Number(id);

if (isNaN(normalaizedId)) {
return res.status(400).send('Bad request');
}

const expense = await expenseServices.getOne(normalaizedId);

if (!expense) {
Comment thread
Vlshedevr marked this conversation as resolved.
return res.status(404).send('Not found');
}

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

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

const normalaizedId = Number(userId);
const normalaizedAmount = Number(amount);
const normalaizedTitle = title ? title.trim() : null;
const normalaizedSpentAt = spentAt ? Date.parse(spentAt) : Date.now();
const normalaizedCategoryId = categoryId ? Number(categoryId) : null;

const checker =
isNaN(normalaizedId) ||
isNaN(normalaizedAmount) ||
!normalaizedTitle ||
isNaN(normalaizedSpentAt) ||
(categoryId && isNaN(normalaizedCategoryId));

if (checker) {
return res.status(400).send('Bad request');
}

const user = await userServices.getOne(normalaizedId);

if (!user) {
return res.status(400).send('Not found');
}

if (categoryId) {
const category = await categoryServices.getOne(normalaizedCategoryId);

if (!category) {
return res.status(404).send('Not found');
}
}

const newExpense = await expenseServices.add({
userId: normalaizedId,
title: normalaizedTitle,
amount: normalaizedAmount,
spentAt: normalaizedSpentAt,
categoryId: normalaizedCategoryId,
note: note ? note.trim() : null,
});

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

const remove = async (req, res) => {
const { id } = req.params;
Comment thread
Vlshedevr marked this conversation as resolved.
const normalizedId = Number(id);

Comment thread
Vlshedevr marked this conversation as resolved.
if (isNaN(normalizedId)) {
return res.status(400).send('Bad request');
}

Comment thread
Vlshedevr marked this conversation as resolved.
const expense = await expenseServices.getOne(normalizedId);

if (!expense) {
return res.status(404).send('Not found');
}

Comment thread
Vlshedevr marked this conversation as resolved.
await expenseServices.remove(normalizedId);
res.sendStatus(204);
};

const update = async (req, res) => {
const { id } = req.params;
const { spentAt, title, amount, categoryId, note } = req.body;

const normalaizedId = Number(id);

if (isNaN(normalaizedId)) {
return res.status(400).send('Bad request');
}

const expense = await expenseServices.getOne(normalaizedId);

if (!expense) {
return res.status(404).send('Not found');
}

const data = {};

if (title !== undefined) {
const normalaizedTitle = title.trim();

if (!normalaizedTitle) {
return res.status(400).send('Bad request');
}

data.title = normalaizedTitle;
}

if (amount !== undefined) {
const normalaizedAmount = Number(amount);

if (isNaN(normalaizedAmount)) {
return res.status(400).send('Bad request');
}

data.amount = normalaizedAmount;
}

if (spentAt !== undefined) {
const normalaizedSpentAt = Date.parse(spentAt);

if (isNaN(normalaizedSpentAt)) {
return res.status(400).send('Bad request');
}

data.spentAt = new Date(normalaizedSpentAt);
}

if (categoryId !== undefined) {
const normalaizedCategoryId = Number(categoryId);

if (isNaN(normalaizedCategoryId)) {
return res.status(400).send('Bad request');
}

const category = await categoryServices.getOne(normalaizedCategoryId);

if (!category) {
return res.status(404).send('Category not found');
}

data.categoryId = normalaizedCategoryId;
}

if (note !== undefined) {
data.note = note.trim() || null;
}

await expenseServices.update({ id: normalaizedId, ...data });

const updatedExpense = await expenseServices.getOne(normalaizedId);

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

module.exports = {
getAll,
getOne,
add,
remove,
update,
};
Loading
Loading