Commit f8f8e800 by Rocky Duan

use helper method to simplify

parent 8feb52a9
...@@ -20,134 +20,141 @@ Mongoid.logger.level = Logger::INFO ...@@ -20,134 +20,141 @@ 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}
helpers do
def commentable
@commentable ||= Commentable.find(params[:commentable_id])
end
def user # TODO handle 404 if integrated user service
@user ||= (User.find_or_create_by(external_id: params[:user_id]) if params[:user_id])
end
def thread
@thread ||= CommentThread.find(params[:thread_id])
end
def comment
@comment ||= Comment.find(params[:comment_id])
end
def source
@source ||= case params["source_type"]
when "user"
User.find_or_create_by(external_id: params["source_id"])
when "thread"
CommentThread.find(params["source_id"])
when "other"
Commentable.find(params["source_id"])
end
end
def vote_for(obj)
user.vote(obj, params["value"].to_sym)
obj.reload.to_hash.to_json
end
def undo_vote_for(obj)
user.unvote(obj)
obj.reload.to_hash.to_json
end
end
delete '/api/v1/:commentable_id/threads' do |commentable_id| delete '/api/v1/:commentable_id/threads' do |commentable_id|
Commentable.find(commentable_id).comment_threads.destroy_all commentable.comment_threads.destroy_all
{}.to_json {}.to_json
end end
get '/api/v1/:commentable_id/threads' do |commentable_id| get '/api/v1/:commentable_id/threads' do |commentable_id|
Commentable.find(commentable_id).comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json commentable.comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json
end end
post '/api/v1/:commentable_id/threads' do |commentable_id| 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 = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"] thread.author = user
thread.author = author
thread.save! thread.save!
if params["auto_subscribe"] if params["auto_subscribe"] and author
author.subscribe(thread) author.subscribe(thread)
end end
thread.to_hash.to_json thread.to_hash.to_json
end end
get '/api/v1/threads/:thread_id' do |thread_id| get '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id)
thread.to_hash(recursive: params["recursive"]).to_json thread.to_hash(recursive: params["recursive"]).to_json
end end
put '/api/v1/threads/:thread_id' do |thread_id| put '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id)
thread.update_attributes!(params.slice(*%w[title body])) thread.update_attributes!(params.slice(*%w[title body]))
thread.to_hash.to_json thread.to_hash.to_json
end end
post '/api/v1/threads/:thread_id/comments' do |thread_id| post '/api/v1/threads/:thread_id/comments' do |thread_id|
thread = CommentThread.find(thread_id)
comment = thread.comments.new(params.slice(*%w[body course_id])) comment = thread.comments.new(params.slice(*%w[body course_id]))
author = User.find_or_create_by(external_id: params["user_id"]) if params["user_id"] comment.author = user
comment.author = author
comment.save! comment.save!
if params["auto_subscribe"] if params["auto_subscribe"] and author
author.subscribe(thread) author.subscribe(thread)
end end
comment.to_hash.to_json comment.to_hash.to_json
end end
delete '/api/v1/threads/:thread_id' do |thread_id| delete '/api/v1/threads/:thread_id' do |thread_id|
thread = CommentThread.find(thread_id)
thread.destroy thread.destroy
thread.to_hash.to_json thread.to_hash.to_json
end end
get '/api/v1/comments/:comment_id' do |comment_id| get '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id)
comment.to_hash(recursive: params["recursive"]).to_json comment.to_hash(recursive: params["recursive"]).to_json
end end
put '/api/v1/comments/:comment_id' do |comment_id| put '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id)
comment.update_attributes!(params.slice(*%w[body endorsed])) comment.update_attributes!(params.slice(*%w[body endorsed]))
comment.to_hash.to_json comment.to_hash.to_json
end end
post '/api/v1/comments/:comment_id' do |comment_id| post '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id)
sub_comment = comment.children.new(params.slice(*%w[body course_id])) sub_comment = comment.children.new(params.slice(*%w[body course_id]))
sub_comment.author = User.find_or_create_by(external_id: params["user_id"]) sub_comment.author = user
sub_comment.comment_thread = comment.comment_thread sub_comment.comment_thread = comment.comment_thread
sub_comment.save! sub_comment.save!
sub_comment.to_hash.to_json sub_comment.to_hash.to_json
end end
delete '/api/v1/comments/:comment_id' do |comment_id| delete '/api/v1/comments/:comment_id' do |comment_id|
comment = Comment.find(comment_id)
comment.destroy comment.destroy
comment.to_hash.to_json comment.to_hash.to_json
end end
put '/api/v1/comments/:comment_id/votes' do |comment_id| put '/api/v1/comments/:comment_id/votes' do |comment_id|
comment = Comment.find(comment_id)
vote_for comment vote_for comment
end end
delete '/api/v1/comments/:comment_id/votes' do |comment_id| delete '/api/v1/comments/:comment_id/votes' do |comment_id|
comment = Comment.find(comment_id)
undo_vote_for comment undo_vote_for comment
end end
put '/api/v1/threads/:thread_id/votes' do |thread_id| put '/api/v1/threads/:thread_id/votes' do |thread_id|
thread = CommentThread.find(thread_id)
vote_for thread vote_for thread
end end
delete '/api/v1/threads/:thread_id/votes' do |thread_id| delete '/api/v1/threads/:thread_id/votes' do |thread_id|
thread = CommentThread.find(thread_id)
undo_vote_for thread undo_vote_for thread
end end
get '/api/v1/users/:user_id' do |user_id| get '/api/v1/users/:user_id' do |user_id|
user = User.find_or_create_by(external_id: user_id)
user.to_hash(complete: params["complete"]).to_json user.to_hash(complete: params["complete"]).to_json
end end
get '/api/v1/users/:user_id/notifications' do |user_id| get '/api/v1/users/:user_id/notifications' do |user_id|
user = User.find_or_create_by(external_id: user_id)
user.notifications.map(&:to_hash).to_json user.notifications.map(&:to_hash).to_json
end end
post '/api/v1/users/:user_id/subscriptions' do |user_id| post '/api/v1/users/:user_id/subscriptions' do |user_id|
user = User.find_or_create_by(external_id: user_id)
source = case params["source_type"]
when "user"
User.find_or_create_by(external_id: params["source_id"])
when "thread"
CommentThread.find(params["source_id"])
when "other"
Commentable.find(params["source_id"])
end
user.subscribe(source).to_hash.to_json user.subscribe(source).to_hash.to_json
end end
delete '/api/v1/users/:user_id/subscriptions' do |user_id| delete '/api/v1/users/:user_id/subscriptions' do |user_id|
user = User.find_or_create_by(external_id: user_id)
source = case params["source_type"]
when "user"
User.find_or_create_by(external_id: params["source_id"])
when "thread"
CommentThread.find(params["source_id"])
when "other"
Commentable.find(params["source_id"])
end
user.unsubscribe(source).to_hash.to_json user.unsubscribe(source).to_hash.to_json
end end
...@@ -180,14 +187,6 @@ if env.to_s == "development" ...@@ -180,14 +187,6 @@ if env.to_s == "development"
end end
end end
def vote_for(obj) error BSON::InvalidObjectId do
user = User.find_or_create_by(external_id: params["user_id"]) error 404
user.vote(obj, params["value"].to_sym)
obj.reload.to_hash.to_json
end
def undo_vote_for(obj)
user = User.find_or_create_by(external_id: params["user_id"])
user.unvote(obj)
obj.reload.to_hash.to_json
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