-
Notifications
You must be signed in to change notification settings - Fork 663
add task solution #507
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 task solution #507
Changes from 1 commit
67d06ff
e2fecea
d2f753c
328786b
34d078c
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,79 @@ | ||
| const { usersService } = require('../services/users.service.js'); | ||
| const { expensesService } = require('../services/expenses.service.js'); | ||
|
|
||
| const create = async (req, res) => { | ||
| const { userId, spentAt, title, amount, category, note } = req.body; | ||
| const user = await usersService.getOne(userId); | ||
|
|
||
| if (!userId || !user) { | ||
|
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.
|
||
| return res.sendStatus(400); | ||
|
Comment on lines
+4
to
+8
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.
|
||
| } | ||
|
|
||
| const expense = await expensesService.create({ | ||
|
Comment on lines
+9
to
+11
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.
|
||
| userId: userId, | ||
| spentAt, | ||
| title, | ||
| amount, | ||
| category, | ||
| note, | ||
| }); | ||
|
|
||
| res.status(201).json(expensesService.normalize(expense)); | ||
| }; | ||
|
|
||
| const getAll = async (req, res) => { | ||
| const { userId, categories, from, to } = req.query; | ||
| const expenses = await expensesService.getAll({ | ||
| userId: Number(userId), | ||
| categories, | ||
| from, | ||
| to, | ||
| }); | ||
|
|
||
| res.json(expenses.map((expense) => expensesService.normalize(expense))); | ||
| }; | ||
|
|
||
| const getOne = async (req, res) => { | ||
| const expense = await expensesService.getOne(Number(req.params.id)); | ||
|
|
||
| if (!expense) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| res.json(expensesService.normalize(expense)); | ||
| }; | ||
|
|
||
| const update = async (req, res) => { | ||
| const expense = await expensesService.getOne(Number(req.params.id)); | ||
|
|
||
| if (!expense) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| const updatedExpense = await expensesService.update({ | ||
| id: Number(req.params.id), | ||
| ...req.body, | ||
| }); | ||
|
|
||
| res.json(expensesService.normalize(updatedExpense)); | ||
| }; | ||
|
|
||
| const remove = async (req, res) => { | ||
| const expensesRemoved = await expensesService.remove(Number(req.params.id)); | ||
|
|
||
| if (!expensesRemoved) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| res.sendStatus(204); | ||
| }; | ||
|
|
||
| const expensesController = { | ||
| create, | ||
| getAll, | ||
| getOne, | ||
| update, | ||
| remove, | ||
| }; | ||
|
|
||
| exports.expensesController = expensesController; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const { usersController } = require('./users.controller'); | ||
| const { expensesController } = require('./expenses.controller'); | ||
|
|
||
| module.exports = { | ||
| usersController, | ||
|
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 |
||
| expensesController, | ||
|
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 |
||
| }; | ||
|
Comment on lines
+5
to
+9
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 task requires implementing CRUD operations for categories. This would involve creating a 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 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| const { usersService } = require('../services/users.service.js'); | ||
|
|
||
| const create = async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.sendStatus(400); | ||
| } | ||
|
|
||
| const user = await usersService.create(name); | ||
|
|
||
| res.status(201).json(usersService.normalize(user)); | ||
| }; | ||
|
|
||
| const getAll = async (req, res) => { | ||
| const users = await usersService.getAll(); | ||
|
|
||
| res.send(users.map((user) => usersService.normalize(user))); | ||
| }; | ||
|
|
||
| const getOne = async (req, res) => { | ||
| const user = await usersService.getOne(Number(req.params.id)); | ||
|
|
||
| if (!user) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(usersService.normalize(user)); | ||
| }; | ||
|
|
||
| const update = async (req, res) => { | ||
| const { id } = req.params; | ||
| const user = await usersService.getOne(Number(id)); | ||
|
|
||
| if (!user) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| const updatedUser = await usersService.update({ | ||
| id: Number(id), | ||
| name: req.body.name, | ||
| }); | ||
|
|
||
| res.json(usersService.normalize(updatedUser)); | ||
| }; | ||
|
|
||
| const remove = async (req, res) => { | ||
| const user = await usersService.remove(Number(req.params.id)); | ||
|
|
||
| if (!user) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| res.sendStatus(204); | ||
| }; | ||
|
|
||
| const usersController = { | ||
| getAll, | ||
| getOne, | ||
| create, | ||
| update, | ||
| remove, | ||
| }; | ||
|
|
||
| exports.usersController = usersController; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const { routes } = require('./routes/index'); | ||
|
|
||
| const createServer = () => { | ||
| // your code goes here | ||
| const app = express(); | ||
|
|
||
| app.use(express.json()); | ||
| app.use('/users', routes.usersRouter); | ||
| app.use('/expenses', routes.expensesRouter); | ||
|
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 new routes for managing categories are missing. Based on the task requirements, you should add a new router here for the
Comment on lines
+10
to
+11
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 categories router is NOT registered in the server. The task requires: 'Ensure the new routes are registered in |
||
|
|
||
| return app; | ||
| }; | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,32 @@ | ||
| 'use strict'; | ||
|
|
||
| const { sequelize } = require('../db.js'); | ||
| const { DataTypes } = require('sequelize'); | ||
|
|
||
| const Expense = sequelize.define( | ||
| // your code goes here | ||
| ); | ||
| const Expense = sequelize.define('Expense', { | ||
| userId: { | ||
| type: DataTypes.INTEGER, | ||
| allowNull: false, | ||
| }, | ||
| title: { | ||
|
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 categories router is not registered in the server. The task explicitly requires: 'Ensure the new routes are registered in src/createServer.js'. Add: app.use('/categories', routes.categoriesRouter); |
||
| type: DataTypes.STRING, | ||
| allowNull: false, | ||
| }, | ||
| amount: { | ||
| type: DataTypes.INTEGER, | ||
| allowNull: false, | ||
| }, | ||
| spentAt: { | ||
| type: DataTypes.DATE, | ||
| allowNull: false, | ||
| }, | ||
| category: { | ||
| type: DataTypes.STRING, | ||
| }, | ||
| note: { | ||
| type: DataTypes.STRING, | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = { | ||
| Expense, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| 'use strict'; | ||
|
|
||
| const { sequelize } = require('../db.js'); | ||
| const { DataTypes } = require('sequelize'); | ||
|
|
||
| const User = sequelize.define( | ||
| // your code goes here | ||
| ); | ||
| const User = sequelize.define('User', { | ||
|
Comment on lines
1
to
+6
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 categoriesRouter is NOT exported from src/routes/index.js. Currently only usersRouter and expensesRouter are exported. Need to add: |
||
| name: { | ||
| type: DataTypes.STRING, | ||
| allowNull: false, | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = { | ||
| User, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const { Router } = require('express'); | ||
| const { | ||
| expensesController, | ||
| } = require('./../controllers/expenses.controller.js'); | ||
|
|
||
| const expensesRouter = Router(); | ||
|
|
||
| expensesRouter.post('/', expensesController.create); | ||
|
Comment on lines
+1
to
+6
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 categories router is not imported and exported. Need to add: |
||
| expensesRouter.get('/', expensesController.getAll); | ||
| expensesRouter.get('/:id', expensesController.getOne); | ||
| expensesRouter.delete('/:id', expensesController.remove); | ||
| expensesRouter.patch('/:id', expensesController.update); | ||
|
|
||
| exports.expensesRouter = expensesRouter; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const { usersRouter } = require('./users.route'); | ||
| const { expensesRouter } = require('./expenses.route'); | ||
|
|
||
| exports.routes = { | ||
| usersRouter, | ||
| expensesRouter, | ||
|
Comment on lines
+1
to
+7
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 categories router is not exported from src/routes/index.js. Currently only usersRouter and expensesRouter are exported. Need to add: 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. Need to export categoriesRouter in the routes object to make it available for registration in createServer.js. |
||
| }; | ||
|
Comment on lines
+5
to
+9
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. According to the task requirements, you need to implement CRUD for categories. This file should import the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const { Router } = require('express'); | ||
| const { usersController } = require('./../controllers/users.controller'); | ||
|
|
||
| const usersRouter = Router(); | ||
|
|
||
| usersRouter.post('/', usersController.create); | ||
|
Comment on lines
+1
to
+6
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 categoriesService is NOT exported from src/services/index.js. Currently only usersService and expensesService are exported. Need to add: |
||
| usersRouter.get('/', usersController.getAll); | ||
| usersRouter.get('/:id', usersController.getOne); | ||
| usersRouter.patch('/:id', usersController.update); | ||
| usersRouter.delete('/:id', usersController.remove); | ||
|
|
||
| exports.usersRouter = usersRouter; | ||
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.
src/services/index.jsis missing export for categoriesService