Skip to content

Commit 1957909

Browse files
author
alexamm1
committed
updates to modify and delete records
1 parent 867d17d commit 1957909

File tree

1 file changed

+135
-54
lines changed

1 file changed

+135
-54
lines changed

app.js

Lines changed: 135 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var express = require('express');
22
var app = express();
33
var mongoClient = require('mongodb').MongoClient;
4-
var port = 8080;
4+
var port = 9876;
55
var http = require('http');
66
var host = "127.0.0.1";
77
var mongoPort = "27017";
@@ -12,9 +12,8 @@ var multer = require('multer');
1212

1313
app.use(bodyParser.json()); // for parsing application/json
1414
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
15-
//app.use(multer({dest:'./uploads/'}).single('upload')); // for parsing multipart/form-data
1615

17-
function parseQueryParams(urlQueryParams, callback) { // callback
16+
function parseQueryParams(urlQueryParams, callback) { // parses query parameters
1817
var mongoQuery = {};
1918

2019
console.log(JSON.stringify(urlQueryParams));
@@ -71,46 +70,94 @@ function parseQueryParams(urlQueryParams, callback) { // callback
7170
callback(null, mongoQuery);
7271
}
7372

74-
75-
app.get('/kki42', function(req, res) {
76-
//Sub Number,SubjectID,Group,Fiducial,Age,Sex,Subject Code,_id
77-
// if want to do age range, can either do ?ageRange=12,15
78-
var query;
73+
app.get('/collection/:coll_name/delete/:id/', function(req, res) {
74+
// this api takes an id of a record of a particular collection and deletes it
75+
var collName = req.params.coll_name;
76+
var idVal = parseInt(req.params.id);
77+
var rowQuery = {};
78+
rowQuery['_id'] = idVal;
7979

80-
parseQueryParams(req.query, function(err, params) {
81-
query = params;
82-
console.log(params);
80+
console.log(rowQuery);
8381

82+
mongoClient.connect(connectionString, function(err,db) {
83+
db.collection(collName).remove(rowQuery, function(err) {
84+
if (err) {
85+
res.sendStatus(404);
86+
res.end();
87+
}
88+
else {
89+
res.sendStatus(200);
90+
res.end();
91+
}
92+
db.close();
93+
});
8494
});
8595

86-
console.log("yay, this is the subject information API!");
96+
});
8797

98+
app.get('/collection/:coll_name/unset/:id/:field_name', function(req, res) {
99+
// this api takes a record with a particular id (:id) from a collection (:coll_name) and removes a particular field (:field_name)
100+
var collName = req.params.coll_name;
101+
var idVal = parseInt(req.params.id);
102+
var fieldToSet = req.params.field_name;
103+
var rowQuery = {};
104+
rowQuery['_id'] = idVal;
105+
var updateQuery = {};
106+
updateQuery[fieldToSet] = "";
107+
108+
console.log(collName);
109+
console.log(rowQuery);
110+
console.log(updateQuery);
111+
88112
mongoClient.connect(connectionString, function(err,db) {
89-
90-
if(err) {
91-
callback(err,null);
92-
}
93-
var collName = "kki42";
94-
95-
db.collection(collName).find(query).toArray(function(err, results) {
96-
if(err) {
97-
res.send("Sorry, this result cannot be found!");
98-
res.end();
113+
db.collection(collName).update(rowQuery,{$unset:updateQuery}, function(err) {
114+
if (err) {
115+
res.sendStatus(404);
116+
res.end();
99117
}
100118
else {
101-
res.setHeader('Content-Type', 'application/json');
102-
res.send(results);
119+
res.sendStatus(200);
103120
res.end();
104-
db.close();
105121
}
106-
}); // end of collection connection
107-
}); // end of db connection
122+
db.close();
123+
});
124+
});
125+
126+
});
108127

128+
app.get('/collection/:coll_name/set/:id/:field_name', function(req, res) {
129+
// this api takes a record with a particular id (:id) from a collection (:coll_name) and removes a particular field (:field_name)
130+
var collName = req.params.coll_name;
131+
var idVal = parseInt(req.params.id);
132+
var fieldToSet = req.params.field_name;
133+
var valueToSet = req.query.value;
134+
var rowQuery = {};
135+
rowQuery['_id'] = idVal;
136+
var updateQuery = {};
137+
updateQuery[fieldToSet] = valueToSet;
138+
139+
console.log(collName);
140+
console.log(rowQuery);
141+
console.log(updateQuery);
142+
143+
mongoClient.connect(connectionString, function(err,db) {
144+
db.collection(collName).update(rowQuery,{$set:updateQuery}, function(err) {
145+
if (err) {
146+
res.sendStatus(404);
147+
res.end();
148+
}
149+
else {
150+
res.sendStatus(200);
151+
res.end();
152+
}
153+
db.close();
154+
});
155+
});
156+
109157
});
110158

111159
app.get('/collection/:coll_name', function(req, res) {
112-
//Sub Number,SubjectID,Group,Fiducial,Age,Sex,Subject Code,_id
113-
// if want to do age range, can either do ?ageRange=12,15
160+
// this api returns all records contained in a particular collection
114161
var collName = req.params.coll_name;
115162
var query;
116163

@@ -143,9 +190,8 @@ app.get('/collection/:coll_name', function(req, res) {
143190

144191
});
145192

146-
147-
// check to see if a field exists in a collection, if so, return it
148193
app.get('/collection/:coll_name/exists/:field_name', function(req, res) {
194+
// check to see if a field exists in a particular collection, if so, return it
149195
var collName = req.params.coll_name;
150196
var fieldName = req.params.field_name;
151197
var query = {};
@@ -178,7 +224,41 @@ app.get('/collection/:coll_name/exists/:field_name', function(req, res) {
178224

179225
});
180226

227+
app.get('/collection/:coll_name/subnum/:subj_num', function(req, res) {
228+
// this api gets all metadata from a particular collection belonging to a particular subject number
229+
console.log("yay, this is the specific subject information API!");
230+
var collName = req.params.coll_name;
231+
var sub_num = req.params.subj_num;
232+
var subject_num = parseInt(sub_num);
233+
234+
if(req.query) {
235+
console.log("query parameters are: " + JSON.stringify(req.query));
236+
}
237+
238+
mongoClient.connect(connectionString, function(err,db) {
239+
240+
if(err) {
241+
callback(err,null);
242+
}
243+
244+
db.collection(collName).find({"Sub Number" : subject_num}).toArray(function(err, results) {
245+
if(err) {
246+
res.send("Sorry, this result cannot be found!");
247+
res.end();
248+
}
249+
else {
250+
res.setHeader('Content-Type', 'application/json');
251+
res.send(results);
252+
res.end();
253+
db.close();
254+
}
255+
}); // end of collection connection
256+
}); // end of db connection
257+
258+
});
259+
181260
app.post('/lims/:project_name', function(req, res) {
261+
// this api accepts JSON and inserts it into the db with corresponding project name
182262
var json = req.body;
183263
var projName = req.params.project_name;
184264

@@ -194,7 +274,7 @@ app.post('/lims/:project_name', function(req, res) {
194274

195275
db.collection('channels').insert(json, function(err, results) {
196276
if(err) {
197-
res.send("Sorry, you done messed up!");
277+
res.send("Sorry, your data is in the wrong format!");
198278
res.end();
199279
}
200280
else {
@@ -208,62 +288,63 @@ app.post('/lims/:project_name', function(req, res) {
208288

209289
});
210290

211-
app.get('/lims/:project_name', function(req, res) {
212-
var projName = req.params.project_name;
291+
app.post('/lims/:token/:channel', function(req, res) {
292+
// this api accepts JSON and inserts it into the db
293+
var json = req.body;
294+
var token = req.params.token;
295+
var channel = req.params.channel;
296+
var collName = "test";
213297
console.log("woo, this is the post API!");
214-
215-
var query = {};
216-
query.project_name = projName;
298+
299+
console.log(json);
217300

218301
mongoClient.connect(connectionString, function(err,db) {
219302

220303
if(err) {
221304
callback(err,null);
222305
}
223306

224-
db.collection('channels').find(query).toArray(function(err, results) {
307+
db.collection(collName).insert(json, function(err, results) {
225308
if(err) {
226-
res.send("Sorry, you done messed up!");
309+
res.send("Sorry, your stuff isn't in the right format!");
227310
res.end();
228311
}
229312
else {
230313
res.setHeader('Content-Type', 'application/json');
231314
res.send(results);
232315
res.end();
316+
db.close();
233317
}
234318
}); // end of collection connection
235319
}); // end of db connection
236320

237321
});
238322

239-
240-
// get subject info by subject number
241-
app.get('/collection/:coll_name/subnum/:subj_num', function(req, res) {
242-
console.log("yay, this is the specific subject information API!");
243-
var collName = req.params.coll_name;
244-
var sub_num = req.params.subj_num;
245-
var subject_num = parseInt(sub_num);
323+
app.get('/lims/:token/:channel_name', function(req, res) {
324+
// this api gets all metadata corresponding to a particular token and channel name
325+
var token = req.params.token;
326+
var chanName = req.params.channel_name;
327+
console.log("woo, this is the post API!");
328+
329+
var query = {};
330+
query.name = chanName;
331+
query.tokens = token;
246332

247-
if(req.query) {
248-
console.log("query parameters are: " + JSON.stringify(req.query));
249-
}
250-
251333
mongoClient.connect(connectionString, function(err,db) {
252334

253335
if(err) {
254336
callback(err,null);
255-
}
337+
}
256338

257-
db.collection(collName).find({"Sub Number" : subject_num}).toArray(function(err, results) {
339+
db.collection('channels').find(query).toArray(function(err, results) {
258340
if(err) {
259-
res.send("Sorry, this result cannot be found!");
341+
res.send("Sorry, you done messed up!");
260342
res.end();
261343
}
262344
else {
263345
res.setHeader('Content-Type', 'application/json');
264346
res.send(results);
265347
res.end();
266-
db.close();
267348
}
268349
}); // end of collection connection
269350
}); // end of db connection

0 commit comments

Comments
 (0)