Skip to content

Commit 6c67fe5

Browse files
committed
myContacts-backend
1 parent e21bc4c commit 6c67fe5

File tree

11 files changed

+336
-149
lines changed

11 files changed

+336
-149
lines changed

.vscode/launch.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": []
7+
}

package-lock.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dotenv": "^16.3.1",
1414
"express": "^4.18.2",
1515
"express-async-handler": "^1.2.0",
16+
"jsonwebtoken": "^9.0.2",
1617
"mongodb": "^6.3.0",
1718
"mongoose": "^8.0.3",
1819
"punycode": "^2.3.1"

src/config/dbConnection.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
const mongoose = require("mongoose");
22

3-
4-
const connectDb = async ()=>{
5-
try{
6-
const connect = await mongoose.connect(process.env.CONNECTION_STRING);
7-
console.log("Database Connected",
8-
connect.connection.host,
9-
connect.connection.name
10-
);
11-
}catch(err){
12-
console.log(err)
13-
process.exit(1)
14-
}
3+
const connectDb = async () => {
4+
try {
5+
const connect = await mongoose.connect(process.env.CONNECTION_STRING);
6+
console.log(
7+
"Database connected: ",
8+
connect.connection.host,
9+
connect.connection.name
10+
);
11+
} catch (err) {
12+
console.log(err);
13+
process.exit(1);
14+
}
1515
};
1616

1717
module.exports = connectDb;

src/controllers/contactController.js

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,90 @@
11
const asyncHandler = require("express-async-handler");
22
const Contact = require("../models/contactModel");
3-
43
//@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);
289
});
2910

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+
});
3327

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);
4129
});
4230

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);
5541
});
5642

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+
}
6952

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+
}
7057

58+
const updatedContact = await Contact.findByIdAndUpdate(
59+
req.params.id,
60+
req.body,
61+
{ new: true }
62+
);
7163

72-
module.exports = {getContacts,createContact, getContact, deleteContact,updateContact}
64+
res.status(200).json(updatedContact);
65+
});
7366

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+
});
7483

84+
module.exports = {
85+
getContacts,
86+
createContact,
87+
getContact,
88+
updateContact,
89+
deleteContact,
90+
};

0 commit comments

Comments
 (0)