-
Notifications
You must be signed in to change notification settings - Fork 663
test #509
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?
test #509
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,120 @@ | ||
| const expensesServices = require('../services/expenses.services'); | ||
| const usersService = require('../services/users.services'); | ||
|
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 |
||
|
|
||
| async function getExpenses(req, res) { | ||
| const { userId, categories, from, to } = req.query; | ||
|
|
||
| const expenses = await expensesServices.getExpenses({ | ||
| userId: +userId, | ||
| categories, | ||
| from, | ||
| to, | ||
| }); | ||
|
|
||
| res.send(expenses.map(expensesServices.normalize)); | ||
| } | ||
|
|
||
| async function getExpenseById(req, res) { | ||
| const { id } = req.params; | ||
|
|
||
| if (!id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const expense = await expensesServices.getExpenseById(+id); | ||
|
|
||
| if (!expense) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.send(expensesServices.normalize(expense)); | ||
| } | ||
|
|
||
| async function createExpense(req, res) { | ||
| const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| if ( | ||
| userId == null || | ||
| !spentAt || | ||
| !title || | ||
| amount == null || | ||
| isNaN(+amount) | ||
| ) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const userExist = await usersService.getUserById(+userId); | ||
|
|
||
| if (!userExist) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const newExpense = await expensesServices.createExpense({ | ||
| userId: +userId, | ||
| spentAt, | ||
| title, | ||
| amount, | ||
| category: category || '', | ||
| note: note || '', | ||
| }); | ||
|
|
||
| res.status(201).send(expensesServices.normalize(newExpense)); | ||
| } | ||
|
|
||
| async function deleteExpense(req, res) { | ||
| const { id } = req.params; | ||
|
|
||
| if (!id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const wasDeleted = await expensesServices.deleteExpense(+id); | ||
|
|
||
| if (!wasDeleted) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.status(204).send(); | ||
| } | ||
|
|
||
| async function updateExpense(req, res) { | ||
| const { id } = req.params; | ||
|
|
||
| if (!id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const expenseExist = await expensesServices.getExpenseById(+id); | ||
|
|
||
| if (!expenseExist) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
|
|
||
| const { spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| if ( | ||
| spentAt === undefined && | ||
| title === undefined && | ||
| amount === undefined && | ||
| category === undefined && | ||
| note === undefined | ||
| ) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const updatedExpense = await expensesServices.updateExpense({ | ||
| id: +id, | ||
| ...req.body, | ||
| }); | ||
|
|
||
| if (!updatedExpense) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.send(expensesServices.normalize(updatedExpense)); | ||
| } | ||
|
|
||
| module.exports = { | ||
| getExpenses, | ||
| getExpenseById, | ||
| createExpense, | ||
| deleteExpense, | ||
| updateExpense, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| const usersService = require('../services/users.services'); | ||
|
|
||
| async function getUsers(req, res) { | ||
| const users = await usersService.getUsers(); | ||
|
|
||
| res.send(users.map(usersService.normalize)); | ||
| } | ||
|
|
||
| async function getUserById(req, res) { | ||
| const { id } = req.params; | ||
|
|
||
| if (!id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const user = await usersService.getUserById(+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. The service method |
||
|
|
||
| if (!user) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.send(usersService.normalize(user)); | ||
| } | ||
|
|
||
| async function createUser(req, res) { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const newUser = await usersService.createUser(name); | ||
|
|
||
| res.status(201).send(usersService.normalize(newUser)); | ||
| } | ||
|
|
||
| async function deleteUser(req, res) { | ||
| const { id } = req.params; | ||
|
|
||
| if (!id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const wasDeleted = await usersService.deleteUser(+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. The |
||
|
|
||
| if (!wasDeleted) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.status(204).send(); | ||
| } | ||
|
|
||
| async function updateUser(req, res) { | ||
| const { id } = req.params; | ||
| const { name } = req.body; | ||
|
|
||
| if (!name || !id || isNaN(+id)) { | ||
| return res.status(400).json({ message: 'Bad Request' }); | ||
| } | ||
|
|
||
| const updatedUser = await usersService.updateUser({ id: +id, name }); | ||
|
|
||
| if (!updatedUser) { | ||
| return res.status(404).json({ message: 'Not found' }); | ||
| } | ||
| res.send(usersService.normalize(updatedUser)); | ||
| } | ||
|
|
||
| module.exports = { | ||
| getUsers, | ||
| getUserById, | ||
| createUser, | ||
| deleteUser, | ||
| updateUser, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,35 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
| const { usersRouter } = require('./routers/users.router'); | ||
| const cors = require('cors'); | ||
| const { expensesRouter } = require('./routers/expenses.router'); | ||
| const { User } = require('./models/User.model'); | ||
| const { Expense } = require('./models/Expense.model'); | ||
|
Comment on lines
+7
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. You're missing the import for the |
||
| const { sequelize } = require('./db.js'); | ||
| const createServer = () => { | ||
| // your code goes here | ||
| const syncDb = async () => { | ||
| try { | ||
| await sequelize.authenticate(); | ||
| await User.sync(); | ||
| await Expense.sync(); | ||
|
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 a
Comment on lines
+14
to
+15
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 |
||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('DB sync failed:', error); | ||
| } | ||
| }; | ||
|
|
||
| syncDb(); | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| app.use('/users', usersRouter); | ||
| app.use('/expenses', 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. To handle category-related requests, you need to create a router for categories and register it here, for example: 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. To make the category management available through the API, you'll need to create a router for categories and register it here using
Comment on lines
+29
to
+30
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 router for the categories feature is not registered. You need to import |
||
|
|
||
| return app; | ||
| }; | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const expensesController = require('../controller/expenses.controller'); | ||
|
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 controller used for these routes relies on a service that has not been migrated to PostgreSQL. All the expense-related operations are still using an in-memory array instead of the database. |
||
|
|
||
| const Router = require('express').Router; | ||
|
|
||
| const expensesRouter = Router(); | ||
|
|
||
| expensesRouter.get('/', expensesController.getExpenses); | ||
| expensesRouter.get('/:id', expensesController.getExpenseById); | ||
| expensesRouter.post('/', expensesController.createExpense); | ||
| expensesRouter.delete('/:id', expensesController.deleteExpense); | ||
| expensesRouter.patch('/:id', expensesController.updateExpense); | ||
|
|
||
| module.exports = { | ||
| expensesRouter, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const usersController = require('../controller/users.controller'); | ||
|
|
||
| const Router = require('express').Router; | ||
|
|
||
| const usersRouter = Router(); | ||
|
|
||
| usersRouter.get('/', usersController.getUsers); | ||
| usersRouter.get('/:id', usersController.getUserById); | ||
| usersRouter.post('/', usersController.createUser); | ||
| usersRouter.delete('/:id', usersController.deleteUser); | ||
| usersRouter.patch('/:id', usersController.updateUser); | ||
|
|
||
| module.exports = { | ||
| 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.
The imported
expensesServicesmodule still relies on an in-memory array for data storage. To meet the task requirement of using PostgreSQL, this service must be refactored to use theExpenseSequelize model for all its database operations.