-
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
done #246
base: master
Are you sure you want to change the base?
done #246
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const expensesService = require('../services/expense.service.js'); | ||
const userService = require('../services/user.service.js'); | ||
|
||
const get = async (req, res) => { | ||
res.send(await expensesService.getAll(req.query)); | ||
}; | ||
|
||
const create = async (req, res) => { | ||
const expense = req.body; | ||
|
||
if (!(await userService.getById(expense.userId))) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const newExpense = await expensesService.create(expense); | ||
|
||
res.statusCode = 201; | ||
res.send(newExpense); | ||
}; | ||
|
||
const getOne = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
const expense = await expensesService.getById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
res.send(expense); | ||
}; | ||
|
||
const remove = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await expensesService.getById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await expensesService.remove(id); | ||
res.sendStatus(204); | ||
}; | ||
|
||
const update = async (req, res) => { | ||
const { id } = req.params; | ||
const data = req.body; | ||
|
||
const expense = await expensesService.getById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
if (!data) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const updatedExpense = await expensesService.update(expense, data); | ||
|
||
res.send(updatedExpense); | ||
}; | ||
|
||
module.exports = { | ||
get, | ||
create, | ||
getOne, | ||
remove, | ||
update, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const userServices = require('../services/user.service.js'); | ||
|
||
const get = async (req, res) => { | ||
res.send(await userServices.getAll()); | ||
}; | ||
|
||
const create = async (req, res) => { | ||
const { name } = req.body; | ||
|
||
if (!name) { | ||
res.sendStatus(400); | ||
} | ||
|
||
const user = await userServices.create(name); | ||
|
||
res.statusCode = 201; | ||
res.send(user); | ||
}; | ||
|
||
const getOne = async (req, res) => { | ||
const { id } = req.params; | ||
const user = await userServices.getById(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also would be great to handle errors on this statge There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. applicable to all service requests |
||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
res.send(user); | ||
}; | ||
|
||
const remove = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await userServices.getById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await userServices.remove(id); | ||
res.sendStatus(204); | ||
}; | ||
|
||
const update = async (req, res) => { | ||
const { id } = req.params; | ||
const { name } = req.body; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is better to validate received data immediately after receiving |
||
|
||
const user = await userServices.getById(id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
if (typeof name !== 'string') { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const updatedUser = await userServices.update({ id, name }); | ||
|
||
res.send(updatedUser); | ||
}; | ||
|
||
module.exports = { | ||
get, | ||
create, | ||
getOne, | ||
remove, | ||
update, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const expensesController = require('../controllers/expense.controller.js'); | ||
|
||
router.get('/', expensesController.get); | ||
|
||
router.post('/', expensesController.create); | ||
|
||
router.get('/:id', expensesController.getOne); | ||
|
||
router.delete('/:id', expensesController.remove); | ||
|
||
router.patch('/:id', expensesController.update); | ||
|
||
module.exports = { router }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const userController = require('../controllers/user.controller.js'); | ||
|
||
router.get('/', userController.get); | ||
|
||
router.post('/', userController.create); | ||
|
||
router.get('/:id', userController.getOne); | ||
|
||
router.delete('/:id', userController.remove); | ||
|
||
router.patch('/:id', userController.update); | ||
|
||
module.exports = { router }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const { | ||
models: { Expense }, | ||
} = require('../models/models'); | ||
const { Op } = require('sequelize'); | ||
|
||
const getAll = async (userId, categories, from, to) => { | ||
const where = {}; | ||
|
||
if (userId) { | ||
where.userId = userId; | ||
} | ||
|
||
if (categories) { | ||
where.category = categories; | ||
} | ||
|
||
if (from || to) { | ||
where.spentAt = { | ||
[Op.between]: [from || new Date(0), to || new Date()], | ||
}; | ||
} | ||
|
||
return Expense.findAll({ where }); | ||
}; | ||
|
||
const getById = async (id) => { | ||
return Expense.findByPk(id); | ||
}; | ||
|
||
const create = async ({ userId, spentAt, title, amount, category, note }) => { | ||
return Expense.create({ | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
}; | ||
|
||
const remove = async (id) => { | ||
await Expense.destroy({ where: { id } }); | ||
}; | ||
|
||
const update = async (id, { spentAt, title, amount, category, note }) => { | ||
const expense = await Expense.findByPk(id); | ||
|
||
if (!expense) { | ||
return null; | ||
} | ||
|
||
if (spentAt) { | ||
expense.spentAt = spentAt; | ||
} | ||
|
||
if (title) { | ||
expense.title = title; | ||
} | ||
|
||
if (amount) { | ||
expense.amount = amount; | ||
} | ||
|
||
if (category) { | ||
expense.category = category; | ||
} | ||
|
||
if (note) { | ||
expense.note = note; | ||
} | ||
|
||
await expense.save(); | ||
|
||
return expense; | ||
}; | ||
|
||
module.exports = { | ||
getAll, | ||
getById, | ||
create, | ||
remove, | ||
update, | ||
}; |
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.
check if
id
was passed