Commit 3d8d780d by Rocky Duan

api to get a single comment

parent bd78a26d
...@@ -58,6 +58,16 @@ post '/api/v1/comments/:comment_id' do |comment_id| ...@@ -58,6 +58,16 @@ post '/api/v1/comments/:comment_id' do |comment_id|
end end
end end
# get the information of a single comment
get '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find_by_id(comment_id)
if comment.nil? or comment.is_root?
error 400, {:error => "invalid comment id"}.to_json
else
comment.to_json
end
end
# delete the comment and the associated sub comments only if the comment is NOT the super comment # delete the comment and the associated sub comments only if the comment is NOT the super comment
delete '/api/v1/comments/:comment_id' do |comment_id| delete '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find_by_id(comment_id) comment = Comment.find_by_id(comment_id)
......
...@@ -17,24 +17,19 @@ class Comment < ActiveRecord::Base ...@@ -17,24 +17,19 @@ class Comment < ActiveRecord::Base
validates_presence_of :comment_thread_id validates_presence_of :comment_thread_id
def self.hash_tree(nodes) def self.hash_tree(nodes)
nodes.map do |node, sub_nodes| nodes.map {|node, sub_nodes| node.to_hash.merge(:children => hash_tree(sub_nodes).compact)}
{
:id => node.id,
:body => node.body,
:title => node.title,
:user_id => node.user_id,
:course_id => node.course_id,
:created_at => node.created_at,
:updated_at => node.updated_at,
:comment_thread_id => node.comment_thread_id,
:children => hash_tree(sub_nodes).compact,
:votes => {:up => Vote.comment_id(node.id).up.count, :down => Vote.comment_id(node.id).down.count},
}
end
end end
def to_hash_tree def to_hash_tree
self.class.hash_tree(self.subtree.arrange(:order => "updated_at DESC")) self.class.hash_tree(self.subtree.arrange(:order => "updated_at DESC"))
end end
def to_hash
attributes.merge(:votes => {:up => Vote.comment_id(id).up.count, :down => Vote.comment_id(id).down.count})
end
def to_json
to_hash.to_json
end
end end
...@@ -96,6 +96,29 @@ describe "app" do ...@@ -96,6 +96,29 @@ describe "app" do
children["updated_at"].should_not be_nil children["updated_at"].should_not be_nil
end end
end end
describe "GET on /api/v1/comments/:comment_id" do
it "returns information for a single comment" do
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1
comment = []
sub_comment = []
comment << (comment_thread.root_comments.create :body => "top comment", :title => "top 0", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id)
sub_comment << (comment[0].children.create :body => "comment body", :title => "comment title 0", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id)
sub_comment << (comment[0].children.create :body => "comment body", :title => "comment title 1", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id)
get "/api/v1/comments/#{comment[0].id}"
last_response.should be_ok
c = Yajl::Parser.parse last_response.body
c["title"].should == "top 0"
c["body"].should == "top comment"
c["user_id"].should == 1
c["course_id"].should == 1
c["comment_thread_id"].should == comment_thread.id
c["created_at"].should_not be_nil
c["updated_at"].should_not be_nil
c["votes"]["up"].should == 0
c["votes"]["down"].should == 0
c["children"].should be_nil
end
end
describe "DELETE on /api/v1/commentables/:commentable_type/:commentable_id" do describe "DELETE on /api/v1/commentables/:commentable_type/:commentable_id" do
before :each do before :each do
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1 comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment