-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts_api.js
208 lines (169 loc) · 5.06 KB
/
contacts_api.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var express = require('express');
var app = express();
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/mydb');
app.use(function(req,res,next){
req.db = db;
next();
});
app.use(express.bodyParser());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
next();
});
//GET
//Gets contacts, possibly filtered by name, phone and/or email
app.get('/contacts', function(req, res) {
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Getting the fields as regex
var partialName = new RegExp(req.param('name'), "gi");
var partialPhone = new RegExp(req.param('phone'), "gi");
var partialEmail = new RegExp(req.param('email'), "gi");
// Querying the database
collection.find({
name: partialName,
phone: partialPhone,
email: partialEmail
}, function(err, doc) {
if(err) {
res.statusCode = 500;
res.send("There was a problem retrieving information from the database.");
} else {
res.json(doc);
}
});
});
//Retrieves one contact by its ID
app.get('/contacts/:id', function(req, res) {
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Getting the parameter 'id'
var id = req.params.id
// Querying the database
collection.findById(id, function(err, doc) {
if(err) {
res.statusCode = 500;
res.send("There was a problem retrieving information from the database.");
} else if(doc == null) {
res.statusCode = 404;
res.send("No contact with given ID found.");
} else {
res.json(doc);
}
});
});
//PUT
//Updates one contact
app.put('/contacts/:id', function(req, res) {
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Getting the parameter 'id'
var id = req.params.id
// getting the new fields of the contact
var newContactInfo = {};
if(req.body.name) {
newContactInfo.name = req.body.name;
}
if(req.body.phone) {
newContactInfo.phone = req.body.phone;
}
if(req.body.email) {
newContactInfo.email = req.body.email;
}
// Querying the database to update the contact
collection.update({_id: id}, {$set: newContactInfo}, function(err, doc) {
if(err) {
res.statusCode = 500;
res.send("There was a problem updating information in the database.");
} else if(doc == 0) {
res.statusCode = 404;
res.send("Contact with given ID not found.");
} else {
res.json({nr_contacts_updated: doc});
}
});
});
//POST
//Adds one contact, name is mandatory
app.post('/contacts', function(req, res) {
// Checking that there is the mandatory parameter
if(!req.body.hasOwnProperty('name')) {
res.statusCode = 400;
return res.send('Error 400: Name attribute missing.');
}
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Creating a new contact
var newContact = {
name : (req.body.name ? req.body.name : ''),
phone : (req.body.phone ? req.body.phone : ''),
email: (req.body.email ? req.body.email : '')
};
// Inserting the new contact in the database
collection.insert(newContact, function (err, doc) {
if (err) {
// If insert failed, return error
res.statusCode = 500;
res.send("There was a problem adding the information to the database."+err+doc);
}
else {
res.statusCode = 201;
res.json({id: doc._id});
}
});
});
//DELETE
//Deletes contacts, possibly filtered by name, phone and/or email
app.delete('/contacts', function(req, res) {
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Getting the parameters as regex
var partialName = new RegExp(req.param('name'), "gi");
var partialPhone = new RegExp(req.param('phone'), "gi");
var partialEmail = new RegExp(req.param('email'), "gi");
// Querying the database to remove the contact
collection.remove({
name: partialName,
phone: partialPhone,
email: partialEmail
}, function(err, doc) {
if(err) {
res.statusCode = 500;
res.send("There was a problem removing information from the database.");
} else {
res.json({nr_contacts_deleted: doc});
}
});
});
//Deletes one contact by its ID
app.delete('/contacts/:id', function(req, res) {
// Getting the database connection
var db = req.db;
var collection = db.get('contacts');
// Getting the id parameter
var id = req.params.id
// Querying the database to remove the contact
collection.remove({_id: id}, function(err, doc) {
if(err) {
res.statusCode = 500;
res.send("There was a problem retrieving information from the database.");
} else if(doc == 0) {
res.statusCode = 404;
res.send("Contact with given ID not found.");
} else {
res.json({nr_contacts_deleted: doc});
}
});
});
// Serve static web pages
app.use(express.static(__dirname + '/public'));
app.listen(8080);