mirror of
https://github.com/wahyd4/libr-2.git
synced 2026-08-09 05:15:53 +10:00
24 lines
820 B
Ruby
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
|