Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
190 changes: 189 additions & 1 deletion src/createServer.js
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);
});
Comment on lines +10 to +76
Copy link
Copy Markdown

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.


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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint validates userId but is missing validation for other required fields (spentAt, title, amount, category). According to the Swagger API documentation, these are required parameters. Consider adding validation: if (!spentAt) { return res.status(400).json({ error: 'spentAt is required' }); } and similar checks for title, amount, and category.

});

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 = {
Expand Down
Loading