1
1
var express = require ( 'express' ) ;
2
2
var app = express ( ) ;
3
3
var mongoClient = require ( 'mongodb' ) . MongoClient ;
4
- var port = 8080 ;
4
+ var port = 9876 ;
5
5
var http = require ( 'http' ) ;
6
6
var host = "127.0.0.1" ;
7
7
var mongoPort = "27017" ;
@@ -12,9 +12,8 @@ var multer = require('multer');
12
12
13
13
app . use ( bodyParser . json ( ) ) ; // for parsing application/json
14
14
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
16
15
17
- function parseQueryParams ( urlQueryParams , callback ) { // callback
16
+ function parseQueryParams ( urlQueryParams , callback ) { // parses query parameters
18
17
var mongoQuery = { } ;
19
18
20
19
console . log ( JSON . stringify ( urlQueryParams ) ) ;
@@ -71,46 +70,94 @@ function parseQueryParams(urlQueryParams, callback) { // callback
71
70
callback ( null , mongoQuery ) ;
72
71
}
73
72
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 ;
79
79
80
- parseQueryParams ( req . query , function ( err , params ) {
81
- query = params ;
82
- console . log ( params ) ;
80
+ console . log ( rowQuery ) ;
83
81
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
+ } ) ;
84
94
} ) ;
85
95
86
- console . log ( "yay, this is the subject information API!" ) ;
96
+ } ) ;
87
97
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
+
88
112
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 ( ) ;
99
117
}
100
118
else {
101
- res . setHeader ( 'Content-Type' , 'application/json' ) ;
102
- res . send ( results ) ;
119
+ res . sendStatus ( 200 ) ;
103
120
res . end ( ) ;
104
- db . close ( ) ;
105
121
}
106
- } ) ; // end of collection connection
107
- } ) ; // end of db connection
122
+ db . close ( ) ;
123
+ } ) ;
124
+ } ) ;
125
+
126
+ } ) ;
108
127
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
+
109
157
} ) ;
110
158
111
159
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
114
161
var collName = req . params . coll_name ;
115
162
var query ;
116
163
@@ -143,9 +190,8 @@ app.get('/collection/:coll_name', function(req, res) {
143
190
144
191
} ) ;
145
192
146
-
147
- // check to see if a field exists in a collection, if so, return it
148
193
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
149
195
var collName = req . params . coll_name ;
150
196
var fieldName = req . params . field_name ;
151
197
var query = { } ;
@@ -178,7 +224,41 @@ app.get('/collection/:coll_name/exists/:field_name', function(req, res) {
178
224
179
225
} ) ;
180
226
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
+
181
260
app . post ( '/lims/:project_name' , function ( req , res ) {
261
+ // this api accepts JSON and inserts it into the db with corresponding project name
182
262
var json = req . body ;
183
263
var projName = req . params . project_name ;
184
264
@@ -194,7 +274,7 @@ app.post('/lims/:project_name', function(req, res) {
194
274
195
275
db . collection ( 'channels' ) . insert ( json , function ( err , results ) {
196
276
if ( err ) {
197
- res . send ( "Sorry, you done messed up !" ) ;
277
+ res . send ( "Sorry, your data is in the wrong format !" ) ;
198
278
res . end ( ) ;
199
279
}
200
280
else {
@@ -208,62 +288,63 @@ app.post('/lims/:project_name', function(req, res) {
208
288
209
289
} ) ;
210
290
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" ;
213
297
console . log ( "woo, this is the post API!" ) ;
214
-
215
- var query = { } ;
216
- query . project_name = projName ;
298
+
299
+ console . log ( json ) ;
217
300
218
301
mongoClient . connect ( connectionString , function ( err , db ) {
219
302
220
303
if ( err ) {
221
304
callback ( err , null ) ;
222
305
}
223
306
224
- db . collection ( 'channels' ) . find ( query ) . toArray ( function ( err , results ) {
307
+ db . collection ( collName ) . insert ( json , function ( err , results ) {
225
308
if ( err ) {
226
- res . send ( "Sorry, you done messed up !" ) ;
309
+ res . send ( "Sorry, your stuff isn't in the right format !" ) ;
227
310
res . end ( ) ;
228
311
}
229
312
else {
230
313
res . setHeader ( 'Content-Type' , 'application/json' ) ;
231
314
res . send ( results ) ;
232
315
res . end ( ) ;
316
+ db . close ( ) ;
233
317
}
234
318
} ) ; // end of collection connection
235
319
} ) ; // end of db connection
236
320
237
321
} ) ;
238
322
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 ;
246
332
247
- if ( req . query ) {
248
- console . log ( "query parameters are: " + JSON . stringify ( req . query ) ) ;
249
- }
250
-
251
333
mongoClient . connect ( connectionString , function ( err , db ) {
252
334
253
335
if ( err ) {
254
336
callback ( err , null ) ;
255
- }
337
+ }
256
338
257
- db . collection ( collName ) . find ( { "Sub Number" : subject_num } ) . toArray ( function ( err , results ) {
339
+ db . collection ( 'channels' ) . find ( query ) . toArray ( function ( err , results ) {
258
340
if ( err ) {
259
- res . send ( "Sorry, this result cannot be found !" ) ;
341
+ res . send ( "Sorry, you done messed up !" ) ;
260
342
res . end ( ) ;
261
343
}
262
344
else {
263
345
res . setHeader ( 'Content-Type' , 'application/json' ) ;
264
346
res . send ( results ) ;
265
347
res . end ( ) ;
266
- db . close ( ) ;
267
348
}
268
349
} ) ; // end of collection connection
269
350
} ) ; // end of db connection
0 commit comments