Commit 1e62f218 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Sorting of recent comments by votes.

parent 4523e8fd
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -92,11 +92,11 @@ app.post('/auth/logout', validator.doLogout, function(req, res) {
// Requests for Comments

// Returns n most recent comments.
// Takes two parameters: offset and limit.
app.get('/auth/:sdk/:version/comments_recent', function(req, res) {
    var query = {
        offset: parseInt(req.query.offset, 10),
        limit: parseInt(req.query.limit, 10)
        limit: parseInt(req.query.limit, 10),
        orderBy: req.query.sortByScore ? "vote" : "created_at"
    };
    new Request(req).getRecentComments(query, function(comments) {
        res.json(comments);
+5 −1
Original line number Diff line number Diff line
@@ -105,17 +105,21 @@ Comments.prototype = {
     * @param {Object} opts Options for the query:
     * @param {Number} [opts.limit=100] Number of rows to return.
     * @param {Number} [opts.offset=0] The starting index.
     * @param {Number} [opts.orderBy="created_at"] By which column to sort the results.
     * Two possible options here: "created_at" and "vote".
     *
     * @param {Function} callback Called with the result.
     * @param {Error} callback.err The error object.
     * @param {Object[]} callback.comments An array of comment rows.
     */
    findRecent: function(opts, callback) {
        opts.orderBy = opts.orderBy || "created_at";

        var sql = [
            'SELECT ', this.fields.join(", "),
            'FROM', this.view,
            'WHERE domain = ?',
            'ORDER BY created_at DESC',
            'ORDER BY '+opts.orderBy+' DESC',
            'LIMIT ? OFFSET ?'
        ];

+7 −0
Original line number Diff line number Diff line
@@ -144,6 +144,13 @@ describe("Comments", function() {
        });
    });

    it("#findRecent with orderBy:vote sorts to highest voted comment to top", function(done) {
        comments.findRecent({orderBy: "vote"}, function(err, rows) {
            expect(rows[0].vote).toEqual(4);
            done();
        });
    });

    it("#count gets total number of comments in current domain", function(done) {
        comments.count({}, function(err, cnt) {
            expect(cnt).toEqual(24);