1
1
const asyncHandler = require ( "express-async-handler" ) ;
2
2
const Contact = require ( "../models/contactModel" ) ;
3
-
4
3
//@desc Get all contacts
5
- //@route GET/api/contacts
6
- //@access public
7
-
8
- const getContacts = asyncHandler ( async ( req , res ) => {
9
- const contacts = await Contact . find ( ) ;
10
- return res . status ( 200 ) . json ( contacts )
11
- } ) ;
12
- //@desc create contacts
13
- //@route POST/api/contacts
14
- //@access public
15
- const createContact = asyncHandler ( async ( req , res ) => {
16
- console . log ( "The req body is :" , req . body )
17
- const { name, email, phone} = req . body ;
18
- if ( ! name || ! email || ! phone ) {
19
- res . status ( 400 ) ;
20
- throw new Error ( "All Fields are mandatory" )
21
- }
22
- const contact = await Contact . create ( {
23
- name,
24
- email,
25
- phone
26
- } )
27
- res . status ( 201 ) . json ( contact )
4
+ //@route GET /api/contacts
5
+ //@access private
6
+ const getContacts = asyncHandler ( async ( req , res ) => {
7
+ const contacts = await Contact . find ( { user_id : req . user . id } ) ;
8
+ res . status ( 200 ) . json ( contacts ) ;
28
9
} ) ;
29
10
30
- //@desc Get contact by id
31
- //@route GET/api/contacts:id
32
- //@access public
11
+ //@desc Create New contact
12
+ //@route POST /api/contacts
13
+ //@access private
14
+ const createContact = asyncHandler ( async ( req , res ) => {
15
+ console . log ( "The request body is :" , req . body ) ;
16
+ const { name, email, phone } = req . body ;
17
+ if ( ! name || ! email || ! phone ) {
18
+ res . status ( 400 ) ;
19
+ throw new Error ( "All fields are mandatory !" ) ;
20
+ }
21
+ const contact = await Contact . create ( {
22
+ name,
23
+ email,
24
+ phone,
25
+ user_id : req . user . id ,
26
+ } ) ;
33
27
34
- const getContact = asyncHandler ( async ( req , res ) => {
35
- const contact = await Contact . findById ( req . params . id ) ;
36
- if ( ! contact ) {
37
- res . status ( 404 ) ;
38
- throw new Error ( "Contact not found" )
39
- }
40
- res . status ( 200 ) . json ( contact )
28
+ res . status ( 201 ) . json ( contact ) ;
41
29
} ) ;
42
30
43
- //@desc update contacts
44
- //@route PUT/api/contacts/:id
45
- //@access public
46
- const updateContact = asyncHandler ( async ( req , res ) => {
47
- const contact = await Contact . findById ( req . params . id ) ;
48
- if ( ! contact ) {
49
- res . status ( 404 ) ;
50
- throw new Error ( "Contact not found" )
51
- }
52
-
53
- const updatedContact = await Contact . findByIdAndUpdate ( req . params . id , req . body , { new : true } )
54
- res . status ( 200 ) . json ( updatedContact )
31
+ //@desc Get contact
32
+ //@route GET /api/contacts/:id
33
+ //@access private
34
+ const getContact = asyncHandler ( async ( req , res ) => {
35
+ const contact = await Contact . findById ( req . params . id ) ;
36
+ if ( ! contact ) {
37
+ res . status ( 404 ) ;
38
+ throw new Error ( "Contact not found" ) ;
39
+ }
40
+ res . status ( 200 ) . json ( contact ) ;
55
41
} ) ;
56
42
57
- //@desc delete contact
58
- //@route delete/api/contacts/:id
59
- //@access public
60
- const deleteContact = asyncHandler ( async ( req , res ) => {
61
- const contact = await Contact . findById ( req . params . id ) ;
62
- if ( ! contact ) {
63
- res . status ( 404 ) ;
64
- throw new Error ( "Contact not found" )
65
- }
66
- const deletedContect = await Contact . findOneAndDelete ( ) ;
67
- res . status ( 200 ) . json ( contact ) ;
68
- } ) ;
43
+ //@desc Update contact
44
+ //@route PUT /api/contacts/:id
45
+ //@access private
46
+ const updateContact = asyncHandler ( async ( req , res ) => {
47
+ const contact = await Contact . findById ( req . params . id ) ;
48
+ if ( ! contact ) {
49
+ res . status ( 404 ) ;
50
+ throw new Error ( "Contact not found" ) ;
51
+ }
69
52
53
+ if ( contact . user_id . toString ( ) !== req . user . id ) {
54
+ res . status ( 403 ) ;
55
+ throw new Error ( "User don't have permission to update other user contacts" ) ;
56
+ }
70
57
58
+ const updatedContact = await Contact . findByIdAndUpdate (
59
+ req . params . id ,
60
+ req . body ,
61
+ { new : true }
62
+ ) ;
71
63
72
- module . exports = { getContacts, createContact, getContact, deleteContact, updateContact}
64
+ res . status ( 200 ) . json ( updatedContact ) ;
65
+ } ) ;
73
66
67
+ //@desc Delete contact
68
+ //@route DELETE /api/contacts/:id
69
+ //@access private
70
+ const deleteContact = asyncHandler ( async ( req , res ) => {
71
+ const contact = await Contact . findById ( req . params . id ) ;
72
+ if ( ! contact ) {
73
+ res . status ( 404 ) ;
74
+ throw new Error ( "Contact not found" ) ;
75
+ }
76
+ if ( contact . user_id . toString ( ) !== req . user . id ) {
77
+ res . status ( 403 ) ;
78
+ throw new Error ( "User don't have permission to update other user contacts" ) ;
79
+ }
80
+ await Contact . deleteOne ( { _id : req . params . id } ) ;
81
+ res . status ( 200 ) . json ( contact ) ;
82
+ } ) ;
74
83
84
+ module . exports = {
85
+ getContacts,
86
+ createContact,
87
+ getContact,
88
+ updateContact,
89
+ deleteContact,
90
+ } ;
0 commit comments