Commit 353a6981 by Rocky Duan

pagination for threads

parent 8b0ccc0c
...@@ -25,6 +25,7 @@ get '/api/v1/search/threads' do ...@@ -25,6 +25,7 @@ get '/api/v1/search/threads' do
CommentThread.solr_search do CommentThread.solr_search do
fulltext(params["text"]) if params["text"] fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"] with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:course_id, params["course_id"]) if params["course_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"] with(:tags).all_of(params["tags"].split /,/) if params["tags"]
end.results.map(&:to_hash).to_json end.results.map(&:to_hash).to_json
end end
...@@ -35,7 +36,17 @@ delete '/api/v1/:commentable_id/threads' do |commentable_id| ...@@ -35,7 +36,17 @@ delete '/api/v1/:commentable_id/threads' do |commentable_id|
end end
get '/api/v1/:commentable_id/threads' do |commentable_id| get '/api/v1/:commentable_id/threads' do |commentable_id|
commentable.comment_threads.map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))}.to_json page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
comment_threads = commentable.comment_threads
num_pages = [1, (comment_threads.count / per_page.to_f).ceil].max
page = [num_pages, [1, page].max].min
{
collection: comment_threads.page(page).per(per_page).map{|t| t.to_hash(recursive: value_to_boolean(params["recursive"]))},
num_pages: num_pages,
per_page: per_page,
page: page,
}.to_json
end end
post '/api/v1/:commentable_id/threads' do |commentable_id| post '/api/v1/:commentable_id/threads' do |commentable_id|
......
...@@ -18,7 +18,8 @@ describe "app" do ...@@ -18,7 +18,8 @@ describe "app" do
it "get all comment threads associated with a commentable object" do it "get all comment threads associated with a commentable object" do
get "/api/v1/question_1/threads" get "/api/v1/question_1/threads"
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body response = parse last_response.body
threads = response['collection']
threads.length.should == 2 threads.length.should == 2
threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil
threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil
...@@ -26,7 +27,8 @@ describe "app" do ...@@ -26,7 +27,8 @@ describe "app" do
it "get all comment threads and comments associated with a commentable object" do it "get all comment threads and comments associated with a commentable object" do
get "/api/v1/question_1/threads", recursive: true get "/api/v1/question_1/threads", recursive: true
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body response = parse last_response.body
threads = response['collection']
threads.length.should == 2 threads.length.should == 2
threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil threads.index{|c| c["body"] == "can anyone help me?"}.should_not be_nil
threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil threads.index{|c| c["body"] == "it is unsolvable"}.should_not be_nil
...@@ -45,7 +47,8 @@ describe "app" do ...@@ -45,7 +47,8 @@ describe "app" do
it "returns an empty array when the commentable object does not exist (no threads)" do it "returns an empty array when the commentable object does not exist (no threads)" do
get "/api/v1/does_not_exist/threads" get "/api/v1/does_not_exist/threads"
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body response = parse last_response.body
threads = response['collection']
threads.length.should == 0 threads.length.should == 0
end end
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