-
Notifications
You must be signed in to change notification settings - Fork 0
01 hw already to check #2
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
Open
bogdanAndrushchenko
wants to merge
1
commit into
main
Choose a base branch
from
01_node_js_basic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| # rest_api_node_js | ||
| # rest_api_node_js | ||
|
|
||
| [Table list](https://ibb.co/xg37Ndc) | ||
|
|
||
| [Get contact by id](https://ibb.co/s9Pqv3H) | ||
|
|
||
| [Add new contact](https://ibb.co/JyF8mr4) | ||
|
|
||
| [Remove contact by id](https://ibb.co/KV3KznV) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| const fs = require("fs").promises; | ||
| const path = require("path"); | ||
|
|
||
| const contactsPath = path.join(__dirname, "model", "test.json"); | ||
|
|
||
| console.log(contactsPath, "<= contactsPath"); | ||
|
|
||
| const listContacts = async () => { | ||
| try { | ||
| const res = await fs.readFile(contactsPath); | ||
| console.table(JSON.parse(res)); | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| }; | ||
|
|
||
| const getContactById = async (contactId) => { | ||
| try { | ||
| const res = await fs.readFile(contactsPath); | ||
| const findContact = JSON.parse(res).find( | ||
| (contact) => contact.id === contactId | ||
| ); | ||
| if (findContact === undefined) { | ||
| console.log(`Your id:${contactId} not found!`); | ||
| } | ||
| console.log(findContact); | ||
|
Collaborator
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. тоже что и в listContacts. Возвращай значения з функции. Выводи в консоль в индексе |
||
| // return findContact | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| }; | ||
|
|
||
| const removeContact = async (contactId) => { | ||
| try { | ||
| const res = await fs.readFile(contactsPath); | ||
| const dataArr = JSON.parse(res); | ||
|
|
||
| const contacts = dataArr.filter((contact) => contact.id !== contactId); | ||
|
|
||
| if (contacts.length !== dataArr.length) { | ||
| fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2)); | ||
|
|
||
| console.log(`Contact with id: ${contactId} removed!`); | ||
| } else { | ||
| console.log(`Id:${contactId} not found!`); | ||
| return; | ||
| } | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| }; | ||
|
|
||
| const addContact = async (name, email, phone) => { | ||
| try { | ||
| const res = await fs.readFile(contactsPath); | ||
| const contacts = JSON.parse(res); | ||
| const newContact = {id: contacts.length + 1, name, email, phone}; | ||
| contacts.push(newContact); | ||
|
|
||
| fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2)); | ||
|
|
||
| console.log("Your contact added!"); | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| listContacts, | ||
| getContactById, | ||
| removeContact, | ||
| addContact, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const yargs = require('yargs'); | ||
| const { | ||
| listContacts, | ||
| getContactById, | ||
| removeContact, | ||
| addContact, | ||
| } = require("./contacts"); | ||
|
|
||
| const argv = yargs | ||
| .number("id") | ||
| .string("name") | ||
| .string("email") | ||
| .string("phone").argv; | ||
|
|
||
|
|
||
| function invokeAction({action, id, name, email, phone}) { | ||
| switch (action) { | ||
| case 'list': | ||
| listContacts(); | ||
| break; | ||
|
|
||
| case 'get': | ||
| getContactById(id); | ||
| break; | ||
|
|
||
| case 'add': | ||
| addContact(name, email, phone); | ||
| break; | ||
|
|
||
| case 'remove': | ||
| removeContact(id) | ||
| break; | ||
|
|
||
| default: | ||
| console.warn('\x1B[31m Unknown action type!'); | ||
| } | ||
| } | ||
|
|
||
| invokeAction(argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| [ | ||
| { | ||
| "id": 1, | ||
| "name": "Allen Raymond", | ||
| "email": "[email protected]", | ||
| "phone": "(992) 914-3792" | ||
| }, | ||
| { | ||
| "id": 2, | ||
| "name": "Chaim Lewis", | ||
| "email": "[email protected]", | ||
| "phone": "(294) 840-6685" | ||
| }, | ||
| { | ||
| "id": 4, | ||
| "name": "Wylie Pope", | ||
| "email": "[email protected]", | ||
| "phone": "(692) 802-2949" | ||
| }, | ||
| { | ||
| "id": 5, | ||
| "name": "Cyrus Jackson", | ||
| "email": "[email protected]", | ||
| "phone": "(501) 472-5218" | ||
| }, | ||
| { | ||
| "id": 6, | ||
| "name": "Abbot Franks", | ||
| "email": "[email protected]", | ||
| "phone": "(186) 568-3720" | ||
| }, | ||
| { | ||
| "id": 7, | ||
| "name": "Reuben Henry", | ||
| "email": "[email protected]", | ||
| "phone": "(715) 598-5792" | ||
| }, | ||
| { | ||
| "id": 8, | ||
| "name": "Simon Morton", | ||
| "email": "[email protected]", | ||
| "phone": "(233) 738-2360" | ||
| }, | ||
| { | ||
| "id": 9, | ||
| "name": "Thomas Lucas", | ||
| "email": "[email protected]", | ||
| "phone": "(704) 398-7993" | ||
| }, | ||
| { | ||
| "id": 10, | ||
| "name": "Alec Howard", | ||
| "email": "[email protected]", | ||
| "phone": "(748) 206-2688" | ||
| }, | ||
| { | ||
| "id": 11, | ||
| "name": "Mango", | ||
| "email": "[email protected]", | ||
| "phone": "322-22-22" | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Хорошой практикой есть разделение функционала. Сделай так что бы эта функция возвращала данные, таким образом ты сможешь их использовать в коде ниже, например в функции (getContactById). Выводить их в консоль будет в конструкции switch case в index.js.