diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/package-lock.json b/package-lock.json index c86872bf..00b1884e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,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", @@ -1472,10 +1472,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -5420,6 +5421,7 @@ "resolved": "https://registry.npmjs.org/jest/-/jest-29.7.0.tgz", "integrity": "sha512-NIy3oAFp9shda19hy4HK0HRTWKtPJmGdnvywu01nOqNC2vZg+Z+fvJDxpMQA88eb2I9EcafcdjYgsDthnYTvGw==", "dev": true, + "license": "MIT", "dependencies": { "@jest/core": "^29.7.0", "@jest/types": "^29.6.3", diff --git a/package.json b/package.json index 9b8e432d..b5470cee 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/createServer.js b/src/createServer.js index 5b405372..be6f9dba 100644 --- a/src/createServer.js +++ b/src/createServer.js @@ -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; + } + + 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 = {