source: Dev/trunk/src/server/api/responses.js @ 525

Last change on this file since 525 was 525, checked in by hendrikvanantwerpen, 11 years ago
  • Allow empty subcodes.
  • Use HTTPResult exclusively on server (no more q).
  • Set readonly & disabled on ourselves as well in _ComplexValueMixin
  • Split server into several modules.
  • Check codes on the variable level, not question level.
  • We can add modules in design documents now.
File size: 2.3 KB
Line 
1var express = require("express")
2  , HTTPResult = require("../util/http-result")
3  , etags = require("../util/etags")
4  , cryptoken = require("../util/crypto-token")
5  , _ = require('underscore')
6  ;
7
8module.exports = function(couch,schema) {
9    var exports = {};
10
11    var util = require('./util')(couch,schema);
12    var app = exports.app = express();
13
14    var getResponsesBySurveyRunId = exports.getResponsesBySurveyRunId = function(surveyRunId) {
15        var url = '_design/responses/_view/by_surveyrun?key='+JSON.stringify(surveyRunId);
16        return couch.get(url)
17        .handle({
18            200: util.handleRowValues,
19            default: util.handleUnknownResponse
20        });
21    };
22
23    app.get('/',
24        util.ensureMIME(util.JSON_MIME),
25        function(req,res){
26            var hr;
27            if ( 'surveyRunId' in req.query ) {
28                hr = getResponsesBySurveyRunId(req.query.surveyRunId);
29            } else {
30                hr = util.getDocumentsOfType('Response');
31            }
32            hr.handle(res.send.bind(res));
33        });
34    app.get('/:id',
35        util.ensureMIME(util.JSON_MIME),
36        util.makeDocGet_id('Response'));
37    app.post('/api/responses',
38        util.ensureMIME(util.JSON_MIME),
39        function (req,res) {
40            var doc = req.body;
41            cryptoken()
42            .then(function(token) {
43                doc.secret = token;
44                return util.postDocument('Response',doc);
45            })
46            .handle(res.send.bind(res));
47        });
48    app.put('/:id',
49        util.ensureMIME(util.JSON_MIME),
50        function (req,res) {
51            var id = req.params.id;
52            var doc = req.body;
53            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
54            var hr;
55            if ( typeof doc.secret === 'undefined' ) {
56                hr = util.randomToken()
57                .handle({
58                    201: function(token) {
59                        doc.secret = token;
60                        return util.putDocument(id,rev,'Response',doc);
61                    }
62                });
63            } else {
64                hr = util.putDocument(id,rev,'Response',doc);
65            }
66            hr.handle(res.send.bind(res));
67        });
68    app.delete('/:id',
69        util.ensureMIME(util.JSON_MIME),
70        util.makeDocDel_id('Response'));
71    return exports;
72};
Note: See TracBrowser for help on using the repository browser.