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
|
|
|
|
requires :openid, type: String
|
|
|
|
end
|
|
|
|
post ':id' do
|
|
|
|
obj_id = params[:id]
|
|
|
|
obj_type = params[:type]
|
|
|
|
user = UserWechat.find_by_openid(params[:openid]).user
|
2016-04-07 15:42:08 +08:00
|
|
|
obj = PraiseTreadCache.find_object_by_type_and_id(obj_id,obj_type)
|
2016-04-06 11:40:05 +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)
|
|
|
|
if pts.empty?
|
|
|
|
praise_or_cancel(obj_type,obj_id,user,1)
|
|
|
|
num = get_activity_praise_num(obj)
|
|
|
|
else
|
2016-04-07 15:17:42 +08:00
|
|
|
pts.delete if !pts.empty?
|
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
|
|
|
|
ptc.praise_minus(1) if !ptc.nil?
|
|
|
|
if ptc.praise_num == 0
|
|
|
|
ptc.delete
|
|
|
|
end
|
|
|
|
num = get_activity_praise_num(obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
present :data, num
|
|
|
|
present :status, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|