-
Notifications
You must be signed in to change notification settings - Fork 663
app-with-db #522
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
Open
Vlshedevr
wants to merge
6
commits into
mate-academy:master
Choose a base branch
from
Vlshedevr:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
app-with-db #522
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
|
||
| res.status(200).send(categories); | ||
|
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); | ||
|
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)) { | ||
|
Vlshedevr marked this conversation as resolved.
|
||
| return res.status(400).send('Bad request'); | ||
| } | ||
|
|
||
| const category = await categoryServices.getOne(normalaizedId); | ||
|
|
||
|
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, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
|
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'); | ||
| } | ||
|
Vlshedevr marked this conversation as resolved.
|
||
| } else { | ||
| userId = null; | ||
| } | ||
|
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'); | ||
| } | ||
|
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'); | ||
|
Vlshedevr marked this conversation as resolved.
|
||
| } | ||
|
|
||
| to = new Date(parsed); | ||
| } else { | ||
|
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) { | ||
|
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; | ||
|
Vlshedevr marked this conversation as resolved.
|
||
| const normalizedId = Number(id); | ||
|
|
||
|
Vlshedevr marked this conversation as resolved.
|
||
| if (isNaN(normalizedId)) { | ||
| return res.status(400).send('Bad request'); | ||
| } | ||
|
|
||
|
Vlshedevr marked this conversation as resolved.
|
||
| const expense = await expenseServices.getOne(normalizedId); | ||
|
|
||
| if (!expense) { | ||
| return res.status(404).send('Not found'); | ||
| } | ||
|
|
||
|
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, | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.