This commit is contained in:
Rocky Duan 2012-08-01 16:44:21 -04:00
Родитель 8b0ccc0c72
Коммит 353a69814e
2 изменённых файлов: 18 добавлений и 4 удалений

13
app.rb
Просмотреть файл

@ -25,6 +25,7 @@ get '/api/v1/search/threads' do
CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
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"]
end.results.map(&:to_hash).to_json
end
@ -35,7 +36,17 @@ delete '/api/v1/:commentable_id/threads' do |commentable_id|
end
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
post '/api/v1/:commentable_id/threads' do |commentable_id|

Просмотреть файл

@ -18,7 +18,8 @@ describe "app" do
it "get all comment threads associated with a commentable object" do
get "/api/v1/question_1/threads"
last_response.should be_ok
threads = parse last_response.body
response = parse last_response.body
threads = response['collection']
threads.length.should == 2
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
@ -26,7 +27,8 @@ describe "app" do
it "get all comment threads and comments associated with a commentable object" do
get "/api/v1/question_1/threads", recursive: true
last_response.should be_ok
threads = parse last_response.body
response = parse last_response.body
threads = response['collection']
threads.length.should == 2
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
@ -45,7 +47,8 @@ describe "app" do
it "returns an empty array when the commentable object does not exist (no threads)" do
get "/api/v1/does_not_exist/threads"
last_response.should be_ok
threads = parse last_response.body
response = parse last_response.body
threads = response['collection']
threads.length.should == 0
end
end