-
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
3a2abbf
commit 4061f05
Showing
12 changed files
with
378 additions
and
6 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,75 @@ | ||
const expensesService = require('../services/expenses.services'); | ||
|
||
const getAll = async (req, res) => { | ||
const query = req.query; | ||
const expenses = await expensesService.get(query); | ||
|
||
res.send(expenses); | ||
}; | ||
|
||
const getOne = async (req, res) => { | ||
const { id } = req.params; | ||
const expense = await expensesService.getById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
res.statusCode = 200; | ||
res.send(expense); | ||
}; | ||
|
||
const add = async (req, res) => { | ||
const body = req.body; | ||
const userId = parseInt(body.userId); | ||
|
||
if (!userId) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const expense = await expensesService.add(body); | ||
|
||
res.statusCode = 201; | ||
res.send(expense); | ||
}; | ||
|
||
const remove = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
try { | ||
await expensesService.remove(id); | ||
|
||
res.sendStatus(204); | ||
} catch (error) { | ||
res.sendStatus(404); | ||
} | ||
}; | ||
|
||
const update = async (req, res) => { | ||
const { id } = req.params; | ||
const body = req.body; | ||
const expense = await expensesService.getById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
const updatedExpense = await expensesService.update(expense.id, body); | ||
|
||
res.statusCode = 200; | ||
res.send(updatedExpense); | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
add, | ||
getOne, | ||
remove, | ||
update, | ||
}; |
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,84 @@ | ||
const userService = require('../services/user.services'); | ||
|
||
const getAll = async (req, res) => { | ||
const users = await userService.getAll(); | ||
|
||
res.send(users); | ||
}; | ||
|
||
const getOne = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
const user = await userService.getUserById(id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
res.status(200).send(user); | ||
}; | ||
|
||
const create = async (req, res) => { | ||
const { name } = req.body; | ||
|
||
if (!name) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const user = await userService.create(name); | ||
|
||
res.statusCode = 201; | ||
|
||
res.status(201).send(user); | ||
}; | ||
|
||
const remove = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
const user = await userService.getUserById(id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
userService.remove(id); | ||
|
||
res.sendStatus(204); | ||
}; | ||
|
||
const update = async (req, res) => { | ||
const { id } = req.params; | ||
const { name } = req.body; | ||
|
||
const user = userService.getUserById(id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
if (typeof name !== 'string') { | ||
res.sendStatus(422); | ||
|
||
return; | ||
} | ||
|
||
const updatedUser = await userService.update({ id, name }); | ||
|
||
res.send(updatedUser); | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
getOne, | ||
create, | ||
remove, | ||
update, | ||
}; |
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,19 @@ | ||
const express = require('express'); | ||
|
||
const expenseConroller = require('../controllers/expenses.controllers'); | ||
|
||
const expensesRouter = express.Router(); | ||
|
||
expensesRouter.get('/', expenseConroller.getAll); | ||
|
||
expensesRouter.get('/:id', expenseConroller.getOne); | ||
|
||
expensesRouter.post('/', expenseConroller.add); | ||
|
||
expensesRouter.delete('/:id', expenseConroller.remove); | ||
|
||
expensesRouter.patch('/:id', expenseConroller.update); | ||
|
||
module.exports = { | ||
expensesRouter, | ||
}; |
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,19 @@ | ||
const express = require('express'); | ||
|
||
const userController = require('../controllers/user.controllers'); | ||
|
||
const userRouter = express.Router(); | ||
|
||
userRouter.get('/', userController.getAll); | ||
|
||
userRouter.get('/:id', userController.getOne); | ||
|
||
userRouter.post('/', userController.create); | ||
|
||
userRouter.delete('/:id', userController.remove); | ||
|
||
userRouter.patch('/:id', userController.update); | ||
|
||
module.exports = { | ||
userRouter, | ||
}; |
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,47 @@ | ||
const { Expense } = require('../models/Expense.model'); | ||
const { getFilteredExpenses } = require('../utils/getFilteredxpense'); | ||
|
||
const get = async (query) => { | ||
const filteredExpenses = getFilteredExpenses(query); | ||
|
||
return Expense.findAll({ | ||
where: filteredExpenses, | ||
}); | ||
}; | ||
|
||
const getById = (id) => { | ||
return Expense.findByPk(id); | ||
}; | ||
|
||
const add = async (data) => { | ||
return Expense.create(data); | ||
}; | ||
|
||
const remove = async (id) => { | ||
Expense.destroy({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
}; | ||
|
||
const update = async (id, data) => { | ||
await Expense.update( | ||
{ ...data }, | ||
{ | ||
where: { | ||
id, | ||
}, | ||
}, | ||
); | ||
|
||
return Expense.findByPk(id); | ||
}; | ||
|
||
module.exports = { | ||
get, | ||
add, | ||
getById, | ||
remove, | ||
update, | ||
}; |
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 { User } = require('../models/User.model'); | ||
|
||
const getAll = () => { | ||
return User.findAll(); | ||
}; | ||
|
||
const getUserById = (id) => { | ||
return User.findByPk(id); | ||
}; | ||
|
||
const create = (name) => { | ||
return User.create({ name }); | ||
}; | ||
|
||
const remove = async (id) => { | ||
User.destroy({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
}; | ||
|
||
const update = async ({ id, name }) => { | ||
await User.update( | ||
{ name }, | ||
{ | ||
where: { | ||
id, | ||
}, | ||
}, | ||
); | ||
|
||
return getUserById(id); | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
getUserById, | ||
create, | ||
remove, | ||
update, | ||
}; |
Oops, something went wrong.