Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete task #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
164 changes: 164 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"eslint": "^5.16.0",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-node": "^8.0.1",
"jest": "^29.7.0"
"jest": "^29.7.0",
"nodemon": "^3.0.3"
},
"mateAcademy": {
"projectType": "nodeJs"
Expand Down
126 changes: 126 additions & 0 deletions src/controllers/expense.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict';

const expenseService = require('../services/expense.service');
const userService = require('../services/user.service');

const get = async(req, res) => {
const { userId, categories, from, to } = req.query;

if (userId || categories || from || to) {
const parsedId = parseInt(userId, 10);

if (userId && isNaN(parsedId)) {
res.sendStatus(400);

return;
}

const expensesByQueries = await expenseService.getByQueries(req.query);

res.send(expensesByQueries);

return;
}

const allExpenses = await expenseService.getAll();

res.send(allExpenses);
};

const getOne = async(req, res) => {
const { parsedId } = req;

const expense = await expenseService.getById(parsedId);

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

return;
}

res.send(expense);
};

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

const user = await userService.getById(userId);
const isDateValid = !isNaN(Date.parse(spentAt));

if (!user
|| !isDateValid
|| typeof title !== 'string'
|| typeof amount !== 'number') {
res.sendStatus(400);

return;
}

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

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

const remove = async(req, res) => {
const { parsedId } = req;

const expense = expenseService.getById(parsedId);

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

return;
}

await expenseService.remove(parsedId);

res.sendStatus(204);
};

const update = async(req, res) => {
const { parsedId } = req;
const { title, amount, category, note } = req.body;

const expense = await expenseService.getById(parsedId);

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

return;
}

if ((title && typeof title !== 'string')
|| (amount && typeof amount !== 'number')
|| (category && typeof category !== 'string')
|| (note && typeof note !== 'string')) {
res.sendStatus(400);

return;
}

const expenseToUpdate = {
...expense,
...req.body,
};

await expenseService.update(parsedId, expenseToUpdate);

const updatedExpense = await expenseService.getById(parsedId);

res.send(updatedExpense);
};

module.exports = {
get,
getOne,
remove,
update,
create,
};
Loading
Loading