Skip to content

Commit

Permalink
feat: add endpoint to user's accelerometer and exercise metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira committed May 22, 2024
1 parent 99cd25c commit 0000402
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/v1/user_metrics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Api
module V1
class UserMetricsController < ApplicationController
skip_before_action :authenticate_user!, only: :index

def index
current_user = User.find(params[:user_id])
render json: UserMetricSerializer.new(current_user)
end
end
end
end
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, :duration_in_min
attributes :user_id, :name, :steps, :distance_in_m, :intensity, :duration_in_min, :created_at
end
6 changes: 6 additions & 0 deletions app/serializers/user_metric_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class UserMetricSerializer
include JSONAPI::Serializer
attributes :email, :name, :exercise_metrics, :accelerometer_metrics
end
8 changes: 7 additions & 1 deletion app/serializers/user_profile_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

class UserProfileSerializer
include JSONAPI::Serializer
attributes :weight, :height_in_cm, :workout_in_min, :workout_days_frequency, :active_lifestyle, :gender, :physical_activities # rubocop:disable Layout/LineLength
attributes :weight,
:height_in_cm,
:workout_in_min,
:workout_days_frequency,
:active_lifestyle,
:gender,
:physical_activities
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
resources :bmi_calculations, only: :show
resources :exercise_metrics, only: %i[index create update destroy]
resources :profiles, only: %i[index show update]
resources :user_metrics, only: :index
end
end
end
15 changes: 15 additions & 0 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- [Authentication](#authentication)
- [Profiles](#profiles)
- [User Metrics](#user-metrics)
- [Exercise Metrics](#exercise-metrics)
- [Accelerometer Metrics](#accelerometer-metrics)

Expand Down Expand Up @@ -206,6 +207,20 @@ PUT `/api/v1/profiles/:id`
}
}
```
## User Metrics

### Read

Returns a list of user's accelerometer and exercise metrics.

GET `/api/v1/user_metrics?user_id={id}`

```
curl -X GET https://balance-dxhn.onrender.com/api/v1/user_metrics?user_id=1
-H 'Accept: application/json'
-H 'Content-Type: application/json'
```

## Exercise Metrics

### Create
Expand Down
29 changes: 29 additions & 0 deletions spec/requests/api/v1/user_metrics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Api::V1::UserMetricsController do
describe "GET #index" do
it "renders a successful response" do
user = create(:user)

get api_v1_user_metrics_path(user_id: user.id), as: :json

expect(response).to be_successful
end

it "renders a list of user's accelerometer and exercise metrics" do
user = create(:user)
create_list(:accelerometer_metric, 3, user: user)
create_list(:exercise_metric, 3, user: user)
get api_v1_user_metrics_path(user_id: user.id), as: :json

parsed_response_body = response.parsed_body["data"]
accelerometer_metrics_response = parsed_response_body["attributes"]["accelerometer_metrics"]
exercise_metrics_response = parsed_response_body["attributes"]["exercise_metrics"]

expect(accelerometer_metrics_response.count).to eq(3)
expect(exercise_metrics_response.count).to eq(3)
end
end
end
11 changes: 11 additions & 0 deletions spec/routing/api/v1/user_metrics_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Api::V1::UserMetricsController do
it do
expect(described_class).to route(:get, "/api/v1/user_metrics").to(
controller: "api/v1/user_metrics", action: :index
)
end
end

0 comments on commit 0000402

Please sign in to comment.