-
Notifications
You must be signed in to change notification settings - Fork 664
v1 #510
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
base: master
Are you sure you want to change the base?
v1 #510
Changes from 4 commits
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,44 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
| services: | ||
| postgres: | ||
| image: postgres:latest | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_DB: students | ||
| POSTGRES_PORT: 5432 | ||
| POSTGRES_HOST: localhost | ||
| ports: | ||
| - 5432:5432 | ||
| options: >- | ||
| --health-cmd pg_isready | ||
| --health-interval 10s | ||
| --health-timeout 5s | ||
| --health-retries 5 | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: '20' | ||
| - name: Install dependencies | ||
| run: npm install | ||
| - name: Run tests | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_DB: students | ||
| POSTGRES_HOST: localhost | ||
| POSTGRES_PORT: 5432 | ||
| run: npm test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,3 @@ node_modules | |
|
|
||
| # MacOS | ||
| .DS_Store | ||
|
|
||
| # env files | ||
| *.env | ||
| .env* | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| const categoriesService = require('../services/categories.service'); | ||
|
|
||
| const get = () => async (req, res) => { | ||
| res.json(await categoriesService.getAll()); | ||
| }; | ||
|
|
||
| const getOne = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const reqId = await categoriesService.getById(id); | ||
|
|
||
| if (!reqId) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| res.json(reqId); | ||
| }; | ||
|
|
||
| const create = () => async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const user = await categoriesService.create(name); | ||
|
|
||
| return res.status(201).json(user); | ||
|
Comment on lines
+30
to
+32
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. Good work on the |
||
| }; | ||
|
|
||
| const remove = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const deleted = await categoriesService.remove(id); | ||
|
|
||
| if (!deleted) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| res.status(204).send(); | ||
| }; | ||
|
|
||
| const update = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const { name } = req.body; | ||
|
|
||
| if (typeof name !== 'string' || name.trim() === '') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const userIdToUpdate = await categoriesService.getById(id); | ||
|
|
||
| if (!userIdToUpdate) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| const updatedUser = await categoriesService.update({ id, name }); | ||
|
|
||
| res.status(200).json(updatedUser); | ||
|
Comment on lines
+64
to
+72
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. The variable names
Comment on lines
+64
to
+72
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. The update logic is correct. To make the code even more self-explanatory, I suggest renaming |
||
| }; | ||
|
|
||
| module.exports = { | ||
| get, | ||
| getOne, | ||
| create, | ||
| remove, | ||
| update, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| const { Expense } = require('../models/Expense.model'); | ||
| const { User } = require('../models/User.model'); | ||
|
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. For consistency with other files like |
||
|
|
||
| const expensesService = require('../services/expenses.service'); | ||
|
|
||
| const get = () => async (req, res) => { | ||
|
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. This higher-order function pattern seems to be a remnant from a previous implementation. Since services are now directly imported, you can simplify this and other controller methods to be simple |
||
| res.json(await expensesService.getAll(req.query)); | ||
| }; | ||
|
|
||
| const getOne = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const reqId = await expensesService.getById(id); | ||
|
|
||
| if (!reqId) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| res.json(reqId); | ||
| }; | ||
|
|
||
| const create = () => async (req, res) => { | ||
| const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| const parsedUserId = Number(userId); | ||
| const parsedAmount = Number(amount); | ||
|
|
||
| if (Number.isNaN(parsedUserId)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| if (Number.isNaN(parsedAmount)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| if (typeof title !== 'string' || title.trim() === '') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| if (typeof category !== 'string' || category.trim() === '') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| if (typeof note !== 'string') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const userExists = await User.findByPk(parsedUserId); | ||
|
|
||
| if (!userExists) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
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. Now that you have a |
||
|
|
||
| let spentAtValue; | ||
|
|
||
| if (spentAt !== undefined) { | ||
| const date = new Date(spentAt); | ||
|
|
||
| if (Number.isNaN(date.getTime())) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| spentAtValue = date.toISOString(); | ||
| } else { | ||
| spentAtValue = new Date().toISOString(); | ||
| } | ||
|
|
||
| const expense = await Expense.create({ | ||
|
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. The logic for creating an entity should be in the service layer. This separates the controller's responsibility (handling HTTP requests) from the service's responsibility (business logic and data access). Consider creating an |
||
| userId: parsedUserId, | ||
| spentAt: spentAtValue, | ||
| title, | ||
| amount: parsedAmount, | ||
| category, | ||
| note, | ||
| }); | ||
|
|
||
| return res.status(201).json(expense); | ||
| }; | ||
|
|
||
| const update = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const { userId, title, spentAt, amount, category, note } = req.body; | ||
|
|
||
| const updates = {}; | ||
|
|
||
| if (userId !== undefined) { | ||
| const parsedUserId = Number(userId); | ||
|
|
||
| if (Number.isNaN(parsedUserId)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const userExists = await User.findByPk(parsedUserId); | ||
|
|
||
| if (!userExists) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| updates.userId = parsedUserId; | ||
| } | ||
|
|
||
| if (amount !== undefined) { | ||
| const parsedAmount = Number(amount); | ||
|
|
||
| if (Number.isNaN(parsedAmount)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| updates.amount = amount; | ||
| } | ||
|
|
||
| if (title !== undefined) { | ||
| if (typeof title !== 'string' || title.trim() === '') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| updates.title = title; | ||
| } | ||
|
|
||
| if (category !== undefined) { | ||
| if (typeof category !== 'string' || category.trim() === '') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
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. Similar to the |
||
|
|
||
| updates.category = category; | ||
| } | ||
|
|
||
| if (note !== undefined) { | ||
| if (typeof note !== 'string') { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| updates.note = note; | ||
| } | ||
|
|
||
| if (spentAt !== undefined) { | ||
| const date = new Date(spentAt); | ||
|
|
||
| if (Number.isNaN(date.getTime())) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| updates.spentAt = date.toISOString(); | ||
| } | ||
|
|
||
| const expense = await expensesService.getById(id); | ||
|
|
||
| if (!expense) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| await Expense.update(updates, { where: { 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. Similar to the |
||
|
|
||
| return res.status(200).json(expense); | ||
|
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. The |
||
| }; | ||
|
|
||
| const remove = () => async (req, res) => { | ||
| const id = Number(req.params.id); | ||
|
|
||
| if (Number.isNaN(id)) { | ||
| return res.status(400).send('Bad Request'); | ||
| } | ||
|
|
||
| const deleted = await expensesService.remove(id); | ||
|
|
||
| if (!deleted) { | ||
| return res.status(404).send('Not Found'); | ||
| } | ||
|
|
||
| res.status(204).send(); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| get, | ||
| getOne, | ||
| 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.
The variable
userseems to be a leftover from copying code. Since you're creating a category here, a more descriptive name likenewCategorywould improve readability.