diff --git a/README.md b/README.md index 02ceff0..22bdf5f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,11 @@ { "park_count": 474, "alert_count": 708, - "most_alerts": { + "state_with_most_parks": { + "state": "WY", + "park_count": 10 + }, + "park_with_most_alerts": { "park_code": "zion", "alert_count": 6 } diff --git a/app/controllers/api/v1/stats_controller.rb b/app/controllers/api/v1/stats_controller.rb index 5755150..360f68f 100644 --- a/app/controllers/api/v1/stats_controller.rb +++ b/app/controllers/api/v1/stats_controller.rb @@ -3,6 +3,7 @@ module Api::V1 def index @park_count = Park.count @alert_count = Alert.count + @state_with_most_parks = ParksByState.group(:state).count.max @park_with_most_alerts = Alert.group(:park_code).count.max end end diff --git a/app/views/api/v1/stats/index.json.jbuilder b/app/views/api/v1/stats/index.json.jbuilder index ecbd12e..a1bbe91 100644 --- a/app/views/api/v1/stats/index.json.jbuilder +++ b/app/views/api/v1/stats/index.json.jbuilder @@ -1,6 +1,10 @@ json.park_count @park_count json.alert_count @alert_count -json.most_alerts do +json.state_with_most_parks do + json.state @state_with_most_parks[0] + json.park_count @state_with_most_parks[1] +end +json.park_with_most_alerts do json.park_code @park_with_most_alerts[0] json.alert_count @park_with_most_alerts[1] end diff --git a/spec/requests/api/v1/stats_spec.rb b/spec/requests/api/v1/stats_spec.rb index 1e52b91..a52e31f 100644 --- a/spec/requests/api/v1/stats_spec.rb +++ b/spec/requests/api/v1/stats_spec.rb @@ -4,6 +4,7 @@ RSpec.describe "Api::V1::Stats", type: :request do describe "GET /stats" do it "returns summary stats" do alerts(:two).update!(park_code: parks(:one).code) + parks_by_states(:two).update!(state: "OR") get api_v1_stats_url @@ -11,7 +12,11 @@ RSpec.describe "Api::V1::Stats", type: :request do expect(response.parsed_body).to match( park_count: 2, alert_count: 2, - most_alerts: { + state_with_most_parks: { + state: "OR", + park_count: 2 + }, + park_with_most_alerts: { park_code: "crla", alert_count: 2 }