2013-08-08 17:01:56 +08:00
|
|
|
|
class PraiseTreadController < ApplicationController
|
2013-08-12 14:50:54 +08:00
|
|
|
|
|
|
|
|
|
accept_api_auth :tread_plus,:praise_plus
|
|
|
|
|
before_filter :require_login,:only => [:praise_plus,:tread_plus]
|
|
|
|
|
|
2013-08-08 17:01:56 +08:00
|
|
|
|
def praise_plus
|
|
|
|
|
@obj = nil
|
2013-08-12 14:50:54 +08:00
|
|
|
|
# @is_in_list = nil
|
2013-08-08 17:01:56 +08:00
|
|
|
|
if request.get?
|
2013-08-11 23:44:12 +08:00
|
|
|
|
@obj_id = params[:obj_id]
|
|
|
|
|
@obj_type = params[:obj_type]
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@obj = find_object_by_type_and_id(@obj_type,@obj_id)
|
|
|
|
|
# @is_in_list = params[:is_in_list]
|
|
|
|
|
praise_tread_plus(@obj_type,@obj_id,1)
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def praise_minus
|
|
|
|
|
@obj = nil
|
|
|
|
|
if request.get?
|
|
|
|
|
@obj = params[:obj] # 传的是对象,最后变成id了
|
|
|
|
|
|
|
|
|
|
#首先更新praise_tread 表 删除关注记录
|
|
|
|
|
@pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user")
|
|
|
|
|
@pt.delete
|
|
|
|
|
|
|
|
|
|
#再更新praise_tread_cache表 使相应的记录减1 当为0时删除
|
|
|
|
|
@ptc = PraiseTreadCache.find_by_object_id(@obj)
|
|
|
|
|
@ptc.minus(1)
|
|
|
|
|
if @ptc.praise_num == 0
|
|
|
|
|
@ptc.delete
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
@obj = User.find_by_id(@obj)
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tread_plus
|
2013-08-11 23:44:12 +08:00
|
|
|
|
@obj = nil
|
2013-08-12 14:50:54 +08:00
|
|
|
|
# @is_in_list = nil
|
2013-08-11 23:44:12 +08:00
|
|
|
|
if request.get?
|
|
|
|
|
@obj_id = params[:obj_id]
|
|
|
|
|
@obj_type = params[:obj_type]
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@obj = find_object_by_type_and_id(@obj_type,@obj_id)
|
|
|
|
|
# @is_in_list = params[:is_in_list]
|
|
|
|
|
praise_tread_plus(@obj_type,@obj_id,0)
|
2013-08-11 23:44:12 +08:00
|
|
|
|
end
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tread_minus
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-11 23:44:12 +08:00
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_object_by_type_and_id(type,id)
|
|
|
|
|
@obj = nil
|
|
|
|
|
case type
|
|
|
|
|
when 'User'
|
|
|
|
|
@obj = User.find_by_id(id)
|
|
|
|
|
when 'Issue'
|
|
|
|
|
@obj = Issue.find_by_id(id)
|
|
|
|
|
when 'Project'
|
|
|
|
|
@obj = Project.find_by_id(id)
|
|
|
|
|
when 'Bid'
|
|
|
|
|
@obj = Bid.find_by_id(id)
|
|
|
|
|
end
|
|
|
|
|
return @obj
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def praise_tread_plus(type,id,flag)
|
|
|
|
|
unless id.nil? and type.nil?
|
|
|
|
|
#首先创建或更新praise_tread 表
|
|
|
|
|
@pt = PraiseTread.new
|
|
|
|
|
@pt.user_id = User.current.id
|
|
|
|
|
@pt.praise_tread_object_id = id.to_i
|
|
|
|
|
@pt.praise_tread_object_type = type
|
|
|
|
|
@pt.praise_or_tread = flag
|
|
|
|
|
@pt.save
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
#再创建或更新praise_tread_cache表
|
|
|
|
|
@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type)
|
|
|
|
|
@ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@ptc.object_id = id.to_i
|
|
|
|
|
@ptc.object_type = type
|
|
|
|
|
@ptc.save
|
|
|
|
|
@ptc.plus(flag,1)
|
2013-08-11 23:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|