Changeset 507 for Dev


Ignore:
Timestamp:
03/11/14 22:58:35 (11 years ago)
Author:
hendrikvanantwerpen
Message:

Check that subcodes are unique.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/app.js

    r501 r507  
    407407        });
    408408    }
     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    }
    409429    app.get('/api/questions',
    410430        ensureAuthenticated,
     
    435455                    if ( others.length > 0 ) {
    436456                        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."});
    437459                    } else {
    438460                        return postDocument('Question',doc);
     
    458480                    if ( others.length > 0 && _.some(others,function(other){ return other._id !== id; })  ) {
    459481                        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."});
    460484                    } else {
    461485                        return putDocument(id,rev,'Question',doc);
Note: See TracChangeset for help on using the changeset viewer.