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

solution #259

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

solution #259

wants to merge 2 commits into from

Conversation

yaroslav-na
Copy link

No description provided.

Copy link

@anastasiiavorobiova anastasiiavorobiova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Added a few comments to fix


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider creating a middleware folder and placing middleware here for checking and validating the user input from the request before passing it to the controller. The user data from the request should be sanitized. Also, you will be able to send 400 errors with additional information about errors.

to,
});

res.status(200).send(expenses.map(expenseService.format));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it would be better to wrap this bunch of code inside the try-catch block to handle errors related to the DB connection, etc. In this case, we should provide a client with a proper error code and avoid throwing an error object directly to the client.

Comment on lines 9 to 16
const expenses = await expenseService.getAllFiltered({
userId: numberUserId,
categories,
from,
to,
});

res.status(200).send(expenses.map(expenseService.format));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const expenses = await expenseService.getAllFiltered({
userId: numberUserId,
categories,
from,
to,
});
res.status(200).send(expenses.map(expenseService.format));
try {
const expenses = await expenseService.getAllFiltered({
userId: numberUserId,
categories,
from,
to,
});
res.status(200).send(expenses.map(expenseService.format));
} catch(err) {
res.statusCode = 500;
res.send('Something went wrong);
}


router.get('/', expenseController.getAll);
router.get('/:id', expenseController.getOne);
router.post('/', expenseController.create);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
router.post('/', expenseController.create);
router.post('/', yourMiddlewareToCheckAndSanitizeTheRequest, expenseController.create);

Middlewares should be placed here

Copy link

@lerastarynets lerastarynets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants