Commit f5f27465 by Rocky Duan

depth limit

parent 5a1dcfde
...@@ -21,20 +21,25 @@ namespace :db do ...@@ -21,20 +21,25 @@ namespace :db do
require_relative 'models/comment.rb' require_relative 'models/comment.rb'
require_relative 'models/comment_thread.rb' require_relative 'models/comment_thread.rb'
require_relative 'models/vote.rb' require_relative 'models/vote.rb'
require_relative 'models/user.rb'
Comment.delete_all Comment.delete_all
CommentThread.delete_all CommentThread.delete_all
Vote.delete_all Vote.delete_all
User.delete_all
depth_limit = YAML.load_file("config/application.yml")["depth_limit"]
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1 comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1
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
50.times do 10.times do
Comment.all.reject{|c| c.is_root?}.sample.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id comment = Comment.all.reject{|c| c.is_root? or c.depth - 1 >= depth_limit}.sample
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
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|
Vote.create! :value => ["up", "down"].sample, :comment_id => c.id, :user_id => id user = User.find_or_create_by_id(id)
user.vote(c, {:direction => [:up, :down].sample})
end end
end end
end end
......
...@@ -13,6 +13,7 @@ env_index = ARGV.index("-e") ...@@ -13,6 +13,7 @@ env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index env_arg = ARGV[env_index + 1] if env_index
env = env_arg || ENV["SINATRA_ENV"] || "development" env = env_arg || ENV["SINATRA_ENV"] || "development"
databases = YAML.load_file("config/database.yml") databases = YAML.load_file("config/database.yml")
config = YAML.load_file("config/application.yml")
ActiveRecord::Base.establish_connection(databases[env]) ActiveRecord::Base.establish_connection(databases[env])
# retrive all comments of a commentable object # retrive all comments of a commentable object
...@@ -49,6 +50,8 @@ post '/api/v1/comments/:comment_id' do |comment_id| ...@@ -49,6 +50,8 @@ post '/api/v1/comments/:comment_id' do |comment_id|
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
elsif comment.depth - 1 >= config["depth_limit"] # The depth should be subtracted by 1 because we are counting the super comment
error 400, {:error => "depth limit exceeded"}.to_json
else else
comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key}.merge({:comment_thread_id => comment.comment_thread_id}) comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key}.merge({:comment_thread_id => comment.comment_thread_id})
sub_comment = comment.children.create(comment_params) sub_comment = comment.children.create(comment_params)
......
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