-
Notifications
You must be signed in to change notification settings - Fork 663
solution #514
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?
solution #514
Changes from all commits
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,5 @@ | ||
| POSTGRES_DB = 'postgres' | ||
| POSTGRES_USER = 'postgres' | ||
| POSTGRES_HOST = 'localhost' | ||
| POSTGRES_PORT = '5432' | ||
| POSTGRES_PASSWORD = '123123' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
| services: | ||
| postgres: | ||
| image: postgres:latest | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_DB: students | ||
| POSTGRES_PORT: 5432 | ||
| POSTGRES_HOST: localhost | ||
| ports: | ||
| - 5432:5432 | ||
| options: >- | ||
| --health-cmd pg_isready | ||
| --health-interval 10s | ||
| --health-timeout 5s | ||
| --health-retries 5 | ||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: '20' | ||
| - name: Install dependencies | ||
| run: npm install | ||
| - name: Run tests | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: password | ||
| POSTGRES_DB: students | ||
| POSTGRES_HOST: localhost | ||
| POSTGRES_PORT: 5432 | ||
| run: npm test |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,3 @@ node_modules | |
|
|
||
| # MacOS | ||
| .DS_Store | ||
|
|
||
| # env files | ||
| *.env | ||
| .env* | ||
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 |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /* eslint-disable curly */ | ||
| const expenses = require('../entitys/expenses.reposytory.js'); | ||
| const users = require('../entitys/users.reposytory.js'); | ||
|
|
||
| const isDate = (str) => { | ||
| const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/; | ||
|
|
||
| // eslint-disable-next-line curly | ||
| if (!isoRegex.test(str)) return false; | ||
|
|
||
| const date = new Date(str); | ||
|
|
||
| return !isNaN(date.getTime()); | ||
| }; | ||
|
|
||
| const checkDataForUpdate = (o) => { | ||
| return Object.keys(o).every((k) => { | ||
| switch (k) { | ||
| case 'spentAt': | ||
| return isDate(o[k]); | ||
| case 'title': | ||
| case 'category': | ||
| case 'note': | ||
| return typeof o[k] === 'string'; | ||
| case 'amount': | ||
| return typeof o[k] === 'number'; | ||
| default: | ||
| return false; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const getAll = async (req, res) => { | ||
| const filterOptions = { userId: 'userId', categories: 'category' }; | ||
| const filterParams = {}; | ||
|
|
||
| for (const key in req.query) { | ||
| if (filterOptions[key]) filterParams[filterOptions[key]] = req.query[key]; | ||
| } | ||
|
|
||
| res.send(await expenses.getAll(filterParams)); | ||
| }; | ||
|
|
||
| const create = async (req, res) => { | ||
| const { userId, spentAt, title, amount, category, note } = req.body; | ||
|
|
||
| const intUserId = Number(userId); | ||
|
|
||
| const userExist = !Number.isNaN(intUserId) | ||
| ? await users.getById(intUserId) | ||
| : null; | ||
|
|
||
| if ( | ||
| !userExist || | ||
| !isDate(spentAt) || | ||
| typeof title !== 'string' || | ||
| typeof amount !== 'number' | ||
| ) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const newExpense = { | ||
| userId: intUserId, | ||
| spentAt, | ||
| title, | ||
| amount, | ||
| }; | ||
|
|
||
| if (typeof note === 'string') newExpense.note = note; | ||
| if (typeof category === 'string') newExpense.category = category; | ||
|
|
||
| res.statusCode = 201; | ||
| res.send(await expenses.create(newExpense)); | ||
| }; | ||
|
|
||
| const getById = async (req, res) => { | ||
| const { id } = req.params; | ||
| const intId = Number(id); | ||
|
|
||
| if (Number.isNaN(intId)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const expense = await expenses.getById(intId); | ||
|
|
||
| if (!expense) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(expense); | ||
| }; | ||
|
|
||
| const remove = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const intId = Number(id); | ||
|
|
||
| if (Number.isNaN(intId)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const result = await expenses.remove(intId); | ||
|
|
||
| if (result <= 0) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(204); | ||
|
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. Using |
||
| }; | ||
|
|
||
| const update = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const intId = Number(id); | ||
|
|
||
| if (Number.isNaN(intId) || !checkDataForUpdate(req.body)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const [, expense] = await expenses.update(intId, req.body); | ||
|
|
||
| if (!expense[0]) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(expense[0]); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getAll, | ||
| create, | ||
| getById, | ||
| remove, | ||
| update, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| const users = require('../entitys/users.reposytory'); | ||
|
|
||
| // const isUUID = (d) => { | ||
| // // eslint-disable-next-line max-len, prettier/prettier | ||
| // const pattern = | ||
| // /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; | ||
|
|
||
| // return pattern.test(d); | ||
| // }; | ||
|
|
||
| const getAll = async (req, res) => { | ||
| res.send(await users.getAll()); | ||
| }; | ||
|
|
||
| const create = async (req, res) => { | ||
| const { name } = req.body; | ||
|
|
||
| if (typeof name !== 'string') { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 201; | ||
| res.send(await users.create({ name })); | ||
| }; | ||
|
|
||
| const getById = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const intId = Number(id); | ||
|
|
||
| if (Number.isNaN(intId)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const user = await users.getById(intId); | ||
|
|
||
| if (!user) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(user); | ||
| }; | ||
|
|
||
| const removeById = async (req, res) => { | ||
| const { id } = req.params; | ||
|
|
||
| const intId = Number(id); | ||
|
|
||
| if (Number.isNaN(intId)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const result = await users.remove(intId); | ||
|
|
||
| if (result <= 0) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.sendStatus(204); | ||
| }; | ||
|
|
||
| const update = async (req, res) => { | ||
| const { name } = req.body; | ||
| const { id } = req.params; | ||
|
|
||
| const intId = Number(id); | ||
|
|
||
| if (typeof name !== 'string' || Number.isNaN(intId)) { | ||
| res.sendStatus(400); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const [, updatedUsers] = await users.update(intId, { name }); | ||
|
|
||
| if (!updatedUsers[0]) { | ||
| res.sendStatus(404); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.send(updatedUsers[0]); | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getAll, | ||
| create, | ||
| getById, | ||
| removeById, | ||
| update, | ||
| }; |
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 a full CRUD for categories. This implies 'category' should be its own resource (with a model, DB table, etc.), not just a string property of an expense. You would then reference it using an ID (e.g.,
categoryId) and validate that ID here.