-
Notifications
You must be signed in to change notification settings - Fork 973
Develop #944
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?
Develop #944
Changes from all commits
76842c7
49d3a21
0457723
af604f5
1b28711
a4db3fc
b606d92
181bb29
aa8324c
2ab0728
ff0333a
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 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
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 |
|---|---|---|
| @@ -1,11 +1,199 @@ | ||
| 'use strict'; | ||
|
|
||
| // const express = require('express'); | ||
| const express = require('express'); | ||
|
|
||
| function createServer() { | ||
| // Use express to create a server | ||
| // Add a routes to the server | ||
| // Return the server (express app) | ||
| const app = express(); | ||
| const users = []; | ||
| const expenses = []; | ||
| let nextUserId = 1; | ||
| let nextExpenseId = 1; | ||
|
|
||
| app.use(express.json()); | ||
|
|
||
| app.post('/users', (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (!name) { | ||
| return res.status(400).json({ error: 'Name is required' }); | ||
| } | ||
|
|
||
| const newUser = { | ||
| id: nextUserId++, | ||
| name, | ||
| }; | ||
|
|
||
| users.push(newUser); | ||
|
|
||
| return res.status(201).json(newUser); | ||
| }); | ||
|
|
||
| app.get('/users', (req, res) => { | ||
| return res.json(users); | ||
| }); | ||
|
|
||
| app.get('/users/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const user = users.find((u) => u.id === Number(id)); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).json({ error: 'User not found' }); | ||
| } | ||
|
|
||
| return res.json(user); | ||
| }); | ||
|
|
||
| app.patch('/users/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const { name } = req.body; | ||
| const user = users.find((u) => u.id === Number(id)); | ||
|
|
||
| if (!user) { | ||
| return res.status(404).json({ error: 'User not found' }); | ||
| } | ||
|
|
||
| if (name) { | ||
| user.name = name; | ||
| } | ||
|
|
||
| return res.json(user); | ||
| }); | ||
|
|
||
| app.delete('/users/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const index = users.findIndex((u) => u.id === Number(id)); | ||
|
|
||
| if (index === -1) { | ||
| return res.status(404).json({ error: 'User not found' }); | ||
| } | ||
|
|
||
| users.splice(index, 1); | ||
|
|
||
| return res.sendStatus(204); | ||
| }); | ||
|
|
||
| app.post('/expenses', (req, res) => { | ||
| const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| if (!spentAt || !title || !amount || !category) { | ||
| return res.status(400).json({ error: 'Required fields are missing' }); | ||
| } | ||
|
|
||
| const user = users.find((u) => u.id === userId); | ||
|
|
||
| if (!user) { | ||
| return res.status(400).json({ error: 'User not found' }); | ||
| } | ||
|
|
||
| const expense = { | ||
| id: nextExpenseId++, | ||
| userId, | ||
| spentAt, | ||
| title, | ||
| amount, | ||
| category, | ||
| note, | ||
| }; | ||
|
|
||
| expenses.push(expense); | ||
|
|
||
| return res.status(201).json(expense); | ||
|
Comment on lines
+78
to
+103
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 endpoint validates |
||
| }); | ||
|
|
||
| app.get('/expenses', (req, res) => { | ||
| const { userId, from, to, categories } = req.query; | ||
|
|
||
| const fromDate = from ? new Date(from) : null; | ||
| const toDate = to ? new Date(to) : null; | ||
| const categorySet = categories ? new Set(categories.split(',')) : null; | ||
|
|
||
| const filteredExpenses = expenses.filter((expense) => { | ||
| if (userId && expense.userId !== Number(userId)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (fromDate && new Date(expense.spentAt) < fromDate) { | ||
| return false; | ||
| } | ||
|
|
||
| if (toDate && new Date(expense.spentAt) > toDate) { | ||
| return false; | ||
| } | ||
|
|
||
| if (categorySet && !categorySet.has(expense.category)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| }); | ||
|
|
||
| return res.json(filteredExpenses); | ||
| }); | ||
|
|
||
| app.get('/expenses/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const expenseId = Number(id); | ||
| const expense = expenses.find((e) => e.id === expenseId); | ||
|
|
||
| if (!expense) { | ||
| return res.status(404).json({ error: 'Expense not found' }); | ||
| } | ||
|
|
||
| return res.json(expense); | ||
| }); | ||
|
|
||
| app.patch('/expenses/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const expenseId = Number(id); | ||
| const expense = expenses.find((e) => e.id === expenseId); | ||
|
|
||
| if (!expense) { | ||
| return res.status(404).json({ error: 'Expense not found' }); | ||
| } | ||
|
|
||
| const { spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| if (spentAt !== undefined) { | ||
| expense.spentAt = spentAt; | ||
| } | ||
|
|
||
| if (title !== undefined) { | ||
| expense.title = title; | ||
| } | ||
|
|
||
| if (amount !== undefined) { | ||
| expense.amount = amount; | ||
| } | ||
|
|
||
| if (category !== undefined) { | ||
| expense.category = category; | ||
| } | ||
|
|
||
| if (note !== undefined) { | ||
| expense.note = note; | ||
| } | ||
|
|
||
| return res.json(expense); | ||
| }); | ||
|
|
||
| app.delete('/expenses/:id', (req, res) => { | ||
| const { id } = req.params; | ||
| const expenseId = Number(id); | ||
| const index = expenses.findIndex((e) => e.id === expenseId); | ||
|
|
||
| if (index === -1) { | ||
| return res.status(404).json({ error: 'Expense not found' }); | ||
| } | ||
|
|
||
| expenses.splice(index, 1); | ||
|
|
||
| return res.sendStatus(204); | ||
| }); | ||
|
|
||
| return app; | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
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.
Only the 'users' collection is implemented. The task requires 2 collections with 5 endpoints each for an expense tracking app. Based on the requirements, you need to add a second collection (likely 'expenses' or 'transactions') with its own 5 endpoints: POST, GET all, GET by ID, PATCH by ID, and DELETE by ID.