-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontactController.js
More file actions
91 lines (81 loc) · 2.39 KB
/
contactController.js
File metadata and controls
91 lines (81 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const asyncHandler=require("express-async-handler");
const Contact=require("../models/contactModel");
//@desc Get all contacts
//@route GET /api/contacts
//@access public
const getContacts = asyncHandler(async(req, res)=>{
const contacts=await Contact.find({user_id:req.user_id});
res.status(200).json(contacts);
});
//@desc Create new contact
//@route POST /api/contacts
//@access private
const createContact = asyncHandler(async(req, res)=>{
console.log("the request body is :",req.body);
const{name,email,phone}=req.body;
if(!name || !email || !phone){
res.status(400);
throw new Error("All fields are mandatory!");
}
const contact=await Contact.create({
user_id:req.user.id,
name,
email,
phone,
});
res.status(201).json(contact);
});
//@desc Get a single contact by ID
//@route GET /api/contacts/:id
//@access private
const getContact = asyncHandler(async(req, res)=>{
const contact=await Contact.findById(req.params.id);
if(!contact){
res.status(404);
throw new Error("Contact not found");
}
res.status(200).json(contact);
});
//@desc Update a contact
//@route PUT /api/contacts/:id
//@access private
const updateContact = asyncHandler(async(req, res)=>{
const contact=await Contact.findById(req.params.id);
if(!contact){
res.status(404);
throw new Error("Contact not found");
}
if(contact.user_id.toString()!== req.user.id){
res.status(403);
throw new Error("User dont have permission to update other users");
}
const updatedContact=await Contact.findByIdAndUpdate(
req.params.id,
req.body,
{new:true}
);
res.status(200).json(updatedContact);
});
//@desc Delete a contact
//@route DELETE /api/contacts/:id
//@access private
const deleteContact = asyncHandler(async (req, res)=>{
const contact = await Contact.findById(req.params.id);
if (!contact) {
res.status(404);
throw new Error("Contact not found");
}
if(contact.user_id.toString()!== req.user.id){
res.status(403);
throw new Error("User dont have permission to update other users");
}
await contact.deleteOne(); // ← remove that specific document
res.status(200).json(contact); // or you can send a success message
});
module.exports = {
getContacts,
createContact,
getContact,
updateContact,
deleteContact,
};