diff --git a/comments/comments.js b/comments/comments.js index 9defb782c3cdea85735edd1ede76356da3d31bb1..5ae78fbbfa1b54c469943c375f995686cd8ecbc5 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 b8cb4b903f592fd219b8a66e5c81ce7956b830ff..a1e06f8ca7f49d2dfe164798c02cc28c530b1e01 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();