Commit d8c77ab2 by Rocky Duan

add tag feature

parent 4edecb12
......@@ -32,6 +32,7 @@ end
post '/api/v1/:commentable_id/threads' do |commentable_id|
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
thread.tags = params["tags"] || ""
thread.author = user
thread.save!
if params["auto_subscribe"] and author
......@@ -46,6 +47,10 @@ end
put '/api/v1/threads/:thread_id' do |thread_id|
thread.update_attributes!(params.slice(*%w[title body]))
if params["tags"]
thread.tags = params["tags"]
thread.save!
end
thread.to_hash.to_json
end
......
......@@ -20,6 +20,9 @@ class CommentThread < Content
string :course_id
string :commentable_id
string :author_id
string :tags, multiple: true do
end
end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true, autosave: true
......@@ -53,10 +56,12 @@ class CommentThread < Content
end
def to_hash(params={})
doc = as_document.slice(*%w[title body course_id created_at updated_at]).
merge("id" => _id).
merge("user_id" => (author.id if author)).
merge("votes" => votes.slice(*%w[count up_count down_count point]))
doc = as_document.slice(*%w[title body course_id created_at updated_at])
.merge("id" => _id)
.merge("user_id" => (author.id if author))
.merge("votes" => votes.slice(*%w[count up_count down_count point]))
.merge("tags" => tags_array)
if params[:recursive]
doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
end
......
......@@ -32,6 +32,23 @@ describe "app" do
get "/api/v1/threads/does_not_exist"
last_response.status.should == 400
end
it "get information of a single comment thread with its tags" do
thread = CommentThread.new
thread.title = "new thread"
thread.body = "hahaah"
thread.course_id = "1"
thread.commentable_id = "1"
thread.author = User.first
thread.tags = "taga, tagb, tagc"
thread.save!
get "/api/v1/threads/#{thread.id}"
last_response.should be_ok
response_thread = parse last_response.body
response_thread["tags"].length.should == 3
response_thread["tags"].should include "taga"
response_thread["tags"].should include "tagb"
response_thread["tags"].should include "tagc"
end
end
describe "PUT /api/v1/threads/:thread_id" do
it "update information of comment thread" do
......@@ -46,6 +63,22 @@ describe "app" do
put "/api/v1/threads/does_not_exist", body: "new body", title: "new title"
last_response.status.should == 400
end
it "updates tag of comment thread" do
thread = CommentThread.first
put "/api/v1/threads/#{thread.id}", tags: "haha, hoho, huhu"
last_response.should be_ok
thread.reload
thread.tags_array.length.should == 3
thread.tags_array.should include "haha"
thread.tags_array.should include "hoho"
thread.tags_array.should include "huhu"
put "/api/v1/threads/#{thread.id}", tags: "aha, oho"
last_response.should be_ok
thread.reload
thread.tags_array.length.should == 2
thread.tags_array.should include "aha"
thread.tags_array.should include "oho"
end
end
describe "POST /api/v1/threads/:thread_id/comments" do
it "create a comment to the comment thread" do
......
......@@ -68,6 +68,16 @@ describe "app" do
Commentable.find("does_not_exist").comment_threads.length.should == 1
Commentable.find("does_not_exist").comment_threads.first.body.should == "cool"
end
it "create a new comment thread with tag" do
post '/api/v1/question_1/threads', title: "Interesting question", body: "cool", course_id: "1", user_id: "1", tags: "a, b, c"
last_response.should be_ok
CommentThread.count.should == 3
thread = CommentThread.where(title: "Interesting question").first
thread.tags_array.length.should == 3
thread.tags_array.should include "a"
thread.tags_array.should include "b"
thread.tags_array.should include "c"
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