-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (80 loc) · 2.55 KB
/
index.js
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
92
93
94
const express = require('express');
const path = require('path');
const db = require('./config/mongoos')
const Contact = require('./models/contact')
const port = 8000;
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.urlencoded({ extended: true }));
app.get('/' , (req , res) =>{
return res.render('form' , {
title : "contactVision Form",
editMode: false,
});
});
app.post('/create-contact', async (req, res) => {
try {
// Adding contact to the database using async/await
const newContact = await Contact.create({
firstname: req.body.first_name,
phone: req.body.phone_number,
address: req.body.address
});
console.log("New contact created:", newContact);
res.redirect('getContacts');
} catch (err) {
console.log("Error creating contact:", err);
res.status(500).send("Error creating contact");
}
});
app.get('/getContacts' , async (req ,res) => {
const contactLists = await Contact.find();
return res.render('listContact', {
title : "contact",
contact : contactLists,
editMode: false
})
})
app.post("/delete-contact" , async (req ,res) => {
const firstNameToDelete = req.body.id;
const result = await Contact.findByIdAndDelete(firstNameToDelete);
return res.redirect('getContacts');
})
app.get('/edit-contact/:_id', async (req, res) => {
const id= req.params._id;
const contactLists = await Contact.find();
const contact = await Contact.findById(id);
if (contact) {
return res.render('editform', {
title: "Edit Contact",
contact: contactLists,
editMode: true,
editContact: contact
});
} else {
return res.status(404).send('Contact not found');
}
});
app.post('/update-contact/:id', async (req, res) => {
const id = req.params.id;
console.log(id);
console.log(req.body);
console.log(req.body.firstname);
const updatedContact = await Contact.findByIdAndUpdate(
id,
{
firstname: req.body.firstname,
phone: req.body.phone,
address: req.body.address
},
{ new: true } // Return the updated document
);
res.redirect('/getContacts');
});
app.listen(port , (err) => {
if(err)
console.log("error in running the server" , err);
console.log("yup! my express server is running on port:" , port);
});