Loading comments/comments.js +21 −6 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ Comments.prototype = { * * @param {Number} id The ID of the comment to find. * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object} callback.comment The comment found or undefined. */ getById: function(id, callback) { Loading @@ -47,6 +48,7 @@ Comments.prototype = { * @param {String} target.member The name of class member or empty string. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object[]} callback.comments An array of comment rows. */ find: function(target, callback) { Loading @@ -69,6 +71,7 @@ Comments.prototype = { * @param {Number} [opts.offset=0] The starting index. * * @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) { Loading @@ -90,6 +93,7 @@ Comments.prototype = { * @param {Object} opts Reserved for future. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Number} callback.count The number of comments found. */ count: function(opts, callback) { Loading @@ -99,8 +103,8 @@ Comments.prototype = { 'WHERE domain = ?' ]; this.db.queryOne(sql, [this.domain], function(row) { callback(+row.count); this.db.queryOne(sql, [this.domain], function(err, row) { callback(err, +row.count); }); }, Loading @@ -109,6 +113,7 @@ Comments.prototype = { * domain. Excludes deleted comments. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object} callback.counts Map of targets to counts: * * { Loading @@ -129,13 +134,13 @@ Comments.prototype = { 'GROUP BY target_id' ]; this.db.query(sql, [this.domain], function(rows) { this.db.query(sql, [this.domain], function(err, rows) { var map = {}; rows.forEach(function(r) { var id = [r.type, r.cls, r.member].join("__"); map[id] = +r.count; }); callback(map); callback(err, map); }); }, Loading @@ -151,10 +156,15 @@ Comments.prototype = { * @param {String} comment.cls Class name of target. * @param {String} comment.member Member name of target. * @param {Function} callback * @param {Error} callback.err The error object. * @param {Function} callback.id The ID of newly inserted comment. */ add: function(comment, callback) { this.targets.ensure(comment, function(target_id) { this.targets.ensure(comment, function(err, target_id) { if (err) { callback(err); return; } this.db.insert('comments', { target_id: target_id, user_id: comment.user_id, Loading @@ -173,6 +183,7 @@ Comments.prototype = { * @param {Number} comment.user_id ID of the user doing the update. * @param {String} comment.content New text for the comment. * @param {String} comment.content_html New formatted text for the comment. * @param {Error} callback.err The error object. * @param {Function} callback Called when done. */ update: function(comment, callback) { Loading @@ -181,7 +192,11 @@ Comments.prototype = { content: comment.content, content_html: comment.content_html }; this.db.update("comments", data, function() { this.db.update("comments", data, function(err) { if (err) { callback(err); return; } this.db.insert("updates", { comment_id: comment.id, user_id: comment.user_id, Loading comments/comments.spec.js +21 −21 Original line number Diff line number Diff line Loading @@ -20,77 +20,77 @@ describe("Comments", function() { }); it("#getById returns comment with given ID", function(done) { comments.getById(7, function(com) { comments.getById(7, function(err, com) { expect(com.id).toEqual(7); done(); }); }); it("#getById returns undefined when no comment with such ID", function(done) { comments.getById(123456, function(com) { comments.getById(123456, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#getById returns undefined when ID exists in other domain.", function(done) { comments.getById(30, function(com) { comments.getById(30, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#getById returns undefined when comment with ID is deleted", function(done) { comments.getById(6, function(com) { comments.getById(6, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#find returns all undeleted comments for a target", function(done) { comments.find({type: "class", cls: "Ext", member: ""}, function(rows) { comments.find({type: "class", cls: "Ext", member: ""}, function(err, rows) { expect(rows.length).toEqual(5); done(); }); }); it("#find returns empty array when target not found", function(done) { comments.find({type: "class", cls: "Foo", member: "bar"}, function(rows) { comments.find({type: "class", cls: "Foo", member: "bar"}, function(err, rows) { expect(rows.length).toEqual(0); done(); }); }); it("#find returns empty array when target not in current domain", function(done) { comments.find({type: "guide", cls: "forms", member: ""}, function(rows) { comments.find({type: "guide", cls: "forms", member: ""}, function(err, rows) { expect(rows.length).toEqual(0); done(); }); }); it("#findRecent returns n recent comments", function(done) { comments.findRecent({limit: 10, offset: 0}, function(rows) { comments.findRecent({limit: 10, offset: 0}, function(err, rows) { expect(rows.length).toEqual(10); done(); }); }); it("#findRecent without offset option defaults to offset:0", function(done) { comments.findRecent({limit: 10}, function(rows) { comments.findRecent({limit: 10}, function(err, rows) { expect(rows.length).toEqual(10); done(); }); }); it("#findRecent without limit option defaults to limit:100 or max nr of comments", function(done) { comments.findRecent({}, function(rows) { comments.findRecent({}, function(err, rows) { expect(rows.length).toEqual(24); done(); }); }); it("#count gets total number of comments in current domain", function(done) { comments.count({}, function(cnt) { comments.count({}, function(err, cnt) { expect(cnt).toEqual(24); done(); }); Loading @@ -102,7 +102,7 @@ describe("Comments", function() { }); it("#count gets total number of comments in the other domain", function(done) { comments.count({}, function(cnt) { comments.count({}, function(err, cnt) { expect(cnt).toEqual(4); done(); }); Loading @@ -110,7 +110,7 @@ describe("Comments", function() { }); it("#countPerTarget gets number of comments for each target", function(done) { comments.countsPerTarget(function(counts) { comments.countsPerTarget(function(err, counts) { expect(counts["class__Ext__"]).toEqual(5); done(); }); Loading @@ -127,8 +127,8 @@ describe("Comments", function() { cls: "Ext", member: "method-getBody" }; comments.add(com, function(id) { comments.getById(id, function(newCom) { comments.add(com, function(err, id) { comments.getById(id, function(err, newCom) { expect(newCom.id).toEqual(id); done(); }); Loading @@ -146,8 +146,8 @@ describe("Comments", function() { cls: "Blah", member: "method-foo" }; comments.add(com, function(id) { comments.getById(id, function(newCom) { comments.add(com, function(err, id) { comments.getById(id, function(err, newCom) { expect(newCom.cls).toEqual("Blah"); done(); }); Loading @@ -163,8 +163,8 @@ describe("Comments", function() { content: "New content.", content_html: "<p>New content.</p>" }; comments.update(com, function() { comments.getById(com.id, function(newCom) { comments.update(com, function(err) { comments.getById(com.id, function(err, newCom) { expect(newCom.content).toEqual("New content."); done(); }); Loading @@ -179,8 +179,8 @@ describe("Comments", function() { content: "New content.", content_html: "<p>New content.</p>" }; comments.update(com, function() { comments.getById(com.id, function(newCom) { comments.update(com, function(err) { comments.getById(com.id, function(err, newCom) { expect(newCom.user_id).toEqual(4); done(); }); Loading comments/db_facade.js +8 −9 Original line number Diff line number Diff line Loading @@ -26,25 +26,22 @@ DbFacade.prototype = { * whitespace. * @param {Mixed[]} params Parameters for the query * @param {Function} callback Called after query finishes:. * @param {Error} callback.err The error object on failure or null on success. * @param {Object[]/Object} callback.result The result of callback. * In case of SELECT it's an array of rows, * In case of INSERT it's object with insertId property. */ query: function(sql, params, callback) { sql = (typeof sql === "string") ? sql : sql.join("\n"); this.connection.query(sql, params, function(err, result) { if (err) throw err; callback(result); }); this.connection.query(sql, params, callback); }, /** * Exactly like #query, except that the result is just one row. */ queryOne: function(sql, params, callback) { this.query(sql, params, function(rows) { callback(rows[0]); this.query(sql, params, function(err, rows) { callback(err, rows[0]); }); }, Loading @@ -54,11 +51,12 @@ DbFacade.prototype = { * @param {String} table Name of the table. * @param {Object} fields The row to insert. * @param {Function} callback Called when query finishes. * @param {Error} callback.err The error object. * @param {Number} callback.insertId ID of the inserted row. */ insert: function(table, fields, callback) { this.query("INSERT INTO "+table+" SET ?", [fields], function(result) { callback(result.insertId); this.query("INSERT INTO "+table+" SET ?", [fields], function(err, result) { callback(err, result.insertId); }); }, Loading @@ -69,6 +67,7 @@ DbFacade.prototype = { * @param {Object} fields The row to update. This must contain * an `id` field that's used to decide which row to update. * @param {Function} callback Called when query finishes. * @param {Error} callback.err The error object. */ update: function(table, fields, callback) { var id = fields.id; Loading comments/targets.js +3 −2 Original line number Diff line number Diff line Loading @@ -21,12 +21,13 @@ Targets.prototype = { * @param {String} target.cls * @param {String} target.member * @param {Function} callback * @param {Error} callback.err * @param {Number} callback.id The ID of the target */ ensure: function(target, callback) { this.get(target, function(targetFound) { this.get(target, function(err, targetFound) { if (targetFound) { callback(targetFound.id); callback(err, targetFound.id); } else { this.add(target, callback); Loading Loading
comments/comments.js +21 −6 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ Comments.prototype = { * * @param {Number} id The ID of the comment to find. * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object} callback.comment The comment found or undefined. */ getById: function(id, callback) { Loading @@ -47,6 +48,7 @@ Comments.prototype = { * @param {String} target.member The name of class member or empty string. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object[]} callback.comments An array of comment rows. */ find: function(target, callback) { Loading @@ -69,6 +71,7 @@ Comments.prototype = { * @param {Number} [opts.offset=0] The starting index. * * @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) { Loading @@ -90,6 +93,7 @@ Comments.prototype = { * @param {Object} opts Reserved for future. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Number} callback.count The number of comments found. */ count: function(opts, callback) { Loading @@ -99,8 +103,8 @@ Comments.prototype = { 'WHERE domain = ?' ]; this.db.queryOne(sql, [this.domain], function(row) { callback(+row.count); this.db.queryOne(sql, [this.domain], function(err, row) { callback(err, +row.count); }); }, Loading @@ -109,6 +113,7 @@ Comments.prototype = { * domain. Excludes deleted comments. * * @param {Function} callback Called with the result. * @param {Error} callback.err The error object. * @param {Object} callback.counts Map of targets to counts: * * { Loading @@ -129,13 +134,13 @@ Comments.prototype = { 'GROUP BY target_id' ]; this.db.query(sql, [this.domain], function(rows) { this.db.query(sql, [this.domain], function(err, rows) { var map = {}; rows.forEach(function(r) { var id = [r.type, r.cls, r.member].join("__"); map[id] = +r.count; }); callback(map); callback(err, map); }); }, Loading @@ -151,10 +156,15 @@ Comments.prototype = { * @param {String} comment.cls Class name of target. * @param {String} comment.member Member name of target. * @param {Function} callback * @param {Error} callback.err The error object. * @param {Function} callback.id The ID of newly inserted comment. */ add: function(comment, callback) { this.targets.ensure(comment, function(target_id) { this.targets.ensure(comment, function(err, target_id) { if (err) { callback(err); return; } this.db.insert('comments', { target_id: target_id, user_id: comment.user_id, Loading @@ -173,6 +183,7 @@ Comments.prototype = { * @param {Number} comment.user_id ID of the user doing the update. * @param {String} comment.content New text for the comment. * @param {String} comment.content_html New formatted text for the comment. * @param {Error} callback.err The error object. * @param {Function} callback Called when done. */ update: function(comment, callback) { Loading @@ -181,7 +192,11 @@ Comments.prototype = { content: comment.content, content_html: comment.content_html }; this.db.update("comments", data, function() { this.db.update("comments", data, function(err) { if (err) { callback(err); return; } this.db.insert("updates", { comment_id: comment.id, user_id: comment.user_id, Loading
comments/comments.spec.js +21 −21 Original line number Diff line number Diff line Loading @@ -20,77 +20,77 @@ describe("Comments", function() { }); it("#getById returns comment with given ID", function(done) { comments.getById(7, function(com) { comments.getById(7, function(err, com) { expect(com.id).toEqual(7); done(); }); }); it("#getById returns undefined when no comment with such ID", function(done) { comments.getById(123456, function(com) { comments.getById(123456, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#getById returns undefined when ID exists in other domain.", function(done) { comments.getById(30, function(com) { comments.getById(30, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#getById returns undefined when comment with ID is deleted", function(done) { comments.getById(6, function(com) { comments.getById(6, function(err, com) { expect(com).toEqual(undefined); done(); }); }); it("#find returns all undeleted comments for a target", function(done) { comments.find({type: "class", cls: "Ext", member: ""}, function(rows) { comments.find({type: "class", cls: "Ext", member: ""}, function(err, rows) { expect(rows.length).toEqual(5); done(); }); }); it("#find returns empty array when target not found", function(done) { comments.find({type: "class", cls: "Foo", member: "bar"}, function(rows) { comments.find({type: "class", cls: "Foo", member: "bar"}, function(err, rows) { expect(rows.length).toEqual(0); done(); }); }); it("#find returns empty array when target not in current domain", function(done) { comments.find({type: "guide", cls: "forms", member: ""}, function(rows) { comments.find({type: "guide", cls: "forms", member: ""}, function(err, rows) { expect(rows.length).toEqual(0); done(); }); }); it("#findRecent returns n recent comments", function(done) { comments.findRecent({limit: 10, offset: 0}, function(rows) { comments.findRecent({limit: 10, offset: 0}, function(err, rows) { expect(rows.length).toEqual(10); done(); }); }); it("#findRecent without offset option defaults to offset:0", function(done) { comments.findRecent({limit: 10}, function(rows) { comments.findRecent({limit: 10}, function(err, rows) { expect(rows.length).toEqual(10); done(); }); }); it("#findRecent without limit option defaults to limit:100 or max nr of comments", function(done) { comments.findRecent({}, function(rows) { comments.findRecent({}, function(err, rows) { expect(rows.length).toEqual(24); done(); }); }); it("#count gets total number of comments in current domain", function(done) { comments.count({}, function(cnt) { comments.count({}, function(err, cnt) { expect(cnt).toEqual(24); done(); }); Loading @@ -102,7 +102,7 @@ describe("Comments", function() { }); it("#count gets total number of comments in the other domain", function(done) { comments.count({}, function(cnt) { comments.count({}, function(err, cnt) { expect(cnt).toEqual(4); done(); }); Loading @@ -110,7 +110,7 @@ describe("Comments", function() { }); it("#countPerTarget gets number of comments for each target", function(done) { comments.countsPerTarget(function(counts) { comments.countsPerTarget(function(err, counts) { expect(counts["class__Ext__"]).toEqual(5); done(); }); Loading @@ -127,8 +127,8 @@ describe("Comments", function() { cls: "Ext", member: "method-getBody" }; comments.add(com, function(id) { comments.getById(id, function(newCom) { comments.add(com, function(err, id) { comments.getById(id, function(err, newCom) { expect(newCom.id).toEqual(id); done(); }); Loading @@ -146,8 +146,8 @@ describe("Comments", function() { cls: "Blah", member: "method-foo" }; comments.add(com, function(id) { comments.getById(id, function(newCom) { comments.add(com, function(err, id) { comments.getById(id, function(err, newCom) { expect(newCom.cls).toEqual("Blah"); done(); }); Loading @@ -163,8 +163,8 @@ describe("Comments", function() { content: "New content.", content_html: "<p>New content.</p>" }; comments.update(com, function() { comments.getById(com.id, function(newCom) { comments.update(com, function(err) { comments.getById(com.id, function(err, newCom) { expect(newCom.content).toEqual("New content."); done(); }); Loading @@ -179,8 +179,8 @@ describe("Comments", function() { content: "New content.", content_html: "<p>New content.</p>" }; comments.update(com, function() { comments.getById(com.id, function(newCom) { comments.update(com, function(err) { comments.getById(com.id, function(err, newCom) { expect(newCom.user_id).toEqual(4); done(); }); Loading
comments/db_facade.js +8 −9 Original line number Diff line number Diff line Loading @@ -26,25 +26,22 @@ DbFacade.prototype = { * whitespace. * @param {Mixed[]} params Parameters for the query * @param {Function} callback Called after query finishes:. * @param {Error} callback.err The error object on failure or null on success. * @param {Object[]/Object} callback.result The result of callback. * In case of SELECT it's an array of rows, * In case of INSERT it's object with insertId property. */ query: function(sql, params, callback) { sql = (typeof sql === "string") ? sql : sql.join("\n"); this.connection.query(sql, params, function(err, result) { if (err) throw err; callback(result); }); this.connection.query(sql, params, callback); }, /** * Exactly like #query, except that the result is just one row. */ queryOne: function(sql, params, callback) { this.query(sql, params, function(rows) { callback(rows[0]); this.query(sql, params, function(err, rows) { callback(err, rows[0]); }); }, Loading @@ -54,11 +51,12 @@ DbFacade.prototype = { * @param {String} table Name of the table. * @param {Object} fields The row to insert. * @param {Function} callback Called when query finishes. * @param {Error} callback.err The error object. * @param {Number} callback.insertId ID of the inserted row. */ insert: function(table, fields, callback) { this.query("INSERT INTO "+table+" SET ?", [fields], function(result) { callback(result.insertId); this.query("INSERT INTO "+table+" SET ?", [fields], function(err, result) { callback(err, result.insertId); }); }, Loading @@ -69,6 +67,7 @@ DbFacade.prototype = { * @param {Object} fields The row to update. This must contain * an `id` field that's used to decide which row to update. * @param {Function} callback Called when query finishes. * @param {Error} callback.err The error object. */ update: function(table, fields, callback) { var id = fields.id; Loading
comments/targets.js +3 −2 Original line number Diff line number Diff line Loading @@ -21,12 +21,13 @@ Targets.prototype = { * @param {String} target.cls * @param {String} target.member * @param {Function} callback * @param {Error} callback.err * @param {Number} callback.id The ID of the target */ ensure: function(target, callback) { this.get(target, function(targetFound) { this.get(target, function(err, targetFound) { if (targetFound) { callback(targetFound.id); callback(err, targetFound.id); } else { this.add(target, callback); Loading