/** * Responds to POST::/workouts/create requests * * @param WorkoutRequest $request The request containing the new workout data */ public function store(WorkoutRequest $request) { $workout = Workout::create(['user_id' => \Auth::id()]); // very similar to update's logic except there is no checking for existing // entities. $valid = true; $invalidProperty = null; $exercise_groups = array_values($request->get('exercise_groups')); for ($i = 0; $i < count($exercise_groups); $i++) { $group_num = $i + 1; $group = $exercise_groups[$i]; if (!$valid) { $invalidProperty = 'exercise_groups'; break; } $exercise_group = ExerciseGroup::create(['workout_id' => $workout->id, 'exercise_id' => $group['exercise_id'], 'group_order' => $group_num]); $exercise = $exercise_group->exercise; $exercise_measurements = array_values($group['exercise_measurements']); for ($j = 0; $j < count($exercise_measurements); $j++) { $measurement_num = $j + 1; $measurement = $exercise_measurements[$j]; $weight = null; $weight_unit_id = null; $distance = null; $distance_unit_id = null; $reps = null; $time = null; if ($exercise->exercise_type & Exercise::$exerciseTypeWeighted) { if (!isset($measurement['weight'])) { $valid = false; $invalidProperty = 'weight'; break; } $weight = $measurement['weight']; $weight_unit_id = $measurement['weight_unit_id']; } else { if ($exercise->exercise_type & Exercise::$exerciseTypeDistance) { if (!isset($measurement['distance'])) { $valid = false; $invalidProperty = 'distance'; break; } $distance = $measurement['distance']; $distance_unit_id = $measurement['distance_unit_id']; } } if ($exercise->exercise_type & Exercise::$exerciseTypeRepetition) { if (!isset($measurement['reps'])) { $valid = false; $invalidProperty = 'reps'; break; } $reps = $measurement['reps']; } else { if ($exercise->exercise_type & Exercise::$exerciseTypeTime) { if (!isset($measurement['time'])) { $valid = false; $invalidProperty = 'time'; break; } // convert time to seconds $time = $measurement['time']; $interval = \Carbon\CarbonInterval::hours($time['hours'])->minutes($time['minutes'])->seconds($time['seconds']); $time = ExerciseMeasurement::toSeconds($interval); } } ExerciseMeasurement::create(['exercise_group_id' => $exercise_group->id, 'set_order' => $measurement_num, 'weight' => $weight, 'weight_unit_id' => $weight_unit_id, 'distance' => $distance, 'distance_unit_id' => $distance_unit_id, 'reps' => $reps, 'time' => $time]); } } if (!$valid) { $workout->delete(); \Session::flash('workout', 'There was an error creating your workout'); \Session::flash('property', $invalidProperty); } return redirect('/workouts'); }
/** * Save a new workout. * * @param Requests\WorkoutRequest $request * @return mixed */ public function createWorkout(Requests\WorkoutRequest $request) { $workout = \Auth::user()->workouts()->create($request->all()); return $workout; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Requests\WorkoutRequest $request) { $workout = Auth::user()->workouts()->findOrFail($id); $workout->update($request->all()); return back(); }