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
98 changes: 91 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,103 @@
// const fs = require('fs/promises')
const fs = require("fs").promises;
const path = require("path");
const contactsPath = path.join(__dirname, "contacts.json");

const listContacts = async () => {}
async function generateId() {
const { nanoid } = await import("nanoid");
return nanoid();
}

const listContacts = async () => {
try {
const data = await fs.readFile(contactsPath, "utf8");
const contacts = JSON.parse(data);
return contacts;
} catch (err) {
console.log(err.message);
}
};

const getContactById = async (contactId) => {}
const getContactById = async (contactId) => {
try {
const contacts = await listContacts();
const contact = contacts.find(
(contact) => contact.id === contactId.toString()
);
return contact;
} catch (err) {
console.log(err.message);
}
};

const removeContact = async (contactId) => {}
const removeContact = async (contactId) => {
try {
const contacts = await listContacts();
if (contacts) {
const contactExists = contacts.some(
(contact) => contact.id === contactId.toString()
);
if (!contactExists) {
return false;
}
const updatedContacts = contacts.filter(
(contact) => contact.id !== contactId.toString()
);
await fs.writeFile(
contactsPath,
JSON.stringify(updatedContacts, null, 2)
);
return true;
}
} catch (err) {
console.log(err.message);
}
};

const addContact = async (body) => {}
const addContact = async (body) => {
try {
const contacts = await listContacts();
const { name, email, phone } = body;
const newId = (await generateId()).toString();
const newContact = { id: newId, name, email, phone };
contacts.push(newContact);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return newContact;
} catch (err) {
console.log(err.message);
}
};

const updateContact = async (contactId, body) => {}
const updateContact = async (contactId, body) => {
try {
const contacts = await listContacts();
if (contacts) {
const contactExists = contacts.some(
(contact) => contact.id === contactId.toString()
);
if (!contactExists) {
return false;
} else {
const { name, email, phone } = body;
const updatedContact = { id: contactId, name, email, phone };
const updatedContacts = contacts.map((contact) =>
contact.id === contactId.toString() ? updatedContact : contact
);
await fs.writeFile(
contactsPath,
JSON.stringify(updatedContacts, null, 2)
);
return updatedContact;
}
}
} catch (err) {
console.log(err.message);
}
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
};
2 changes: 1 addition & 1 deletion models/contacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@
"email": "[email protected]",
"phone": "(748) 206-2688"
}
]
]
115 changes: 114 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"lint:fix": "eslint --fix **/*.js"
},
"dependencies": {
"@hapi/joi": "^17.1.1",
"cors": "2.8.5",
"cross-env": "7.0.3",
"express": "4.17.1",
"morgan": "1.10.0"
"morgan": "1.10.0",
"nanoid": "^5.1.0"
},
"devDependencies": {
"eslint": "7.19.0",
Expand Down
Loading