-
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 a442fc0
Showing
11 changed files
with
423 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,117 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
const { ExpenseService } = require('../services/expenseService'); | ||
const expenseService = new ExpenseService(); | ||
const userService = require('../services/userService'); | ||
|
||
async function createExpense(req, res) { | ||
try { | ||
const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
||
if (!spentAt || !title || !amount || !userId) { | ||
return res.status(400).send('All fields are required'); | ||
} | ||
|
||
const user = await userService.getAllUsers({ id: userId }); | ||
|
||
if (user.length === 0) { | ||
return res.status(400).send('Bad Request'); | ||
} | ||
|
||
const newExpense = await expenseService.createExpense( | ||
userId, | ||
spentAt, | ||
title, | ||
amount, | ||
category, | ||
note, | ||
); | ||
|
||
res.status(201).json(newExpense); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error creating expense'); | ||
} | ||
} | ||
|
||
async function getAllExpenses(req, res) { | ||
try { | ||
const userId = req.query.userId; | ||
const category = req.query.categories; | ||
const filters = {}; | ||
|
||
if (userId) { | ||
filters.userId = userId; | ||
} | ||
|
||
if (category) { | ||
filters.category = category; | ||
} | ||
|
||
const expenses = await expenseService.getAllExpenses(filters); | ||
|
||
res.json(expenses); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error getting expenses'); | ||
} | ||
} | ||
|
||
async function getExpense(req, res) { | ||
try { | ||
const [expense] = await expenseService.getAllExpenses({ | ||
id: parseInt(req.params.id), | ||
}); | ||
|
||
if (!expense) { | ||
return res.status(404).send('Expense not found'); | ||
} | ||
|
||
res.json(expense); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error getting expense'); | ||
} | ||
} | ||
|
||
async function updateExpense(req, res) { | ||
try { | ||
const expense = await expenseService.updateExpense( | ||
parseInt(req.params.id), | ||
req.body, | ||
); | ||
|
||
if (!expense) { | ||
return res.status(404).send('Expense not found'); | ||
} | ||
|
||
res.json(expense); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error updating expense'); | ||
} | ||
} | ||
|
||
async function deleteExpense(req, res) { | ||
try { | ||
const success = await expenseService.deleteExpense(parseInt(req.params.id)); | ||
|
||
if (!success) { | ||
return res.status(404).send('Expense not found'); | ||
} | ||
|
||
res.status(204).send(); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).send('Error deleting expense'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
createExpense, | ||
getAllExpenses, | ||
getExpense, | ||
updateExpense, | ||
deleteExpense, | ||
}; |
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,90 @@ | ||
'use strict'; | ||
|
||
const userService = require('../services/userService'); | ||
|
||
async function createUser(req, res) { | ||
const { name } = req.body; | ||
|
||
if (!name) { | ||
return res.status(400).send('Name and are required'); | ||
} | ||
|
||
try { | ||
const newUser = await userService.createUser({ name }); | ||
|
||
res.status(201).json(newUser); | ||
} catch (error) { | ||
res.status(500).send('Error creating user'); | ||
} | ||
} | ||
|
||
async function getAllUsers(req, res) { | ||
try { | ||
const users = await userService.getAllUsers(req.query); | ||
|
||
res.json(users); | ||
} catch (error) { | ||
res.status(500).send('Error fetching users'); | ||
} | ||
} | ||
|
||
async function getUser(req, res) { | ||
try { | ||
const users = await userService.getAllUsers({ | ||
id: parseInt(req.params.id), | ||
}); | ||
const user = users[0]; | ||
|
||
if (!user) { | ||
return res.status(404).send('User not found'); | ||
} | ||
|
||
res.json(user); | ||
} catch (error) { | ||
res.status(500).send('Error fetching user'); | ||
} | ||
} | ||
|
||
async function updateUser(req, res) { | ||
const { name } = req.body; | ||
|
||
if (!name) { | ||
return res.status(400).send('Name and are required'); | ||
} | ||
|
||
try { | ||
const user = await userService.updateUser(parseInt(req.params.id), { | ||
name, | ||
}); | ||
|
||
if (!user) { | ||
return res.status(404).send('User not found'); | ||
} | ||
|
||
res.json(user); | ||
} catch (error) { | ||
res.status(500).send('Error updating user'); | ||
} | ||
} | ||
|
||
async function deleteUser(req, res) { | ||
try { | ||
const success = await userService.deleteUser(parseInt(req.params.id)); | ||
|
||
if (!success) { | ||
return res.status(404).send('User not found'); | ||
} | ||
|
||
res.status(204).send(); | ||
} catch (error) { | ||
res.status(500).send('Error deleting user'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
createUser, | ||
getAllUsers, | ||
getUser, | ||
updateUser, | ||
deleteUser, | ||
}; |
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
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,14 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const expenseController = require('../controllers/expenseController'); | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/', expenseController.createExpense); | ||
router.get('/', expenseController.getAllExpenses); | ||
router.get('/:id', expenseController.getExpense); | ||
router.patch('/:id', expenseController.updateExpense); | ||
router.delete('/:id', expenseController.deleteExpense); | ||
|
||
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,14 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const userController = require('../controllers/userController'); | ||
|
||
const router = express.Router(); | ||
|
||
router.post('/', userController.createUser); | ||
router.get('/', userController.getAllUsers); | ||
router.get('/:id', userController.getUser); | ||
router.patch('/:id', userController.updateUser); | ||
router.delete('/:id', userController.deleteUser); | ||
|
||
module.exports = router; |
Oops, something went wrong.