Commit eacc4b9

mo khan <mo@mokhan.ca>
2016-12-25 17:59:18
add api endpoint to load next workout.
1 parent 826fe09
Changed files (4)
app
controllers
views
api
config
spec
app/controllers/api/workouts_controller.rb
@@ -2,4 +2,8 @@ class Api::WorkoutsController < Api::Controller
   def index
     @workouts = current_user.workouts.recent.includes(:exercise_sets).limit(12)
   end
+
+  def new
+    @workout = current_user.next_workout_for(current_user.next_routine)
+  end
 end
app/views/api/workouts/new.json.jbuilder
@@ -0,0 +1,5 @@
+json.body_weight @workout.body_weight
+json.exercises @workout.sets.group_by(&:exercise) do |exercise, sets|
+  json.id exercise.id
+  json.name exercise.name
+end
config/routes.rb
@@ -17,6 +17,6 @@ Rails.application.routes.draw do
 
   namespace :api, defaults: { format: 'json' }  do
     resources :sessions, only: [:create]
-    resources :workouts, only: [:index]
+    resources :workouts, only: [:index, :new]
   end
 end
spec/controllers/api/workouts_controller_spec.rb
@@ -1,14 +1,14 @@
 require "rails_helper"
 
 describe Api::WorkoutsController do
-  describe "#index" do
-    render_views
-    let(:user) { create(:user) }
+  render_views
+  let(:user) { create(:user) }
 
-    before :each do
-      api_login(user)
-    end
+  before :each do
+    api_login(user)
+  end
 
+  describe "#index" do
     it "returns each workout" do
       workout = create(:workout, user: user)
 
@@ -19,4 +19,24 @@ describe Api::WorkoutsController do
       expect(json[:workouts].count).to eql(1)
     end
   end
+
+  describe "#new" do
+    include_context "stronglifts_program"
+
+    it 'loads the next workout' do
+      get :new, format: :json
+
+      expect(response).to have_http_status(:ok)
+      json = JSON.parse(response.body, symbolize_names: true)
+
+      expect(json[:body_weight]).to eql({ amount: 0.0, unit: 'lbs' })
+      expect(json[:exercises]).to match_array([
+        { id: barbell_row.id, name: barbell_row.name, },
+        { id: bench_press.id, name: bench_press.name, },
+        { id: dips.id, name: dips.name, },
+        { id: planks.id, name: planks.name, },
+        { id: squat.id, name: squat.name, },
+      ])
+    end
+  end
 end