From 5d70b1147f318962d7a8884977778fd70e293d0c Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 29 Aug 2012 14:27:32 +0300 Subject: [PATCH] Implement Comments#getById. --- comments/comments.js | 20 ++++++++++++++++++++ comments/comments.spec.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/comments/comments.js b/comments/comments.js index 9defb782..5ae78fbb 100644 --- a/comments/comments.js +++ b/comments/comments.js @@ -18,6 +18,26 @@ module.exports = (function(){ } Comments.prototype = { + /** + * Finds a single comment by ID. + * Excludes deleted comments. + * + * @param {Number} id The ID of the comment to find. + * @param {Function} callback Called with the result. + * @param {Object} callback.comment The comment found or undefined. + */ + getById: function(id, callback) { + var sql = [ + 'SELECT *', + 'FROM full_visible_comments', + 'WHERE domain = ? AND id = ?' + ]; + + this.query(sql, [this.domain, id], function(rows) { + callback(rows[0]); + }); + }, + /** * Finds list of all comments for a particular target. * Excludes deleted comments. diff --git a/comments/comments.spec.js b/comments/comments.spec.js index b8cb4b90..a1e06f8c 100644 --- a/comments/comments.spec.js +++ b/comments/comments.spec.js @@ -19,7 +19,35 @@ describe("Comments", function() { connection.end(); }); - it("#find returns comments for a target", function(done) { + it("#getById returns comment with given ID", function(done) { + comments.getById(7, function(com) { + expect(com.id).toEqual(7); + done(); + }); + }); + + it("#getById returns undefined when no comment with such ID", function(done) { + comments.getById(123456, function(com) { + expect(com).toEqual(undefined); + done(); + }); + }); + + it("#getById returns undefined when ID exists in other domain.", function(done) { + comments.getById(30, function(com) { + expect(com).toEqual(undefined); + done(); + }); + }); + + it("#getById returns undefined when comment with ID is deleted", function(done) { + comments.getById(6, function(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) { expect(rows.length).toEqual(5); done(); -- GitLab