Commit 035d6f4c by Rocky Duan

return comment after voting

parent bfb43459
...@@ -27,8 +27,9 @@ namespace :db do ...@@ -27,8 +27,9 @@ namespace :db do
Vote.delete_all Vote.delete_all
User.delete_all User.delete_all
level_limit = YAML.load_file("config/application.yml")["level_limit"] level_limit = YAML.load_file("config/application.yml")["level_limit"]
(1..3).each do |question_id|
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => question_id def generate_comments(commentable_type, commentable_id, level_limit)
comment_thread = CommentThread.create! :commentable_type => commentable_type, :commentable_id => commentable_id
5.times do 5.times do
comment_thread.root_comments.create :body => "top comment", :title => "top #{rand(10)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id comment_thread.root_comments.create :body => "top comment", :title => "top #{rand(10)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
end end
...@@ -37,6 +38,15 @@ namespace :db do ...@@ -37,6 +38,15 @@ namespace :db do
comment.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id comment.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
end end
end end
generate_comments("questions" , 1, level_limit)
generate_comments("questions" , 2, level_limit)
generate_comments("questions" , 3, level_limit)
generate_comments("courses" , 1, level_limit)
generate_comments("lectures" , 1, level_limit)
generate_comments("lectures" , 2, level_limit)
generate_comments("lectures" , 3, level_limit)
Comment.all.reject{|c| c.is_root?}.each do |c| Comment.all.reject{|c| c.is_root?}.each do |c|
(1..20).each do |id| (1..20).each do |id|
user = User.find_or_create_by_id(id) user = User.find_or_create_by_id(id)
...@@ -44,4 +54,5 @@ namespace :db do ...@@ -44,4 +54,5 @@ namespace :db do
end end
end end
end end
end end
...@@ -69,7 +69,6 @@ end ...@@ -69,7 +69,6 @@ end
# get the information of a single comment # get the information of a single comment
get '/api/v1/comments/:comment_id' do |comment_id| get '/api/v1/comments/:comment_id' do |comment_id|
puts params
comment = Comment.find_by_id(comment_id) comment = Comment.find_by_id(comment_id)
if comment.nil? or comment.is_root? if comment.nil? or comment.is_root?
error 400, {:error => "invalid comment id"}.to_json error 400, {:error => "invalid comment id"}.to_json
...@@ -124,7 +123,7 @@ put '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id| ...@@ -124,7 +123,7 @@ put '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_id|
if %w[up down].include? params["value"] if %w[up down].include? params["value"]
user = User.find_or_create_by_id(user_id) user = User.find_or_create_by_id(user_id)
vote = user.vote(comment, { :direction => (params["value"] == "up" ? :up : :down ), :exclusive => :true}) vote = user.vote(comment, { :direction => (params["value"] == "up" ? :up : :down ), :exclusive => :true})
vote.to_json comment.to_json
else else
error 400, {:error => "value must be up or down"}.to_json error 400, {:error => "value must be up or down"}.to_json
end end
...@@ -138,7 +137,7 @@ delete '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_ ...@@ -138,7 +137,7 @@ delete '/api/v1/votes/comments/:comment_id/users/:user_id' do |comment_id, user_
comment = Comment.find_by_id(comment_id) comment = Comment.find_by_id(comment_id)
if user and comment and not comment.is_root? if user and comment and not comment.is_root?
vote = user.unvote_for(comment) vote = user.unvote_for(comment)
vote.to_json comment.to_json
else else
error 400, {:error => "invalid user or comment id"}.to_json error 400, {:error => "invalid user or comment id"}.to_json
end end
......
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