source: Dev/trunk/src/server/api/topics.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: 1.7 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            '-1': _.identity,
17            200: function(result) {
18                return _.map(result.rows, function(item) { return {name:item.key, count:item.value}; });
19            },
20            default: util.handleUnknownResponse
21        });
22    };
23    var getTopicsWithCategory = exports.getTopicsWithCategory = function(category,sub) {
24        var url = '_design/questions/_view/'+(sub||'all');
25        return couch.get(url,{query:{reduce:true,group:true,group_level:2,startkey:[category],endkey:[category,{}]}})
26        .handle({
27            '-1': _.identity,
28            200: function(result) {
29                return _.map(result.rows, function(item) { return { name:item.key[1], count:item.value }; });
30            },
31            default: util.handleUnknownResponse
32        });
33    };
34
35    app.get('/',
36        util.ensureMIME(util.JSON_MIME),
37        function(req,res) {
38            var sub = 'published' in req.query && 'published';
39            var hr;
40            if ( 'category' in req.query ) {
41                hr = getTopicsWithCategory(req.query.category,sub);
42            } else {
43                hr = getTopics(sub);
44            }
45            hr.handle({'-1': util.handleException})
46              .handle(res.send.bind(res));
47        });
48
49    return exports;
50};
Note: See TracBrowser for help on using the repository browser.