Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add delete endpoint to exercise metrics #30

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add delete endpoint to exercise metrics
  • Loading branch information
karinevieira committed Feb 10, 2024
commit 91c89aacf4d9353b4204c5dd4533e53d7ffae920
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
Loading