Ignore:
Timestamp:
08/06/13 01:48:17 (12 years ago)
Author:
hendrikvanantwerpen
Message:

Quick hack to allow responding despite not being authenticated. Something like tokes need to be added.

File:
1 edited

Legend:

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

    r475 r477  
    4848
    4949    // cookies and session
    50     app.use('/api/login',express.bodyParser());
     50    app.use('/api',express.bodyParser());
    5151    app.use(express.cookieParser());
    5252    app.use(express.session({ secret: "quasi experimental design" }));
     
    9999        });
    100100
     101    // expose everything needed to fill in responses
     102    // FIXME : this needs more security, now you can guess ids
     103    //         and get actual survey and response data!
     104    app.get(
     105        '/live',
     106        function(req,res){
     107            res.redirect('/response.html#!/55704d6a84194c2d7ba9bec93c000e75');
     108        });
     109    app.get(
     110        '/api/surveyRuns/:id',
     111        function(req,res){
     112            var id = req.params.id;
     113            couch.get(id).then(function(result){
     114                if ( result.type === 'SurveyRun') {
     115                    res.send(200, result);
     116                } else {
     117                    res.send(404, {error: "Cannot find survey run "+id});
     118                }
     119            }, function(error){
     120                res.send(404, {error: "Cannot find survey run "+id});
     121            });
     122        });
     123    app.get(
     124        '/api/responses/:id',
     125        function(req,res){
     126            var id = req.params.id;
     127            couch.get(id).then(function(result){
     128                if ( result.type === 'Response') {
     129                    res.send(200, result);
     130                } else {
     131                    res.send(404, {error: "Cannot find response "+id});
     132                }
     133            }, function(error){
     134                res.send(404, {error: "Cannot find response "+id});
     135            });
     136        });
     137    app.post(
     138        '/api/responses',
     139        function(req,res){
     140            console.log(req.body);
     141            couch.post(req.body).then(function(result){
     142                res.send(200, result);
     143            }, function(error){
     144                res.send(500, error);
     145            });
     146        });
     147    app.put(
     148        '/api/responses/:id',
     149        function(req,res){
     150            var id = req.params.id;
     151            couch.put(id,req.body).then(function(result){
     152                res.send(200, result);
     153            }, function(error){
     154                res.send(500, error);
     155            });
     156        });
     157   
    101158    // data is proxied to couch
    102159    app.use('/api/data', ensureAuthenticated);
     
    135192function responseToVariables(response) {
    136193    var result = flattenObject(response.answers);
    137     result.respondent = response.id;
    138194    return result;
    139195}
     
    143199    (function agg(val,key,res){
    144200        if ( _.isObject(val) ) {
    145             _.forEach(val, function(v,k){
    146                 agg(v,(key ? key+'.' : '')+k,res);
    147             });
     201            var keys = _.keys(val);
     202            // FIXME : dirty hack for questions with only one input
     203            if ( keys.length === 1 ) {
     204                agg(val[keys[0]],key,res);
     205            } else {
     206                _.forEach(val, function(v,k){
     207                    agg(v,(key ? key+'.' : '')+k,res);
     208                });
     209            }
    148210        } else if ( _.isArray(val) ) {
    149             _.forEach(val, function(v,i){
    150                 agg(v,(key ? key+'.' : '')+i,res);
    151             });
     211            // FIXME : dirty hack for questions with only one input
     212            if ( val.length === 1 ) {
     213                agg(val[0],key,res);
     214            } else {
     215                _.forEach(val, function(v,i){
     216                    agg(v,(key ? key+'.' : '')+i,res);
     217                });
     218            }
    152219        } else {
    153220            res[key] = val;
Note: See TracChangeset for help on using the changeset viewer.