Loading comments/lib/comments.js +79 −0 Original line number Diff line number Diff line Loading @@ -370,6 +370,85 @@ Comments.prototype = { }); }, /** * Adds tag to the comment. * * @param {Object} tag * @param {Number} tag.user_id The user who's tagging. * @param {Number} tag.comment_id The comment that's tagged. * @param {Number} tag.tagname The text for the tag. * @param {Function} callback * @param {Error} callback.err */ addTag: function(tag, callback) { this.ensureTag(tag.tagname, function(err, tag_id) { if (err) { callback(err); return; } // replace `tagname` with `tag_id` in tag object delete tag.tagname; tag.tag_id = tag_id; this.db.insert("comment_tags", tag, callback); }.bind(this)); }, /** * Removes tag from comment. * * @param {Object} tag * @param {Number} tag.comment_id The comment with the tag. * @param {Number} tag.tagname The text of the tag. * @param {Function} callback * @param {Error} callback.err */ removeTag: function(tag, callback) { this.getTagId(tag.tagname, function(err, tag_id) { if (err) { callback(err); return; } var sql = "DELETE FROM comment_tags WHERE comment_id = ? AND tag_id = ?"; this.db.query(sql, [tag.comment_id, tag_id], callback); }.bind(this)); }, // Returns ID of the given tagname, // creating a new entry to tags table when needed. ensureTag: function(tagname, callback) { this.db.insert("tags", {domain: this.domain, tagname: tagname}, function(err, tag_id) { if (err && err.code === "ER_DUP_ENTRY") { // tag already exists, retrieve it this.getTagId(tagname, callback); } else if (err) { callback(err); } else { callback(null, tag_id); } }.bind(this)); }, // Simple lookup of tag ID by name getTagId: function(tagname, callback) { var sql = "SELECT * FROM tags WHERE tagname = ? AND domain = ?"; this.db.queryOne(sql, [tagname, this.domain], function(err, existingTag) { if (err) { callback(err); } else if (!existingTag) { callback("Tag '"+tagname+"' not found."); } else { callback(null, existingTag.id); } }); }, /** * Retrieves users ordered by number of upvotes or number of comments. * @param {String} sortBy Either "votes" or "comments" Loading comments/spec/comments.spec.js +35 −0 Original line number Diff line number Diff line Loading @@ -443,4 +443,39 @@ describe("Comments", function() { }); }); it("#addTag adds existing tag to a comment", function(done) { comments.addTag({tagname: 'feature', comment_id: 3, user_id: 1}, function(err, com) { comments.getById(3, function(err, com) { expect(com.tags).toEqual("feature"); done(); }); }); }); it("#addTag adds new non-existing tag to a comment", function(done) { comments.addTag({tagname: 'blah', comment_id: 4, user_id: 1}, function(err, com) { comments.getById(4, function(err, com) { expect(com.tags).toEqual("blah"); done(); }); }); }); it("#removeTag removes tag from comment", function(done) { comments.removeTag({tagname: 'feature', comment_id: 2}, function(err, com) { comments.getById(2, function(err, com) { expect(com.tags).toEqual(""); done(); }); }); }); it("#removeTag does nothing when removing non-existing tag", function(done) { comments.removeTag({tagname: 'blablabla', comment_id: 5}, function(err, com) { comments.getById(5, function(err, com) { expect(com.tags).toEqual(""); done(); }); }); }); }); Loading
comments/lib/comments.js +79 −0 Original line number Diff line number Diff line Loading @@ -370,6 +370,85 @@ Comments.prototype = { }); }, /** * Adds tag to the comment. * * @param {Object} tag * @param {Number} tag.user_id The user who's tagging. * @param {Number} tag.comment_id The comment that's tagged. * @param {Number} tag.tagname The text for the tag. * @param {Function} callback * @param {Error} callback.err */ addTag: function(tag, callback) { this.ensureTag(tag.tagname, function(err, tag_id) { if (err) { callback(err); return; } // replace `tagname` with `tag_id` in tag object delete tag.tagname; tag.tag_id = tag_id; this.db.insert("comment_tags", tag, callback); }.bind(this)); }, /** * Removes tag from comment. * * @param {Object} tag * @param {Number} tag.comment_id The comment with the tag. * @param {Number} tag.tagname The text of the tag. * @param {Function} callback * @param {Error} callback.err */ removeTag: function(tag, callback) { this.getTagId(tag.tagname, function(err, tag_id) { if (err) { callback(err); return; } var sql = "DELETE FROM comment_tags WHERE comment_id = ? AND tag_id = ?"; this.db.query(sql, [tag.comment_id, tag_id], callback); }.bind(this)); }, // Returns ID of the given tagname, // creating a new entry to tags table when needed. ensureTag: function(tagname, callback) { this.db.insert("tags", {domain: this.domain, tagname: tagname}, function(err, tag_id) { if (err && err.code === "ER_DUP_ENTRY") { // tag already exists, retrieve it this.getTagId(tagname, callback); } else if (err) { callback(err); } else { callback(null, tag_id); } }.bind(this)); }, // Simple lookup of tag ID by name getTagId: function(tagname, callback) { var sql = "SELECT * FROM tags WHERE tagname = ? AND domain = ?"; this.db.queryOne(sql, [tagname, this.domain], function(err, existingTag) { if (err) { callback(err); } else if (!existingTag) { callback("Tag '"+tagname+"' not found."); } else { callback(null, existingTag.id); } }); }, /** * Retrieves users ordered by number of upvotes or number of comments. * @param {String} sortBy Either "votes" or "comments" Loading
comments/spec/comments.spec.js +35 −0 Original line number Diff line number Diff line Loading @@ -443,4 +443,39 @@ describe("Comments", function() { }); }); it("#addTag adds existing tag to a comment", function(done) { comments.addTag({tagname: 'feature', comment_id: 3, user_id: 1}, function(err, com) { comments.getById(3, function(err, com) { expect(com.tags).toEqual("feature"); done(); }); }); }); it("#addTag adds new non-existing tag to a comment", function(done) { comments.addTag({tagname: 'blah', comment_id: 4, user_id: 1}, function(err, com) { comments.getById(4, function(err, com) { expect(com.tags).toEqual("blah"); done(); }); }); }); it("#removeTag removes tag from comment", function(done) { comments.removeTag({tagname: 'feature', comment_id: 2}, function(err, com) { comments.getById(2, function(err, com) { expect(com.tags).toEqual(""); done(); }); }); }); it("#removeTag does nothing when removing non-existing tag", function(done) { comments.removeTag({tagname: 'blablabla', comment_id: 5}, function(err, com) { comments.getById(5, function(err, com) { expect(com.tags).toEqual(""); done(); }); }); }); });