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

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

const getContactById = async (contactId) => {}
const writeContacts = async (contacts) => {
await fs.writeFile(contactsPath, JSON.stringify(contacts, null, 2));
};

const removeContact = async (contactId) => {}
const listContacts = async () => {
return await readContacts();
};

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

const updateContact = async (contactId, body) => {}
const removeContact = async (contactId) => {
const contacts = await readContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) return null;
const [removedContact] = contacts.splice(index, 1);
await writeContacts(contacts);
return removedContact;
};

const addContact = async (body) => {
const newContact = { id: uuidv4(), ...body };
const contacts = await readContacts();
contacts.push(newContact);
await writeContacts(contacts);
return newContact;
};

const updateContact = async (contactId, body) => {
const contacts = await readContacts();
const index = contacts.findIndex((contact) => contact.id === contactId);
if (index === -1) return null;
contacts[index] = { ...contacts[index], ...body };
await writeContacts(contacts);
return contacts[index];
};

module.exports = {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
}
};
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": "3c2bfa16-21d1-4cc5-81fb-49ede91a331d",
"name": "Paul Mariuta",
"email": "mariuta.paul08@gmail.com",
"phone": "0771-586-458"
}
]
]
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.0.3"
},
"devDependencies": {
"eslint": "7.19.0",
Expand Down
103 changes: 85 additions & 18 deletions routes/api/contacts.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,92 @@
const express = require('express')
const express = require("express");
const Joi = require("joi");
const {
listContacts,
getContactById,
removeContact,
addContact,
updateContact,
} = require("../../models/contacts");

const router = express.Router()
const router = express.Router();

router.get('/', async (req, res, next) => {
res.json({ message: 'template message' })
})
// field-uri obligatorii la adaugarea unui Contact
const contactSchema = Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required(),
phone: Joi.string().required(),
});

router.get('/:contactId', async (req, res, next) => {
res.json({ message: 'template message' })
})
// cel putin un field trebuie modificat pt update
const updateSchema = Joi.object({
name: Joi.string(),
email: Joi.string().email(),
phone: Joi.string(),
}).min(1);

router.post('/', async (req, res, next) => {
res.json({ message: 'template message' })
})
// list all contacts
router.get("/", async (req, res, next) => {
try {
const contacts = await listContacts();
res.status(200).json(contacts);
} catch (error) {
next(error);
}
});

router.delete('/:contactId', async (req, res, next) => {
res.json({ message: 'template message' })
})
// getContactById
router.get("/:contactId", async (req, res, next) => {
try {
const { contactId } = req.params;
const contact = await getContactById(contactId);
if (!contact)
return res.status(404).json({ message: "Contact Not Found ^_^" });
res.status(200).json(contact);
} catch (error) {
next(error);
}
});

router.put('/:contactId', async (req, res, next) => {
res.json({ message: 'template message' })
})
// add a new Contact
router.post("/", async (req, res, next) => {
try {
const { error } = contactSchema.validate(req.body);
if (error)
return res.status(400).json({ message: "Missing required fileds ^_^" });

module.exports = router
const newContact = await addContact(req.body);
res.status(201).json(newContact);
} catch (error) {
next(error);
}
});

// remove a Contact
router.delete("/:contactId", async (req, res, next) => {
try {
const { contactId } = req.params;
const contact = await removeContact(contactId);
if (!contact)
return res.status(404).json({ message: "Contact Not Found in list ^_^" });
res.status(200).json({ message: "Contact deleted successfully ^_^" });
} catch (error) {
next(error);
}
});

// update a Contact
router.put("/:contactId", async (req, res, next) => {
try {
const { contactId } = req.params;
const { error } = updateSchema.validate(req.body);
if (error) return res.status(400).json({ message: "Missing fiels ^_^" });
const updatedContact = await updateContact(contactId);
if (!updatedContact)
return res.status(404).json({ message: "Contact Not Found in list ^_^" });
res.status(200).json(updatedContact);
} catch (error) {
next(error);
}
});

module.exports = router;