Refactor FavoritesController for blueprinter
This commit is contained in:
parent
f86aa30791
commit
5b37e480f8
2 changed files with 37 additions and 2 deletions
26
app/blueprints/api/v1/favorite_blueprint.rb
Normal file
26
app/blueprints/api/v1/favorite_blueprint.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Api
|
||||||
|
module V1
|
||||||
|
class FavoriteBlueprint < ApiBlueprint
|
||||||
|
identifier :id
|
||||||
|
fields :created_at, :updated_at
|
||||||
|
|
||||||
|
association :user,
|
||||||
|
name: :user,
|
||||||
|
blueprint: UserBlueprint,
|
||||||
|
view: :minimal
|
||||||
|
|
||||||
|
association :party,
|
||||||
|
name: :party,
|
||||||
|
blueprint: PartyBlueprint,
|
||||||
|
view: :preview
|
||||||
|
|
||||||
|
view :destroyed do
|
||||||
|
field :destroyed do
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -18,14 +18,23 @@ module Api
|
||||||
user_id: current_user.id,
|
user_id: current_user.id,
|
||||||
party_id: party_id
|
party_id: party_id
|
||||||
})
|
})
|
||||||
render :show, status: :created if @favorite.save!
|
|
||||||
|
if @favorite.save!
|
||||||
|
return render json: FavoriteBlueprint.render(@favorite, root: :favorite),
|
||||||
|
status: :created
|
||||||
|
end
|
||||||
|
|
||||||
|
render_validation_error_response(@favorite)
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
raise Api::V1::UnauthorizedError unless current_user
|
raise Api::V1::UnauthorizedError unless current_user
|
||||||
|
|
||||||
@favorite = Favorite.where(user_id: current_user.id, party_id: favorite_params[:party_id]).first
|
@favorite = Favorite.where(user_id: current_user.id, party_id: favorite_params[:party_id]).first
|
||||||
render :destroyed, status: :ok if @favorite && Favorite.destroy(@favorite.id)
|
render_not_found_response('favorite') unless @favorite
|
||||||
|
|
||||||
|
render_error("Couldn't delete favorite") unless Favorite.destroy(@favorite.id)
|
||||||
|
render json: FavoriteBlueprint.render(@favorite, root: :favorite, view: :destroyed)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue