Skip to content

Commit

Permalink
feat: add delete endpoint to exercise metrics (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira authored Feb 10, 2024
1 parent cfda0eb commit a1af543
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
8 changes: 7 additions & 1 deletion app/controllers/api/v1/exercise_metrics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Api
module V1
class ExerciseMetricsController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index create update]
skip_before_action :authenticate_user!, only: %i[index create update destroy]

def index
options = { is_collection: true }
Expand Down Expand Up @@ -31,6 +31,12 @@ def update
end
end

def destroy
exercise_metric = current_user.exercise_metrics.find(params[:id])

exercise_metric.destroy
end

private

def exercise_metric_params
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/exercise_metric_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class ExerciseMetricSerializer
include JSONAPI::Serializer
attributes :user_id, :name, :steps, :distance_in_m, :intensity
attributes :user_id, :name, :steps, :distance_in_m, :intensity, :duration_in_min
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace :api do
namespace :v1 do
resources :bmi_calculations, only: :show
resources :exercise_metrics, only: %i[index create update]
resources :exercise_metrics, only: %i[index create update destroy]
resources :profiles, only: %i[index show update]
end
end
Expand Down
22 changes: 22 additions & 0 deletions spec/requests/api/v1/exercise_metrics_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,26 @@
end
end
end

describe "DELETE #destroy" do
it "renders a successful response" do
user = create(:user)
exercise_metric = create(:exercise_metric, user: user)

delete api_v1_exercise_metric_path(exercise_metric),
params: { user_id: user.id }, as: :json

expect(response).to have_http_status(:no_content)
end

it "destroys user's exercise metric" do
user = create(:user)
exercise_metric = create(:exercise_metric, user: user)

delete api_v1_exercise_metric_path(exercise_metric),
params: { user_id: user.id }, as: :json

expect(user.reload.exercise_metrics.last).not_to eq(exercise_metric)
end
end
end
7 changes: 7 additions & 0 deletions spec/routing/api/v1/exercise_metrics_routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@
action: :update
)
end

it do
expect(described_class).to route(:delete, "/api/v1/exercise_metrics/1").to(
id: "1", controller: "api/v1/exercise_metrics",
action: :destroy
)
end
end

0 comments on commit a1af543

Please sign in to comment.