mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-09 05:15:53 +10:00
53 lines
1.7 KiB
Ruby
53 lines
1.7 KiB
Ruby
class Api::V1::LikesController < Api::V1::ApplicationController
|
|
|
|
before_action :load_post, only: [:like, :unlike]
|
|
before_action :load_comment, only: [:comment_like, :comment_unlike]
|
|
|
|
def like
|
|
if @current_user.likes.where(likable_id: @target_post, likable_type: Post).present?
|
|
render json: {msg: '你已经喜欢过该文章了'}, status: 400
|
|
else
|
|
@current_user.likes.create likable: @target_post
|
|
@current_user.timeline_sources.create! post_id: @target_post.id, action_type: 'like'
|
|
render json:{msg: 'done', likes_count: @target_post.likes_count}
|
|
end
|
|
end
|
|
|
|
def unlike
|
|
like = @current_user.likes.find_by(likable_id: @target_post, likable_type: Post)
|
|
if like.present?
|
|
like.destroy!
|
|
render json:{msg: 'done', likes_count: @target_post.likes_count}
|
|
else
|
|
render json: {msg: '不能进行该操作'}, status: 400
|
|
end
|
|
end
|
|
|
|
def comment_like
|
|
if @current_user.likes.where(likable_id: @target_comment, likable_type: Comment).present?
|
|
render json: {msg: '你已经喜欢过该评论了'}, status: 400
|
|
else
|
|
@current_user.likes.create likable: @target_comment
|
|
render json:{msg: 'done', likes_count: @target_comment.likes_count}
|
|
end
|
|
end
|
|
|
|
def comment_unlike
|
|
like = @current_user.likes.find_by(likable_id: @target_comment, likable_type: Comment)
|
|
if like.present?
|
|
like.destroy!
|
|
render json:{msg: 'done', likes_count: @target_comment.likes_count}
|
|
else
|
|
render json: {msg: '不能进行该操作'}, status: 400
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def load_comment
|
|
@target_comment = Comment.find_by id: params[:comment_id]
|
|
render json: {msg: '不能找到该评论'}, status: 404 unless @target_comment
|
|
end
|
|
|
|
end
|