- Timestamp:
- 03/11/14 22:58:35 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/src/server/app.js
r501 r507 407 407 }); 408 408 } 409 function collectFields(field,obj) { 410 if ( _.isArray(obj) ) { 411 return _.reduce(obj,function(subcodes,val,idx){ 412 return subcodes.concat(collectFields(field,val)); 413 },[]); 414 } else if ( _.isObject(obj) ) { 415 return _.reduce(obj,function(subcodes,val,key){ 416 if ( key === field ) { 417 subcodes.push(val); 418 } 419 return subcodes.concat(collectFields(field,val)); 420 },[]); 421 } else { 422 return []; 423 } 424 } 425 function areSubcodesUnique(content) { 426 var subcodes = collectFields('subcode',content); 427 return _.uniq(subcodes).length === subcodes.length; 428 } 409 429 app.get('/api/questions', 410 430 ensureAuthenticated, … … 435 455 if ( others.length > 0 ) { 436 456 return new HTTPResult(403,{error:"Other question with this code already exists."}); 457 } else if ( !areSubcodesUnique(doc.content) ) { 458 return new HTTPResult(400,{error:"Subcodes are not unqiue."}); 437 459 } else { 438 460 return postDocument('Question',doc); … … 458 480 if ( others.length > 0 && _.some(others,function(other){ return other._id !== id; }) ) { 459 481 return new HTTPResult(403,{error:"Other question with this code already exists."}); 482 } else if ( !areSubcodesUnique(doc.content) ) { 483 return new HTTPResult(400,{error:"Subcodes are not unqiue."}); 460 484 } else { 461 485 return putDocument(id,rev,'Question',doc);
Note: See TracChangeset
for help on using the changeset viewer.