2016-04-06 11:40:05 +08:00
|
|
|
#coding=utf-8
|
|
|
|
|
|
|
|
module Mobile
|
|
|
|
module Apis
|
|
|
|
class Praise< Grape::API
|
|
|
|
include ApiHelper
|
|
|
|
resources :praise do
|
|
|
|
desc "praise an activity"
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :type, type: String
|
2016-06-20 14:39:20 +08:00
|
|
|
requires :token, type: String
|
2016-04-06 11:40:05 +08:00
|
|
|
end
|
|
|
|
post ':id' do
|
2016-06-20 14:39:20 +08:00
|
|
|
authenticate!
|
2016-04-06 11:40:05 +08:00
|
|
|
obj_id = params[:id]
|
|
|
|
obj_type = params[:type]
|
2016-06-20 14:39:20 +08:00
|
|
|
user = current_user
|
2016-04-07 16:39:04 +08:00
|
|
|
pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",obj_id,obj_type.to_s,user.id).first
|
|
|
|
if pts.blank?
|
2016-04-06 11:40:05 +08:00
|
|
|
praise_or_cancel(obj_type,obj_id,user,1)
|
2016-04-07 16:39:04 +08:00
|
|
|
obj = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
|
|
|
|
num = get_activity_praise_num(obj) if !obj.blank?
|
2016-04-06 11:40:05 +08:00
|
|
|
else
|
2016-04-07 16:39:04 +08:00
|
|
|
pts.destroy if !pts.blank?
|
2016-04-06 11:40:05 +08:00
|
|
|
#再更新praise_tread_cache表 使相应的记录减1 当为0时删除
|
|
|
|
ptc = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
|
2016-04-07 16:39:04 +08:00
|
|
|
ptc.praise_minus(1) if !ptc.blank?
|
2016-04-06 11:40:05 +08:00
|
|
|
if ptc.praise_num == 0
|
|
|
|
ptc.delete
|
|
|
|
end
|
2016-04-07 16:39:04 +08:00
|
|
|
obj = PraiseTreadCache.where("object_id=? and object_type=?",obj_id,obj_type.to_s).first
|
|
|
|
num = !obj.blank? ? get_activity_praise_num(obj) : 0
|
2016-04-06 11:40:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
present :data, num
|
|
|
|
present :status, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|