source: Dev/trunk/src/server/api/surveys.js @ 527

Last change on this file since 527 was 527, checked in by hendrikvanantwerpen, 11 years ago
  • Dropped request module for a simple one we wrote ourselves using the native Node modules. Hopefully this one will not suffer from the problem that sometimes empty bodies are returned even when the statuscode and content-length of the request are ok & present.
  • Handle exceptions better in HTTPResult chain, they were hoisted in unknown responses before. Should we not include them in default processing, because they are special?
File size: 3.3 KB
Line 
1var express = require("express")
2  , _ = require("underscore")
3  , HTTPResult = require("../util/http-result")
4  , etags = require("../util/etags")
5  ;
6
7module.exports = function(couch,schema) {
8    var exports = {};
9    var util = require('./util')(couch,schema);
10    var app = exports.app = express();
11
12    app.get('/',
13        util.ensureMIME(util.JSON_MIME),
14        function(req,res) {
15            var url;
16            if ( 'drafts' in req.query ) {
17                url = '_design/surveys/_view/drafts';
18            } else if ( 'published' in req.query ) {
19                url = '_design/surveys/_view/published';
20            } else {
21                url = '_design/default/_view/by_type?key='+JSON.stringify('Survey');
22            }
23            couch.get(url)
24            .handle({
25                '-1': _.identity,
26                200: function(result) {
27                    return _.map(result.rows, function(item) {
28                        return item.value;
29                    });
30                },
31                default: util.handleUnknownResponse
32            })
33            .handle({'-1': util.handleException})
34            .handle(res.send.bind(res));
35        });
36    app.post('/',
37        util.ensureMIME(util.JSON_MIME),
38        function(req,res) {
39            var doc = req.body;
40            var hr;
41            if ( !util.areDocsUnique(doc.questions) ) {
42                hr = new HTTPResult(400,{error:"Survey contains duplicate questions."});
43            } else if ( util.isDocPublished(doc) &&
44                        !util.areDocsPublished(doc.questions) ) {
45                hr = new HTTPResult(400,{error:"Cannot publish Survey with unpublished questions."});
46            } else {
47                hr = util.postDocument('Survey',doc);
48            }
49            hr.handle({'-1': util.handleException})
50              .handle(res.send.bind(res));
51        });
52    app.get('/:id',
53        util.ensureMIME(util.JSON_MIME),
54        util.makeDocGet_id('Survey'));
55    app.put('/:id',
56        util.ensureMIME(util.JSON_MIME),
57        function(req,res) {
58            var id = req.params.id;
59            var doc = req.body;
60            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
61            if ( !util.areDocsUnique(doc.questions) ) {
62                new HTTPResult(400,{error:"Survey contains duplicate questions."})
63                .handle(res.send.bind(res));
64            } else if ( util.isDocPublished(doc) &&
65                        !util.areDocsPublished(doc.questions) ) {
66                new HTTPResult(400,{error:"Cannot publish Survey with unpublished questions."})
67                .handle(res.send.bind(res));
68            } else {
69                util.putDocument(id,rev,'Survey',doc)
70                .handle({
71                    '-1': function(result) {
72                        util.handleException(result)
73                        .handle(res.send.bind(res));
74                    },
75                    201: function(doc) {
76                        res.set({
77                            'ETag': etags.format([doc._rev])
78                        }).send(201, doc);
79                    },
80                    default: res.send.bind(res)
81                });
82            }
83        });
84    app.delete('/:id',
85        util.ensureMIME(util.JSON_MIME),
86        util.makeDocDel_id('Survey'));
87    return exports;
88};
Note: See TracBrowser for help on using the repository browser.