-
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
Showing
10 changed files
with
359 additions
and
4 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,88 @@ | ||
const expenseService = require('../services/expenceService.js'); | ||
const userService = require('../services/userService.js'); | ||
|
||
const getExpenses = async (req, res) => { | ||
const { userId, categories, from, to } = req.query; | ||
|
||
res.send(await expenseService.getAllExpenses(userId, categories, from, to)); | ||
}; | ||
|
||
const getOneExpense = async (req, res) => { | ||
const { id } = req.params; | ||
const expense = await expenseService.getExpenceById(id); | ||
|
||
if (!expense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
res.status(200); | ||
res.json(expense); | ||
}; | ||
|
||
const createExpance = async (req, res) => { | ||
const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
if (!userId || !spentAt || !title || !amount) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
if (!(await userService.getUserById(userId))) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const expense = await expenseService.createExpence( | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
); | ||
|
||
res.status(201).json(expense); | ||
}; | ||
|
||
const updateExpense = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await expenseService.getExpenceById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
const updatedExpense = await expenseService.patchExpence(id, req.body); | ||
|
||
if (!updatedExpense) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
res.status(200).json(updatedExpense); | ||
}; | ||
|
||
const removeExpense = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await expenseService.getExpenceById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await expenseService.deleteExpence(id); | ||
res.sendStatus(204); | ||
}; | ||
|
||
module.exports = { | ||
getExpenses, | ||
getOneExpense, | ||
createExpance, | ||
updateExpense, | ||
removeExpense, | ||
}; |
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,73 @@ | ||
const userService = require('../services/userService.js'); | ||
|
||
const getUsers = async (req, res) => { | ||
res.send(await userService.getAllUsers()); | ||
}; | ||
|
||
const getUser = async (req, res) => { | ||
const { id } = req.params; | ||
const user = await userService.getUserById(id); | ||
|
||
if (!user) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
res.status(200); | ||
res.json(user); | ||
}; | ||
|
||
const createUser = async (req, res) => { | ||
const { name } = req.body; | ||
|
||
if (!name) { | ||
res.sendStatus(400); | ||
|
||
return; | ||
} | ||
|
||
const user = await userService.createUser(name); | ||
|
||
res.status(201).json(user); | ||
}; | ||
|
||
const updateUser = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await userService.getUserById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
const { name } = req.body; | ||
const updatedUser = await userService.patchUser({ id, name }); | ||
|
||
if (!updatedUser) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
res.status(200).json(updatedUser); | ||
}; | ||
|
||
const removeUser = async (req, res) => { | ||
const { id } = req.params; | ||
|
||
if (!(await userService.getUserById(id))) { | ||
res.sendStatus(404); | ||
|
||
return; | ||
} | ||
|
||
await userService.deleteUser(id); | ||
res.sendStatus(204); | ||
}; | ||
|
||
module.exports = { | ||
getUser, | ||
getUsers, | ||
createUser, | ||
updateUser, | ||
removeUser, | ||
}; |
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,12 @@ | ||
const express = require('express'); | ||
const expenceController = require('../controllers/expenceController.js'); | ||
|
||
const expenseRoute = express.Router(); | ||
|
||
expenseRoute.get('/', expenceController.getExpenses); | ||
expenseRoute.get('/:id', expenceController.getOneExpense); | ||
expenseRoute.post('/', expenceController.createExpance); | ||
expenseRoute.patch('/:id', expenceController.updateExpense); | ||
expenseRoute.delete('/:id', expenceController.removeExpense); | ||
|
||
module.exports = { expenseRoute }; |
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,12 @@ | ||
const express = require('express'); | ||
const userController = require('../controllers/userController.js'); | ||
|
||
const userRoute = express.Router(); | ||
|
||
userRoute.get('/', userController.getUsers); | ||
userRoute.get('/:id', userController.getUser); | ||
userRoute.post('/', userController.createUser); | ||
userRoute.patch('/:id', userController.updateUser); | ||
userRoute.delete('/:id', userController.removeUser); | ||
|
||
module.exports = { userRoute }; |
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,71 @@ | ||
const { Op } = require('sequelize'); | ||
const { models } = require('../models/models.js'); | ||
const { Expense } = models; | ||
|
||
const getAllExpenses = async (userId, categories, from, to) => { | ||
const whereClause = {}; | ||
|
||
if (userId) { | ||
whereClause.userId = userId; | ||
} | ||
|
||
if (categories && categories.length > 0) { | ||
whereClause.category = categories; | ||
} | ||
|
||
if (from) { | ||
whereClause.spentAt = { [Op.gte]: from }; | ||
} | ||
|
||
if (to) { | ||
whereClause.spentAt = { [Op.lte]: to }; | ||
} | ||
|
||
return Expense.findAll({ where: whereClause }); | ||
}; | ||
|
||
const getExpenceById = async (id) => { | ||
return Expense.findByPk(id); | ||
}; | ||
|
||
const createExpence = async ( | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
) => { | ||
return Expense.create({ | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
}); | ||
}; | ||
|
||
const patchExpence = async (id, data) => { | ||
await Expense.update(data, { | ||
where: { id }, | ||
}); | ||
|
||
return Expense.findByPk(id); | ||
}; | ||
|
||
const deleteExpence = async (id) => { | ||
await Expense.destroy({ | ||
where: { | ||
id, | ||
}, | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
getAllExpenses, | ||
getExpenceById, | ||
createExpence, | ||
patchExpence, | ||
deleteExpence, | ||
}; |
Oops, something went wrong.