Files
libr-2/app/controllers/api/v1/bookmarks_controller.rb
2015-10-12 21:25:01 +08:00

24 lines
820 B
Ruby

class Api::V1::BookmarksController < Api::V1::ApplicationController
before_action :load_post, only: [:bookmark, :unbookmark]
def bookmark
if @target_post.favorites.where(user_id: @current_user.id, favoritable_type: Post).present?
render json: {msg: '你已经收藏该文章了'}, status: 400
else
@target_post.favorites.create user_id: @current_user.id
render json:{msg: 'done', bookmarks_count: @target_post.reload.bookmarks_count}
end
end
def unbookmark
bookmark = @target_post.favorites.find_by(user_id: @current_user.id, favoritable_type: Post)
if bookmark.present?
bookmark.destroy!
render json:{msg: 'done', bookmarks_count: @target_post.reload.bookmarks_count}
else
render json: {msg: '不能进行该操作'}, status: 400
end
end
end