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
68 changes: 61 additions & 7 deletions models/contacts.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,73 @@
// const fs = require('fs/promises')
const fs = require("fs/promises");
const path = require("path");
const { nanoid } = require("nanoid");
const Joi = require("joi");

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

const getContactById = async (contactId) => {}
const contactSchema = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
phone: Joi.string().required(),
});

const removeContact = async (contactId) => {}
const updateContactSchema = Joi.object({
name: Joi.string(),
email: Joi.string().email(),
phone: Joi.string(),
}).min(1);

const addContact = async (body) => {}
const listContacts = async () => {
const data = await fs.readFile(contactsPath, "utf-8");
return JSON.parse(data);
};

const updateContact = async (contactId, body) => {}
const getContactById = async (contactId) => {
const contacts = await listContacts();
return contacts.find((contact) => contact.id === contactId) || null;
};

const removeContact = async (contactId) => {
const contacts = await listContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) return null;
const [removedContact] = contacts.splice(index, 1);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return removedContact;
};

const addContact = async (body) => {
const { error } = contactSchema.validate(body);
if (error) {
throw new Error(`Invalid input: ${error.details[0].message}`);
}

const contacts = await listContacts();
const newContact = { id: nanoid(), ...body };
contacts.push(newContact);
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
return newContact;
};

const updateContact = async (contactId, body) => {
const { error } = updateContactSchema.validate(body);
if (error) {
throw new Error("Invalid update data");
}

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,
}
};
26 changes: 19 additions & 7 deletions models/contacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
},
{
"id": "qdggE76Jtbfd9eWJHrssH",
"name": "Chaim Lewis",
"email": "dui.in@egetlacus.ca",
"phone": "(294) 840-6685"
"name": "Gray Ashton",
"email": "grays@gmail.com",
"phone": "(098) 456-0987"
},
{
"id": "drsAJ4SHPYqZeG-83QTVW",
Expand Down Expand Up @@ -43,9 +43,9 @@
},
{
"id": "C9sjBfCo4UJCWjzBnOtxl",
"name": "Simon Morton",
"email": "dui.Fusce.diam@Donec.com",
"phone": "(233) 738-2360"
"name": "Jarett Krone",
"email": "kronej@example.com",
"phone": "(123) 456-0987"
},
{
"id": "e6ywwRe4jcqxXfCZOj_1e",
Expand All @@ -58,5 +58,17 @@
"name": "Alec Howard",
"email": "Donec.elementum@scelerisquescelerisquedui.net",
"phone": "(748) 206-2688"
},
{
"id": "DQImGPlBmZztO2_a1c2Fo",
"name": "Darren Fox",
"email": "foxd@example.com",
"phone": "(123) 456-7890"
},
{
"id": "b3rfF3cosCSADV6YvCAny",
"name": "Mircea Bravo",
"email": "mbravo@mircea.com",
"phone": "(456) 456-2347"
}
]
]
Loading