Skip to content
Open

t #5572

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
118 changes: 112 additions & 6 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,120 @@
// const fs = require('fs/promises')
const { v4: uuidv4 } = require("uuid");
const fs = require('fs').promises;
const path = require('path');
const contactsPath = path.join(__dirname, 'contacts.json');

const listContacts = async () => {}
const listContacts = async () => {
try {
const data = await fs.readFile(contactsPath);
const contacts = JSON.parse(data);
return contacts;
} catch (err) {
console.error("Error reading contacts file in listContacts:", err);
throw err;
}
};

const getContactById = async (contactId) => {}
const getContactById = async (contactId) => {
let contacts;

try {
const data = await fs.readFile(contactsPath);
contacts = JSON.parse(data);
} catch (err) {
console.error("Error reading contacts file in getContactById:", err);
throw err;
}

const removeContact = async (contactId) => {}
const contact = contacts.filter((contact) => contact.id === contactId);
if (contact.length === 0) {
return null;
}
return contact;
};

const addContact = async (body) => {}
const removeContact = async (contactId) => {
let contacts;

const updateContact = async (contactId, body) => {}
try {
contacts = await listContacts();
} catch (err) {
console.error("Error reading contacts file in removeContact:", err);
throw err;
}

const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) {
return null;
}

contacts.splice(index, 1);

try {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
} catch (err) {
console.error("Error writing to contacts file in removeContact:", err);
throw err;
}

return contacts;
};

const addContact = async (body) => {
let contacts;

try {
contacts = await listContacts();
} catch (err) {
console.error("Error reading contacts file in addContact:", err);
throw err;
}

const newContact = {
id: uuidv4(),
...body,
};

contacts.push(newContact);

try {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return newContact;
} catch (err) {
console.error("Error writing to contacts file in addContact:", err);
throw err;
}
};

const updateContact = async (contactId, body) => {
let contacts;

try {
contacts = await listContacts();
} catch (err) {
console.error("Error reading contacts file in updateContact:", err);
throw err;
}

const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) {
return null;
}

const newContact = {
id: uuidv4(),
...body,
};

contacts.splice(index, 1, newContact);

try {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return newContact;
} catch (err) {
console.error("Error writing to contacts file in updateContact:", err);
throw err;
}
};

module.exports = {
listContacts,
Expand Down
114 changes: 113 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 @@ -12,7 +12,9 @@
"cors": "2.8.5",
"cross-env": "7.0.3",
"express": "4.17.1",
"morgan": "1.10.0"
"joi": "^17.13.3",
"morgan": "1.10.0",
"uuid": "^11.1.0"
},
"devDependencies": {
"eslint": "7.19.0",
Expand Down
Loading