-
Notifications
You must be signed in to change notification settings - Fork 0
/
contactController.js
86 lines (76 loc) · 1.78 KB
/
contactController.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
//import contact model
Contact = require('./contactModel');
//Handle index actions
exports.index = function (req,res){
Contact.get(function (err,contacts) {
if(err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
massage: "Retrived successfully",
data: contacts
});
});
};
//Handle create contact actions
exports.new = function (req,res) {
var contact = new Contact();
contact.html = req.params.html;
contact.css = req.params.css;
//save the contact and check for errors
contact.save(function (err){
// if(err)
// res.json(err)
res.json({
message: 'Created!',
data: contact
});
});
};
//Handle view contact info
exports.view = function (req,res){
Contact.findById(req.params.contact_id, function (err, contact)
{
if(err)
res.send(err);
res.json({
message: "Loading..",
data: contact
});
});
};
//Handle update contact info
exports.update = function (req,res){
Contact.findById(req.params.contact_id, function (err, contact){
if(err)
res.send(err);
contact.html = req.params.html;
contact.css = req.params.css;
//save the contact and check for error
contact.save(function (err){
if(err)
res.json(err);
res.json({
message: 'Info updated',
data: contact
});
});
});
};
//Handle delete contact
exports.delete = function (req,res){
Contact.remove({
_id: req.params.contact_id
}, function (err, contact){
if(err)
res.send(err);
res.json({
status: "success",
message: "Deleted"
});
});
};