Commit 809b413e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Create Comments class to handle comments queries.

Also add full_visible_comments view to simplify the queries.

Finally added a small test script to check if it's actually working.
parent 6f16f244
Loading
Loading
Loading
Loading

comments/comments.js

0 → 100644
+98 −0
Original line number Diff line number Diff line

module.exports = (function(){
    /**
     * Represents a Comments table.
     *
     * @constructor
     * Initializes Comments with a database connection and a target domain.
     *
     * @param {mysql.Connection} db MySQL connection object.
     *
     * @param {String} domain The comments domain within which to work.
     * For example by passing "touch-2" the #find method will only find
     * comments within touch-2 domain.
     */
    function Comments(db, domain) {
        this.db = db;
        this.domain = domain;
    }

    Comments.prototype = {
        /**
         * Finds list of all comments for a particular target.
         * Excludes deleted comments.
         *
         * @param {Object} target The target:
         * @param {String} target.type One of: class, guide, video.
         * @param {String} target.cls The name of the class, guide or video.
         * @param {String} target.member The name of class member or empty string.
         *
         * @param {Function} callback Called with the result.
         * @param {Object[]} callback.comments An array of comment rows.
         */
        find: function(target, callback) {
            var sql = [
                'SELECT *',
                'FROM full_visible_comments',
                'WHERE domain = ? AND type = ? AND cls = ? AND member = ?',
                'ORDER BY created_at'
            ];

            this.query(sql, [this.domain, target.type, target.cls, target.member], callback);
        },

        /**
         * Returns all comments sorted in reverse chronological order.
         * Excludes deleted comments.
         *
         * @param {Object} opts Options for the query:
         * @param {Number} opts.limit Number of rows to return.
         * @param {Number} opts.offset The starting index.
         *
         * @param {Function} callback Called with the result.
         * @param {Object[]} callback.comments An array of comment rows.
         */
        findRecent: function(opts, callback) {
            var sql = [
                'SELECT *',
                'FROM full_visible_comments',
                'WHERE domain = ?',
                'ORDER BY created_at DESC',
                'LIMIT ? OFFSET ?'
            ];

            this.query(sql, [this.domain, opts.limit, opts.offset], callback);
        },

        /**
         * Counts number of comments in the current domain.
         * Excludes deleted comments.
         *
         * @param {Object} opts Reserved for future.
         *
         * @param {Function} callback Called with the result.
         * @param {Number} callback.count The number of comments found.
         */
        count: function(opts, callback) {
            var sql = [
                'SELECT COUNT(*) as count',
                'FROM full_visible_comments',
                'WHERE domain = ?'
            ];

            this.query(sql, [this.domain], function(rows) {
                callback(rows[0].count);
            });
        },

        query: function(sqlLines, params, callback) {
            this.db.query(sqlLines.join("\n"), params, function(err, rows) {
                if (err) throw err;

                callback(rows);
            });
        }
    };

    return Comments;
})();
+16 −0
Original line number Diff line number Diff line
var Comments = require("./comments");
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: '',
    password: '',
    database: 'comments'
});

var comments = new Comments(connection, "touch-2");

comments.count({}, function(cnt) {
    console.log(cnt + " comments");
    process.exit();
});
+18 −0
Original line number Diff line number Diff line
@@ -80,3 +80,21 @@ CREATE VIEW voted_comments AS SELECT
FROM visible_comments c LEFT JOIN votes v ON c.id = v.comment_id
GROUP BY c.id;

-- comments table joined with users and targets for easier quering
CREATE VIEW full_visible_comments AS SELECT
    c.id,
    c.content,
    c.content_html,
    c.created_at,
    users.username,
    users.external_id,
    users.email,
    users.moderator,
    targets.domain,
    targets.type,
    targets.cls,
    targets.member
FROM visible_comments AS c
    LEFT JOIN users ON c.user_id = users.id
    LEFT JOIN targets ON c.target_id = targets.id;