-
Notifications
You must be signed in to change notification settings - Fork 449
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
base: master
Are you sure you want to change the base?
solution #259
Conversation
There was a problem hiding this 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
src/controllers/expenseController.js
Outdated
|
||
module.exports = { | ||
async getAll(req, res) { | ||
const { userId, categories, from, to } = req.query; |
There was a problem hiding this comment.
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.
src/controllers/expenseController.js
Outdated
to, | ||
}); | ||
|
||
res.status(200).send(expenses.map(expenseService.format)); |
There was a problem hiding this comment.
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.
src/controllers/expenseController.js
Outdated
const expenses = await expenseService.getAllFiltered({ | ||
userId: numberUserId, | ||
categories, | ||
from, | ||
to, | ||
}); | ||
|
||
res.status(200).send(expenses.map(expenseService.format)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |
} |
src/routes/expenseRoute.js
Outdated
|
||
router.get('/', expenseController.getAll); | ||
router.get('/:id', expenseController.getOne); | ||
router.post('/', expenseController.create); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
router.post('/', expenseController.create); | |
router.post('/', yourMiddlewareToCheckAndSanitizeTheRequest, expenseController.create); |
Middlewares should be placed here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 🚀
No description provided.