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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

.idea/
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

Expand Down
10 changes: 9 additions & 1 deletion README.md
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)
73 changes: 73 additions & 0 deletions contacts.js
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);
Copy link
Collaborator

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.

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
};
39 changes: 39 additions & 0 deletions index.js
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);
62 changes: 62 additions & 0 deletions model/test.json
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"
}
]
Loading