diff --git a/app/blueprints/api/v1/favorite_blueprint.rb b/app/blueprints/api/v1/favorite_blueprint.rb new file mode 100644 index 0000000..fc482ee --- /dev/null +++ b/app/blueprints/api/v1/favorite_blueprint.rb @@ -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 diff --git a/app/controllers/api/v1/favorites_controller.rb b/app/controllers/api/v1/favorites_controller.rb index 177f2f2..4f5279b 100644 --- a/app/controllers/api/v1/favorites_controller.rb +++ b/app/controllers/api/v1/favorites_controller.rb @@ -18,14 +18,23 @@ module Api user_id: current_user.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 def destroy raise Api::V1::UnauthorizedError unless current_user @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 private