-
Notifications
You must be signed in to change notification settings - Fork 663
solution #502
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
Open
nastia7408
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
nastia7408:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
solution #502
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,9 +1,286 @@ | ||
| 'use strict'; | ||
|
|
||
| const express = require('express'); | ||
|
|
||
| const { User, Expense, Category } = require('./models/models'); | ||
|
|
||
| const createServer = () => { | ||
| // your code goes here | ||
| }; | ||
| const app = express(); | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| app.post('/users', async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(400).send('Name is required'); | ||
| } | ||
|
|
||
| try { | ||
| const newUser = await User.create({ name }); | ||
|
|
||
| res.status(201).json(newUser); | ||
| } catch (e) { | ||
| res.status(400).send(e.message); | ||
| } | ||
| }); | ||
|
|
||
| app.get('/users', async (req, res) => { | ||
| res.json(await User.findAll()); | ||
| }); | ||
|
|
||
| app.get('/users/:id', async (req, res) => { | ||
| const user = await User.findByPk(req.params.id); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).send('User not found'); | ||
| } | ||
| res.json(user); | ||
| }); | ||
|
|
||
| app.patch('/users/:id', async (req, res) => { | ||
| const user = await User.findByPk(req.params.id); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).send('Not found'); | ||
| } | ||
| await user.update(req.body, { silent: true }); | ||
| res.json(user); | ||
| }); | ||
|
|
||
| app.delete('/users/:id', async (req, res) => { | ||
| const user = await User.findByPk(req.params.id); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).send('Not found'); | ||
| } | ||
| await user.destroy(); | ||
| res.sendStatus(204); | ||
| }); | ||
|
|
||
| app.post('/expenses', async (req, res) => { | ||
| const { title, amount, userId, category, note, spentAt } = req.body; | ||
|
|
||
| if (!title || !amount || !userId) { | ||
| return res.sendStatus(400); | ||
| } | ||
|
|
||
| try { | ||
| const [foundCategory] = await Category.findOrCreate({ | ||
| where: { name: category || 'Other' }, | ||
| }); | ||
|
|
||
| const expense = await Expense.create({ | ||
| title, | ||
| amount, | ||
| userId, | ||
| categoryId: foundCategory.id, | ||
| note, | ||
|
|
||
| spentAt: spentAt || new Date(), | ||
| }); | ||
|
|
||
| const result = expense.toJSON(); | ||
|
|
||
| delete result.categoryId; | ||
|
|
||
| res.status(201).json({ | ||
| ...result, | ||
| category: foundCategory.name, | ||
| }); | ||
| } catch (error) { | ||
| res.sendStatus(400); | ||
| } | ||
| }); | ||
|
|
||
| app.get('/expenses', async (req, res) => { | ||
| const { userId, categories, from, to } = req.query; | ||
| const { Op } = require('sequelize'); | ||
|
|
||
| const where = {}; | ||
|
|
||
| if (userId) { | ||
| where.userId = userId; | ||
| } | ||
|
|
||
| if (from || to) { | ||
| where.spentAt = {}; | ||
|
|
||
| if (from) { | ||
| where.spentAt[Op.gte] = from; | ||
| } | ||
|
|
||
| if (to) { | ||
| where.spentAt[Op.lte] = to; | ||
| } | ||
| } | ||
|
|
||
| const include = [ | ||
| { | ||
| model: Category, | ||
| as: 'Category', | ||
|
|
||
| ...(categories && { | ||
| where: { name: categories }, | ||
| required: true, | ||
| }), | ||
| }, | ||
| ]; | ||
|
|
||
| try { | ||
| const expenses = await Expense.findAll({ | ||
| where, | ||
| include, | ||
| }); | ||
|
|
||
| module.exports = { | ||
| createServer, | ||
| const result = expenses.map((exp) => { | ||
| const data = exp.toJSON(); | ||
|
|
||
| return { | ||
| id: data.id, | ||
| title: data.title, | ||
| amount: data.amount, | ||
| spentAt: data.spentAt, | ||
| note: data.note, | ||
| userId: data.userId, | ||
| category: data.Category ? data.Category.name : null, | ||
| }; | ||
| }); | ||
|
|
||
| res.json(result); | ||
| } catch (error) { | ||
| res.sendStatus(500); | ||
| } | ||
| }); | ||
|
|
||
| app.delete('/expenses/:id', async (req, res) => { | ||
| const deletedCount = await Expense.destroy({ | ||
| where: { id: req.params.id }, | ||
| }); | ||
|
|
||
| if (deletedCount === 0) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| res.sendStatus(204); | ||
| }); | ||
|
|
||
| app.get('/expenses/:id', async (req, res) => { | ||
| const expense = await Expense.findByPk(req.params.id, { | ||
| include: [{ model: Category, as: 'Category' }], | ||
| }); | ||
|
|
||
| if (!expense) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| const data = expense.toJSON(); | ||
| const response = { | ||
| ...data, | ||
| category: data.Category ? data.Category.name : null, | ||
| }; | ||
|
|
||
| delete response.Category; | ||
| delete response.categoryId; | ||
|
|
||
| res.json(response); | ||
| }); | ||
|
|
||
| app.patch('/expenses/:id', async (req, res) => { | ||
| const expense = await Expense.findByPk(req.params.id); | ||
|
|
||
| if (!expense) { | ||
| return res.sendStatus(404); | ||
| } | ||
|
|
||
| const { category, ...otherData } = req.body; | ||
| const updateData = { ...otherData }; | ||
|
|
||
| if (category) { | ||
| const [foundCategory] = await Category.findOrCreate({ | ||
| where: { name: category }, | ||
| }); | ||
|
|
||
| updateData.categoryId = foundCategory.id; | ||
| } | ||
|
|
||
| await expense.update(updateData); | ||
|
|
||
| const updatedExpense = await Expense.findByPk(req.params.id, { | ||
| include: [{ model: Category, as: 'Category' }], | ||
| }); | ||
|
|
||
| const data = updatedExpense.toJSON(); | ||
| const response = { | ||
| ...data, | ||
| category: data.Category ? data.Category.name : null, | ||
| }; | ||
|
|
||
| delete response.Category; | ||
| delete response.categoryId; | ||
|
|
||
| res.json(response); | ||
| }); | ||
|
|
||
| app.get('/categories', async (req, res) => { | ||
| try { | ||
| const categories = await Category.findAll({ order: [['id', 'ASC']] }); | ||
|
|
||
| res.json(categories); | ||
| } catch (e) { | ||
| res.status(500).json({ error: e.message }); | ||
| } | ||
| }); | ||
|
|
||
| app.post('/categories', async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(400).send('Name is required'); | ||
| } | ||
|
|
||
| try { | ||
| const [category, created] = await Category.findOrCreate({ | ||
| where: { name }, | ||
| }); | ||
|
|
||
| res.status(created ? 201 : 200).json(category); | ||
| } catch (e) { | ||
| res.status(400).json({ error: e.message }); | ||
| } | ||
| }); | ||
|
|
||
| app.patch('/categories/:id', async (req, res) => { | ||
| try { | ||
| const category = await Category.findByPk(req.params.id); | ||
|
|
||
| if (!category) { | ||
| return res.sendStatus(404); | ||
| } | ||
| await category.update(req.body); | ||
| res.json(category); | ||
| } catch (e) { | ||
| res.status(400).json({ error: e.message }); | ||
| } | ||
| }); | ||
|
|
||
| app.delete('/categories/:id', async (req, res) => { | ||
| try { | ||
| const category = await Category.findByPk(req.params.id); | ||
|
|
||
| if (!category) { | ||
| return res.sendStatus(404); | ||
| } | ||
| await category.destroy(); | ||
| res.sendStatus(204); | ||
| } catch (e) { | ||
| res | ||
| .status(400) | ||
| .send('Cannot delete category: it is assigned to expenses.'); | ||
| } | ||
| }); | ||
|
|
||
| return app; | ||
| }; | ||
|
|
||
| module.exports = { createServer }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| const { DataTypes } = require('sequelize'); | ||
| const { sequelize } = require('../db'); | ||
|
|
||
| const Category = sequelize.define('Category', { | ||
| name: { | ||
| type: DataTypes.STRING, | ||
| allowNull: false, | ||
| }, | ||
| }); | ||
|
|
||
| module.exports = { Category }; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 task requires implementing CRUD functionality for categories. This would involve creating a
Categorymodel, importing it here, and then adding the corresponding API endpoints (e.g., GET, POST, PATCH, DELETE for/categories) within this file. This implementation is currently missing.