|
1 | 1 | import { HttpError } from "../helpers/index.js";
|
2 | 2 | import { ctrlWrapper } from "../decorators/index.js";
|
3 | 3 | import Contact from "../models/Contact.js";
|
4 |
| -// import { |
5 |
| -// newContactValidation, |
6 |
| -// contactsEditValidation, |
7 |
| -// } from "../schemas/contacts-validation.js"; |
8 |
| -// import { |
9 |
| -// addContact, |
10 |
| -// getContactById, |
11 |
| -// listContacts, |
12 |
| -// removeContact, |
13 |
| -// updateContact, |
14 |
| -// } from "../models/contacts.js"; |
| 4 | +import { |
| 5 | + newContactValidation, |
| 6 | + contactsEditValidation, |
| 7 | + contactsFavoriteValidation, |
| 8 | +} from "../schemas/contacts-validation.js"; |
15 | 9 |
|
16 | 10 | async function getAllContacts(req, res, next) {
|
17 | 11 | const contacts = await Contact.find();
|
18 |
| - console.log(contacts); |
19 |
| - // const contacts = await listContacts(); |
20 | 12 | if (contacts) {
|
21 | 13 | res.json(contacts);
|
22 | 14 | } else {
|
23 | 15 | throw HttpError(404, "No contacts found");
|
24 | 16 | }
|
25 | 17 | }
|
26 | 18 |
|
27 |
| -// async function getContactsById(req, res, next) { |
28 |
| -// const id = req.params.contactId; |
29 |
| -// const contactById = await getContactById(id); |
30 |
| -// if (contactById) { |
31 |
| -// res.json(contactById); |
32 |
| -// } else { |
33 |
| -// throw HttpError(404, "Contact not found"); |
34 |
| -// } |
35 |
| -// } |
| 19 | +async function getContactsById(req, res, next) { |
| 20 | + const id = req.params.contactId; |
| 21 | + const contactById = await Contact.findById(id); |
| 22 | + if (contactById) { |
| 23 | + res.json(contactById); |
| 24 | + } else { |
| 25 | + throw HttpError(404, "Contact not found"); |
| 26 | + } |
| 27 | +} |
36 | 28 |
|
37 |
| -// async function addNewContact(req, res, next) { |
38 |
| -// const { error } = newContactValidation.validate(req.body); |
39 |
| -// if (error) { |
40 |
| -// throw HttpError(400, error.message); |
41 |
| -// } |
42 |
| -// const newContact = await addContact(req.body); |
43 |
| -// res.status(201).json(newContact); |
44 |
| -// } |
| 29 | +async function addNewContact(req, res, next) { |
| 30 | + const { error } = newContactValidation.validate(req.body); |
| 31 | + if (error) { |
| 32 | + throw HttpError(400, error.message); |
| 33 | + } |
| 34 | + const newContact = await Contact.create(req.body); |
| 35 | + console.log(newContact); |
| 36 | + res.status(201).json(newContact); |
| 37 | +} |
45 | 38 |
|
46 |
| -// async function deleteContact(req, res, next) { |
47 |
| -// const id = req.params.contactId; |
48 |
| -// const deleteContact = await removeContact(id); |
49 |
| -// if (deleteContact) { |
50 |
| -// res.json({ message: "contact deleted", status: 200 }); |
51 |
| -// } else { |
52 |
| -// throw HttpError(404); |
53 |
| -// } |
54 |
| -// } |
| 39 | +async function deleteContact(req, res, next) { |
| 40 | + const id = req.params.contactId; |
| 41 | + const deleteContact = await Contact.findByIdAndDelete(id); |
| 42 | + console.log(deleteContact); |
| 43 | + if (deleteContact) { |
| 44 | + res.json({ message: "contact deleted", status: 200 }); |
| 45 | + } else { |
| 46 | + throw HttpError(404); |
| 47 | + } |
| 48 | +} |
55 | 49 |
|
56 |
| -// async function editContact(req, res, next) { |
57 |
| -// const { error } = contactsEditValidation.validate(req.body); |
58 |
| -// if (error) { |
59 |
| -// throw HttpError(400, error.message); |
60 |
| -// } |
61 |
| -// const id = req.params.contactId; |
62 |
| -// const updatedContact = await updateContact(id, req.body); |
63 |
| -// if (Object.keys(req.body).length === 0) { |
64 |
| -// throw HttpError(404, "missing fields"); |
65 |
| -// } |
66 |
| -// if (!updatedContact) { |
67 |
| -// throw HttpError(404, "Not found"); |
68 |
| -// } |
69 |
| -// res.status(201).json(updatedContact); |
70 |
| -// } |
| 50 | +async function editContact(req, res, next) { |
| 51 | + const { error } = contactsEditValidation.validate(req.body); |
| 52 | + if (error) { |
| 53 | + throw HttpError(400, error.message); |
| 54 | + } |
| 55 | + const id = req.params.contactId; |
| 56 | + const updatedContact = await Contact.findByIdAndUpdate(id, req.body, { |
| 57 | + new: true, |
| 58 | + }); |
| 59 | + if (!updatedContact) { |
| 60 | + throw HttpError(404, "Not found"); |
| 61 | + } |
| 62 | + res.json(updatedContact); |
| 63 | +} |
| 64 | + |
| 65 | +async function updateContact(req, res, next) { |
| 66 | + const { error } = contactsFavoriteValidation.validate(req.body); |
| 67 | + if (error) { |
| 68 | + throw HttpError(400, error.message); |
| 69 | + } |
| 70 | + const id = req.params.contactId; |
| 71 | + const updatedContact = await Contact.findByIdAndUpdate(id, req.body, { |
| 72 | + new: true, |
| 73 | + }); |
| 74 | + |
| 75 | + if (!updatedContact) { |
| 76 | + throw HttpError(404, "Not found"); |
| 77 | + } |
| 78 | + |
| 79 | + res.json(updatedContact); |
| 80 | +} |
71 | 81 |
|
72 | 82 | export default {
|
73 | 83 | getAllContacts: ctrlWrapper(getAllContacts),
|
74 |
| - // getContactsById: ctrlWrapper(getContactsById), |
75 |
| - // addNewContact: ctrlWrapper(addNewContact), |
76 |
| - // deleteContact: ctrlWrapper(deleteContact), |
77 |
| - // editContact: ctrlWrapper(editContact), |
| 84 | + getContactsById: ctrlWrapper(getContactsById), |
| 85 | + addNewContact: ctrlWrapper(addNewContact), |
| 86 | + deleteContact: ctrlWrapper(deleteContact), |
| 87 | + editContact: ctrlWrapper(editContact), |
| 88 | + updateContact: ctrlWrapper(updateContact), |
78 | 89 | };
|
0 commit comments