source: Dev/trunk/src/server/api/topics.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: 1.6 KB
Line 
1var express = require("express")
2  , HTTPResult = require("../util/http-result")
3  , _ = require("underscore")
4  ;
5
6module.exports = function(couch,schema) {
7    var exports = {};
8   
9    var util = require('./util')(couch,schema);
10    var app = exports.app = express();
11
12    var getTopics = exports.getTopics = function(sub) {
13        var url = '_design/questions/_view/'+(sub||'all')+'_topics';
14        return couch.get(url,{query:{reduce:true,group:true}})
15        .handle({
16            200: function(result) {
17                return _.map(result.rows, function(item) { return {name:item.key, count:item.value}; });
18            },
19            default: util.handleUnknownResponse
20        });
21    };
22    var getTopicsWithCategory = exports.getTopicsWithCategory = function(category,sub) {
23        var url = '_design/questions/_view/'+(sub||'all');
24        return couch.get(url,{query:{reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]}})
25        .handle({
26            200: function(result) {
27                return _.map(result.rows, function(item) { return { name:item.key[1], count:item.value }; });
28            },
29            default: util.handleUnknownResponse
30        });
31    };
32
33    app.get('/',
34        util.ensureMIME(util.JSON_MIME),
35        function(req,res) {
36            var sub = 'published' in req.query && 'published';
37            var hr;
38            if ( 'category' in req.query ) {
39                hr = getTopicsWithCategory(req.query.category,sub);
40            } else {
41                hr = getTopics(sub);
42            }
43            hr.handle(res.send.bind(res));
44        });
45
46    return exports;
47};
Note: See TracBrowser for help on using the repository browser.