-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6319c02
commit 5ff2c07
Showing
17 changed files
with
460 additions
and
132 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,101 +1,91 @@ | ||
const expenseService = require('../services/expenseService'); | ||
const userService = require('../services/userService'); | ||
|
||
module.exports = { | ||
async getAll(req, res) { | ||
const { userId, categories, from, to } = req.query; | ||
const numberUserId = parseInt(userId); | ||
|
||
const expenses = await expenseService.getAllFiltered({ | ||
userId: numberUserId, | ||
categories, | ||
from, | ||
to, | ||
}); | ||
|
||
res.status(200).send(expenses.map(expenseService.format)); | ||
try { | ||
const { userId, categories, from, to } = req.query; | ||
const numberUserId = +userId; | ||
const validCategories = categories ? [categories].flat() : null; | ||
|
||
const expenses = await expenseService.getAllFiltered({ | ||
userId: numberUserId, | ||
categories: validCategories, | ||
from, | ||
to, | ||
}); | ||
|
||
res.status(200).send(expenses.map(expenseService.format)); | ||
} catch (error) { | ||
res.status(500).send('Server error'); | ||
} | ||
}, | ||
async getOne(req, res) { | ||
const id = parseInt(req.params.id); | ||
try { | ||
const id = +req.params.id; | ||
|
||
const expense = await expenseService.getById(id); | ||
const expense = await expenseService.getById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
if (!expense) { | ||
res.status(404).send('No expenses found'); | ||
|
||
return; | ||
} | ||
return; | ||
} | ||
|
||
res.status(200).send(expenseService.format(expense)); | ||
res.status(200).send(expenseService.format(expense)); | ||
} catch (error) { | ||
res.status(500).send('Server error'); | ||
} | ||
}, | ||
async create(req, res) { | ||
const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
const foundUser = await userService.getById(userId); | ||
|
||
if (!userId || !spentAt || !title || !amount || !category || !foundUser) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
try { | ||
const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
const expense = await expenseService.create({ | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
|
||
res.status(201).send(expenseService.format(expense)); | ||
} catch (error) { | ||
res.status(500).send('Server error'); | ||
} | ||
|
||
const expense = await expenseService.create({ | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
|
||
res.status(201).send(expenseService.format(expense)); | ||
}, | ||
async update(req, res) { | ||
const currentId = parseInt(req.params.id); | ||
const { id, userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
const foundExpense = await expenseService.getById(currentId); | ||
|
||
if (!foundExpense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
if (!id && !userId && !spentAt && !title && !amount && !category && !note) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
try { | ||
const currentId = +req.params.id; | ||
const { id, userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
await expenseService.update({ | ||
currentId, | ||
id, | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
|
||
const updatedExpense = await expenseService.getById(id ?? currentId); | ||
|
||
res.status(200).send(expenseService.format(updatedExpense)); | ||
} catch (error) { | ||
res.status(500).send('Server error'); | ||
} | ||
|
||
await expenseService.update({ | ||
currentId, | ||
id, | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
|
||
const updatedExpense = await expenseService.getById(id ?? currentId); | ||
|
||
res.status(200).send(expenseService.format(updatedExpense)); | ||
}, | ||
async remove(req, res) { | ||
const id = parseInt(req.params.id); | ||
|
||
const foundExpense = await expenseService.getById(id); | ||
try { | ||
const id = +req.params.id; | ||
|
||
if (!foundExpense) { | ||
res.sendStatus(404); | ||
await expenseService.remove(id); | ||
|
||
return; | ||
res.sendStatus(204); | ||
} catch (error) { | ||
res.status(500).send('Server error'); | ||
} | ||
|
||
await expenseService.remove(id); | ||
|
||
res.sendStatus(204); | ||
}, | ||
}; |
This file contains 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 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,9 @@ | ||
const { validateCreateBody } = require('./validateCreateBody'); | ||
const { validateGetAllQuery } = require('./validateGetAllQuery'); | ||
const { validateUpdateBody } = require('./validateUpdateBody'); | ||
|
||
module.exports = { | ||
validateGetAllQuery, | ||
validateCreateBody, | ||
validateUpdateBody, | ||
}; |
This file contains 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,42 @@ | ||
const { | ||
isValidId, | ||
isValidDate, | ||
isValidString, | ||
} = require('../../utils/validators'); | ||
|
||
module.exports.validateCreateBody = (req, res, next) => { | ||
const { userId, spentAt, title, amount, category, note } = req.body; | ||
const errors = []; | ||
|
||
if (!isValidId(userId)) { | ||
errors.push('Invalid property userId in the request body'); | ||
} | ||
|
||
if (!isValidDate(new Date(spentAt))) { | ||
errors.push('Invalid property spentAt in the request body'); | ||
} | ||
|
||
if (!isValidString(title)) { | ||
errors.push('Invalid property title in the request body'); | ||
} | ||
|
||
if (isNaN(+amount)) { | ||
errors.push('Invalid property amount in the request body'); | ||
} | ||
|
||
if (!isValidString(category)) { | ||
errors.push('Invalid property category in the request body'); | ||
} | ||
|
||
if (note && !isValidString(note)) { | ||
errors.push('Invalid property note in the request body'); | ||
} | ||
|
||
if (errors.length) { | ||
res.status(400).send(errors); | ||
|
||
return; | ||
} | ||
|
||
next(); | ||
}; |
This file contains 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,40 @@ | ||
const { | ||
isValidId, | ||
isValidDate, | ||
isValidString, | ||
} = require('../../utils/validators'); | ||
|
||
module.exports.validateGetAllQuery = (req, res, next) => { | ||
const { userId, categories, from, to } = req.query; | ||
const errors = []; | ||
|
||
if (userId && !isValidId(userId)) { | ||
errors.push('Invalid "userId" query'); | ||
} | ||
|
||
if (from && !isValidDate(new Date(from))) { | ||
errors.push('Invalid "from" query'); | ||
} | ||
|
||
if (from && !isValidDate(new Date(to))) { | ||
errors.push('Invalid "to" query'); | ||
} | ||
|
||
if (categories) { | ||
const isValid = Array.isArray(categories) | ||
? categories.every(isValidString) | ||
: isValidString(categories); | ||
|
||
if (!isValid) { | ||
errors.push('Invalid "categories" query'); | ||
} | ||
} | ||
|
||
if (errors.length) { | ||
res.status(400).send(errors); | ||
|
||
return; | ||
} | ||
|
||
next(); | ||
}; |
Oops, something went wrong.