-
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
6828bc3
commit e355d8d
Showing
10 changed files
with
406 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
'use strict'; | ||
|
||
const expenseService = require('../services/expense.service'); | ||
const usersService = require('../services/user.service'); | ||
|
||
const getAll = async(req, res) => { | ||
const { categories, userId, from, to } = req.query; | ||
|
||
const expenses = await expenseService.get({ | ||
categories, | ||
userId, | ||
from, | ||
to, | ||
}); | ||
|
||
res.send(expenses); | ||
}; | ||
|
||
const getOne = async(req, res) => { | ||
const id = parseInt(req.params.id); | ||
|
||
if (isNaN(id)) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const expense = await expenseService.getById(id); | ||
|
||
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 usersService.getById(userId); | ||
|
||
if (!user) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
if (!userId || !title || !spentAt || !amount) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const expense = await expenseService.create({ | ||
userId: user.id, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
|
||
res.statusCode = 201; | ||
res.send(expense); | ||
}; | ||
|
||
const update = async(req, res) => { | ||
const { id } = req.params; | ||
const body = req.body; | ||
const expense = await expenseService.getById(+id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await expenseService.update(+id, body); | ||
|
||
res.send(await expenseService.getById(+id)); | ||
}; | ||
|
||
const remove = async(req, res) => { | ||
const { id } = req.params; | ||
const expense = await expenseService.getById(+id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await expenseService.remove(expense.id); | ||
|
||
res.sendStatus(204); | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
getOne, | ||
create, | ||
update, | ||
remove, | ||
}; |
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,81 @@ | ||
'use strict'; | ||
|
||
const usersService = require('../services/user.service'); | ||
|
||
const get = async(req, res) => { | ||
res.send(await usersService.getAll()); | ||
}; | ||
|
||
const getOne = async(req, res) => { | ||
const id = parseInt(req.params.id); | ||
const user = await usersService.getById(id); | ||
|
||
if (isNaN(id)) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
res.send(user); | ||
}; | ||
|
||
const create = async(req, res) => { | ||
const { name } = req.body; | ||
|
||
if (!name || typeof name !== 'string') { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const user = await usersService.create(name); | ||
|
||
res.statusCode = 201; | ||
res.send(user); | ||
}; | ||
|
||
const update = async(req, res) => { | ||
const { id } = req.params; | ||
const { name } = req.body; | ||
|
||
const user = await usersService.getById(+id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await usersService.update(+id, name); | ||
|
||
res.send(await usersService.getById(+id)); | ||
}; | ||
|
||
const remove = async(req, res) => { | ||
const { id } = req.params; | ||
const user = await usersService.getById(+id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await usersService.remove(user.id); | ||
|
||
res.sendStatus(204); | ||
}; | ||
|
||
module.exports = { | ||
get, | ||
getOne, | ||
create, | ||
update, | ||
remove, | ||
}; |
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
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
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,18 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const expensesContoller = require('../controllers/expense.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', expensesContoller.getAll); | ||
|
||
router.post('/', express.json(), expensesContoller.create); | ||
|
||
router.get('/:id', expensesContoller.getOne); | ||
|
||
router.delete('/:id', expensesContoller.remove); | ||
|
||
router.patch('/:id', express.json(), expensesContoller.update); | ||
|
||
module.exports = router; |
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,18 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const usersContoller = require('../controllers/user.controller'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', usersContoller.get); | ||
|
||
router.post('/', express.json(), usersContoller.create); | ||
|
||
router.get('/:id', usersContoller.getOne); | ||
|
||
router.delete('/:id', usersContoller.remove); | ||
|
||
router.patch('/:id', express.json(), usersContoller.update); | ||
|
||
module.exports = router; |
Oops, something went wrong.