Commit 405bb12b by Rocky Duan

api for tag autocompletion

parent cc9c6081
...@@ -21,6 +21,17 @@ Mongoid.logger.level = Logger::INFO ...@@ -21,6 +21,17 @@ Mongoid.logger.level = Logger::INFO
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file} Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file} Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
get '/api/v1/search/threads' do
results = CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"]
end.results
puts params["tags"].split /,/
puts results
results.map(&:to_hash).to_json
end
delete '/api/v1/:commentable_id/threads' do |commentable_id| delete '/api/v1/:commentable_id/threads' do |commentable_id|
commentable.comment_threads.destroy_all commentable.comment_threads.destroy_all
{}.to_json {}.to_json
...@@ -45,7 +56,9 @@ get '/api/v1/threads/tags' do ...@@ -45,7 +56,9 @@ get '/api/v1/threads/tags' do
CommentThread.tags.to_json CommentThread.tags.to_json
end end
get '/api/v1/threads/tags/autocomplete' do
CommentThread.tags_autocomplete(params["value"], max: 5, sort_by_count: true).map(&:first).to_json
end
get '/api/v1/threads/:thread_id' do |thread_id| get '/api/v1/threads/:thread_id' do |thread_id|
thread.to_hash(recursive: params["recursive"]).to_json thread.to_hash(recursive: params["recursive"]).to_json
...@@ -129,23 +142,6 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id| ...@@ -129,23 +142,6 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
user.unsubscribe(source).to_hash.to_json user.unsubscribe(source).to_hash.to_json
end end
get '/api/v1/search/threads' do
if params["text"]
CommentThread.solr_search do
fulltext(params["text"])
if params["commentable_id"]
with(:commentable_id, params["commentable_id"])
end
end.results.map(&:to_hash).to_json
else
{}.to_json
end
end
post '/api/v1/search/threads/tags' do
CommentThread.tagged_with_all(params["tags"]).map(&:to_hash).to_json
end
if environment.to_s == "development" if environment.to_s == "development"
get '/api/v1/clean' do get '/api/v1/clean' do
Comment.delete_all Comment.delete_all
......
...@@ -133,4 +133,31 @@ describe "app" do ...@@ -133,4 +133,31 @@ describe "app" do
tags.length.should == 6 tags.length.should == 6
end end
end end
describe "GET /api/v1/threads/tags/autocomplete" do
def create_comment_thread(tags)
c = CommentThread.new(title: "Interesting question", body: "cool")
c.course_id = "1"
c.author = User.first
c.tags = tags
c.commentable_id = "1"
c.save!
c
end
it "returns autocomplete results" do
CommentThread.delete_all
create_comment_thread "c++, clojure, common-lisp, c#, c, coffeescript"
create_comment_thread "c++, clojure, common-lisp, c#, c"
create_comment_thread "c++, clojure, common-lisp, c#"
create_comment_thread "c++, clojure, common-lisp"
create_comment_thread "c++, clojure"
create_comment_thread "c++"
get "/api/v1/threads/tags/autocomplete", value: "c"
last_response.should be_ok
results = parse last_response.body
results.length.should == 5
%w[c++ clojure common-lisp c# c].each_with_index do |tag, index|
results[index].should == tag
end
end
end
end end
...@@ -3,9 +3,8 @@ require 'spec_helper' ...@@ -3,9 +3,8 @@ require 'spec_helper'
describe "app" do describe "app" do
describe "search" do describe "search" do
before(:each) { init_without_subscriptions } before(:each) { init_without_subscriptions }
describe "GET /api/v1/search/threads/tags" do describe "GET /api/v1/search/threads" do
it "returns all threads tagged with all tags" do it "returns all threads tagged with all tags" do
require 'uri'
thread1 = CommentThread.all.to_a.first thread1 = CommentThread.all.to_a.first
thread2 = CommentThread.all.to_a.last thread2 = CommentThread.all.to_a.last
ai = "artificial intelligence" ai = "artificial intelligence"
...@@ -18,33 +17,35 @@ describe "app" do ...@@ -18,33 +17,35 @@ describe "app" do
thread2.tags = [ai, ml, random2].join "," thread2.tags = [ai, ml, random2].join ","
thread2.save thread2.save
post "/api/v1/search/threads/tags", tags: [ai, ml] Sunspot.commit
get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body threads = parse last_response.body
threads.length.should == 2 threads.length.should == 2
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil
post "/api/v1/search/threads/tags", tags: [ai] get "/api/v1/search/threads", tags: [ai].join(",")
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body threads = parse last_response.body
threads.length.should == 2 threads.length.should == 2
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread2.id.to_s}.first.should_not be_nil
post "/api/v1/search/threads/tags", tags: [ai, random1] get "/api/v1/search/threads", tags: [ai, random1].join(",")
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body threads = parse last_response.body
threads.length.should == 1 threads.length.should == 1
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
post "/api/v1/search/threads/tags", tags: [random1] get "/api/v1/search/threads", tags: [random1].join(",")
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body threads = parse last_response.body
threads.length.should == 1 threads.length.should == 1
threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil threads.select{|t| t["id"] == thread1.id.to_s}.first.should_not be_nil
post "/api/v1/search/threads/tags", tags: [random1, random2] get "/api/v1/search/threads", tags: [random1, random2].join(",")
last_response.should be_ok last_response.should be_ok
threads = parse last_response.body threads = parse last_response.body
threads.length.should == 0 threads.length.should == 0
......
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