-
Notifications
You must be signed in to change notification settings - Fork 663
add solution #508
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?
add solution #508
Changes from all 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,23 @@ | ||
| const express = require('express'); | ||
|
|
||
| const { | ||
| getAllCategories, | ||
| getCategoryById, | ||
| deleteCategory, | ||
| createCategory, | ||
| updateCategory, | ||
| } = require('../routers/categories.router.js'); | ||
|
|
||
| const categoriesRoute = express.Router(); | ||
|
|
||
| categoriesRoute.get('/', getAllCategories); | ||
|
|
||
| categoriesRoute.get('/:id', getCategoryById); | ||
|
|
||
| categoriesRoute.post('/', createCategory); | ||
|
|
||
| categoriesRoute.delete('/:id', deleteCategory); | ||
|
|
||
| categoriesRoute.patch('/:id', updateCategory); | ||
|
|
||
| module.exports = { categoriesRoute }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const express = require('express'); | ||
|
|
||
| const { | ||
| getAllExpenses, | ||
| getExpenseById, | ||
| deleteExpense, | ||
| createExpense, | ||
| updateExpense, | ||
| } = require('../routers/expenses.router.js'); | ||
|
|
||
| const expensesRoute = express.Router(); | ||
|
|
||
| expensesRoute.get('/', getAllExpenses); | ||
|
|
||
| expensesRoute.get('/:id', getExpenseById); | ||
|
|
||
| expensesRoute.post('/', createExpense); | ||
|
|
||
| expensesRoute.delete('/:id', deleteExpense); | ||
|
|
||
| expensesRoute.patch('/:id', updateExpense); | ||
|
|
||
| module.exports = { expensesRoute }; | ||
|
Comment on lines
+1
to
+23
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 core task is to implement CRUD functionality for categories. This file handles expenses, but the new functionality for categories seems to be missing entirely from the project. You'll need to add a new set of files ( |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| const express = require('express'); | ||
|
|
||
| const { | ||
| getAllUsers, | ||
| getUsersById, | ||
| deleteUser, | ||
| createUser, | ||
| updateUser, | ||
| } = require('../routers/users.router.js'); | ||
|
|
||
| const usersRoute = express.Router(); | ||
|
|
||
| usersRoute.get('/', getAllUsers); | ||
|
|
||
| usersRoute.get('/:id', getUsersById); | ||
|
|
||
| usersRoute.post('/', createUser); | ||
|
|
||
| usersRoute.delete('/:id', deleteUser); | ||
|
|
||
| usersRoute.patch('/:id', updateUser); | ||
|
|
||
| module.exports = { usersRoute }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,25 @@ | ||
| 'use strict'; | ||
|
|
||
| const createServer = () => { | ||
| // your code goes here | ||
| }; | ||
| const express = require('express'); | ||
| const cors = require('cors'); | ||
|
|
||
| const { expensesRoute } = require('./controllers/expenses.controller.js'); | ||
| const { usersRoute } = require('./controllers/users.controller.js'); | ||
|
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 seems you've missed importing the router for categories. To manage categories, you'll need a dedicated controller and router, similar to what you have for expenses and users. |
||
| const { categoriesRoute } = require('./controllers/categories.controller.js'); | ||
|
|
||
| function createServer() { | ||
| const app = express(); | ||
|
|
||
| app.use(cors()); | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| app.use('/expenses', expensesRoute); | ||
| app.use('/users', usersRoute); | ||
|
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 server is missing the endpoint for categories. You should mount the |
||
| app.use('/categories', categoriesRoute); | ||
|
|
||
| return app; | ||
| } | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 'use strict'; | ||
|
|
||
| const { DataTypes } = require('sequelize'); | ||
| const { sequelize } = require('../db.js'); | ||
|
|
||
| const Category = sequelize.define( | ||
| 'Category', | ||
| { | ||
| id: { | ||
| type: DataTypes.INTEGER, | ||
| primaryKey: true, | ||
| autoIncrement: true, | ||
| }, | ||
| name: { | ||
| type: DataTypes.STRING, | ||
| allowNull: false, | ||
| }, | ||
| }, | ||
| { | ||
| tableName: 'categories', | ||
| timestamps: false, | ||
| }, | ||
| ); | ||
|
|
||
| module.exports = { | ||
| Category, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| const { | ||
| getAll, | ||
| getById, | ||
| create, | ||
| update, | ||
| remove, | ||
| } = require('../services/categories.service.js'); | ||
|
|
||
| const getAllCategories = async (req, res) => { | ||
| const categories = await getAll(); | ||
|
|
||
| res.send(categories); | ||
| }; | ||
|
|
||
| const getCategoryById = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const idNum = Number(id); | ||
|
|
||
| const category = await getById(idNum); | ||
|
|
||
| if (!category) { | ||
| res.status(404).send({ message: 'Not found' }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(category); | ||
| }; | ||
|
|
||
| const createCategory = async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (typeof name !== 'string') { | ||
| res.status(400).send({ message: 'Invalid field' }); | ||
|
|
||
| return; | ||
| } | ||
|
Comment on lines
+34
to
+38
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 order of validation checks here can lead to a less specific error message. If |
||
|
|
||
| if (name === undefined) { | ||
| res.status(400).send({ message: 'Missing required field' }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const category = await create({ name }); | ||
|
|
||
| res.status(201).send(category); | ||
| }; | ||
|
|
||
| const deleteCategory = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const idNum = Number(id); | ||
|
|
||
| const category = await remove(idNum); | ||
|
|
||
| if (!category) { | ||
| res.status(404).send({ message: 'Not found' }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.status(204).send(); | ||
| }; | ||
|
|
||
| const updateCategory = async (req, res) => { | ||
| const { id } = req.params; | ||
| const { name } = req.body; | ||
|
|
||
| const idNum = Number(id); | ||
|
|
||
| if (typeof name !== 'string') { | ||
| res.status(400).send({ message: 'Invalid field' }); | ||
|
|
||
| return; | ||
| } | ||
|
Comment on lines
+73
to
+77
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 |
||
|
|
||
| if (name === undefined) { | ||
| res.status(400).send({ message: 'Missing required field' }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const category = await update({ id: idNum, name }); | ||
|
|
||
| if (!category) { | ||
| res.status(404).send({ message: 'Not found' }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(category); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getAllCategories, | ||
| getCategoryById, | ||
| createCategory, | ||
| deleteCategory, | ||
| updateCategory, | ||
| }; | ||
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.
Great job separating concerns! Just a note on naming conventions: typically, this file, which defines the routes, would be named
categories.router.js. The file containing the request handler logic (likegetAllCategories) would be thecontroller. You currently have this logic in../routers/categories.router.js. Swapping the file names or their contents would align your project better with common Express.js patterns and make it more intuitive for other developers.