Skip to content
Merged
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
90 changes: 83 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,95 @@
// const fs = require('fs/promises')
const fs = require("fs").promises;
const { nanoid } = require("nanoid");
const path = require("path");

const listContacts = async () => {}
const contactsPath = path.join(__dirname, "../models/contacts.json");

const getContactById = async (contactId) => {}
const Joi = require("joi");

const removeContact = async (contactId) => {}
const schema = Joi.object({
name: Joi.string().min(3).max(30).required(),
email: Joi.string().email().required(),
phone: Joi.string()
.pattern(/^[0-9\-()+ ]+$/)
.required(),
});

const addContact = async (body) => {}
const updateSchema = Joi.object({
name: Joi.string().min(3).max(30),
email: Joi.string().email(),
phone: Joi.string().pattern(/^[0-9\-()+ ]+$/),
});

const updateContact = async (contactId, body) => {}
const listContacts = async () => {
const data = await fs.readFile(contactsPath, "utf8");
const contacts = JSON.parse(data);
return contacts;
};

const getContactById = async (contactId) => {
const contacts = await listContacts();
const contact = contacts.find((contact) => contact.id === contactId);

return contact || null;
};

const removeContact = async (contactId) => {
const contacts = await listContacts();
const filteredContacts = contacts.filter(
(contact) => contact.id !== contactId
);

if (contacts.length === filteredContacts.length) {
return null;
}

await fs.writeFile(contactsPath, JSON.stringify(filteredContacts, null, 2));

return true;
};

const addContact = async (name, email, phone) => {
const contacts = await listContacts();

const isDuplicate = contacts.some(
(contact) => contact.email === email || contact.phone === phone
);

if (isDuplicate) {
return null;
}

const newContact = {
id: nanoid(),
name,
email,
phone,
};

contacts.push(newContact);

await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));

return newContact;
};

const updateContact = async (contactId, body) => {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);

if (index === -1) return null;

contacts[index] = { ...contacts[index], ...body };
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return contacts[index];
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
schema,
updateSchema,
};
8 changes: 7 additions & 1 deletion models/contacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@
"name": "Alec Howard",
"email": "Donec.elementum@scelerisquescelerisquedui.net",
"phone": "(748) 206-2688"
},
{
"id": "dZ7QVAS8InYXlOwOeLN9X",
"name": "Magda",
"email": "magdat@gmail.com",
"phone": "123456789"
}
]
]
Loading