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
10 changes: 6 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
228 changes: 224 additions & 4 deletions src/createServer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,231 @@
'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();

app.use(express.json());

// In-memory storage
const users = [];
const expenses = [];

let userIdCounter = 1;
let expenseIdCounter = 1;

// Users
app.post('/users', (req, res) => {
const { name } = req.body || {};

if (!name) {
return res.status(400).send('Name is required');
}

const user = {
id: userIdCounter++,
name,
};

users.push(user);

return res.status(201).json(user);
});

app.get('/users', (req, res) => {
return res.json(users);
});

function findUser(id) {
return users.find((u) => u.id === Number(id));
}

app.get('/users/:id', (req, res) => {
const user = findUser(req.params.id);

if (!user) {
return res.status(404).send('Not found');
}

return res.json(user);
});

app.put('/users/:id', (req, res) => {
const user = findUser(req.params.id);

if (!user) {
return res.status(404).send('Not found');
}

const { name } = req.body || {};

if (!name) {
return res.status(400).send('Name is required');
}

user.name = name;

return res.json(user);
});

app.patch('/users/:id', (req, res) => {
const user = findUser(req.params.id);

if (!user) {
return res.status(404).send('Not found');
}

const { name } = req.body || {};

if (name !== undefined) {
user.name = name;
}

return res.json(user);
});

app.delete('/users/:id', (req, res) => {
const id = Number(req.params.id);
const idx = users.findIndex((u) => u.id === id);

if (idx === -1) {
return res.status(404).send('Not found');
}

users.splice(idx, 1);

return res.status(204).send();
});

// Expenses
app.post('/expenses', (req, res) => {
const body = req.body || {};
const { userId, spentAt, title, amount, category, note } = body;

if (
userId === undefined ||
spentAt === undefined ||
title === undefined ||
amount === undefined ||
category === undefined ||
note === undefined
) {
return res.status(400).send('Missing required field');
}

const user = findUser(userId);

if (!user) {
return res.status(400).send('User not found');
}

const expense = {
id: expenseIdCounter++,
userId,
spentAt,
title,
amount,
category,
note,
};

expenses.push(expense);

return res.status(201).json(expense);
});

app.get('/expenses', (req, res) => {
let result = expenses.slice();

const { userId, from, to, categories } = req.query || {};

if (userId !== undefined) {
result = result.filter((e) => e.userId === Number(userId));
}

if (from !== undefined) {
const fromDate = new Date(from);

result = result.filter((e) => new Date(e.spentAt) >= fromDate);
}

if (to !== undefined) {
const toDate = new Date(to);

result = result.filter((e) => new Date(e.spentAt) <= toDate);
}

if (categories !== undefined) {
const cats = String(categories).split(',');

result = result.filter((e) => cats.includes(e.category));
}

return res.json(result);
});

app.get('/expenses/:id', (req, res) => {
const id = Number(req.params.id);
const expense = expenses.find((e) => e.id === id);

if (!expense) {
return res.status(404).send('Not found');
}

return res.json(expense);
});

app.patch('/expenses/:id', (req, res) => {
const id = Number(req.params.id);
const expense = expenses.find((e) => e.id === id);

if (!expense) {
return res.status(404).send('Not found');
}

const { userId, spentAt, title, amount, category, note } = req.body || {};

if (userId !== undefined) {
expense.userId = userId;
Comment on lines +177 to +189
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The PATCH /expenses/:id endpoint allows updating userId to a non-existent user without validation. While the task requirements don't explicitly mandate this, the POST /expenses endpoint validates userId exists - consider adding the same validation here for consistency when userId is being updated.

}

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 = Number(req.params.id);
const idx = expenses.findIndex((e) => e.id === id);

if (idx === -1) {
return res.status(404).send('Not found');
}

expenses.splice(idx, 1);

return res.status(204).send();
});

return app;
}

module.exports = {
Expand Down
Loading