/** * * * */ public function generateLogFromWK(Request $request) { $planExercises = PlanExercise::with('planSets')->where('plan_workout_id', '=', $request->input('id'))->get(); foreach ($planExercises as $planExercise) { $session = new Session(); $session->exercise_id = $planExercise->exercise->id; $session->session_date = Carbon::now(); Auth::user()->sessions()->save($session); foreach ($planExercise->planSets as $planSet) { $sessionSet = new SessionSet(); $sessionSet->session_id = $session->id; $sessionSet->number_of_reps = $planSet->expected_reps; $sessionSet->weight_lifted = $planSet->expected_weight; $sessionSet->one_rep_max = $sessionSet->weight_lifted * 36 / (37 - $sessionSet->number_of_reps); $sessionSet->failed = 0; $sessionSet->save(); $exercise = Auth::user()->exercises()->findOrFail($sessionSet->session->exercise_id); $bestOneRepMax = SessionSet::join('sessions', 'sessions.id', '=', 'session_sets.session_id')->where('sessions.exercise_id', '=', $exercise->id)->max('one_rep_max'); $exercise->update(['best_one_rep_max' => $bestOneRepMax]); } } return redirect()->action('LogController@showLog'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Requests\SessionSetRequest $request) { $sessionSet = SessionSet::with('session.exercise')->findOrFail($id); $sessionSet->one_rep_max = $this->calculateOneRepMax($request->weight_lifted, $request->number_of_reps); // If the user is updating the weight and reps with lower values, // then they likely failed the set (especially if the set was // generated from a plan). However, if it was not generated from a plan // this could be an incorrect assumption... if ($request->weight_lifted < $sessionSet->weight_lifted or $request->number_of_reps < $sessionSet->number_of_reps) { $sessionSet->update(['failed' => 1]); } $sessionSet->update($request->all()); // refactor this code; create function in ExerciseController to update exercise best_one_rep_max $exercise = Auth::user()->exercises()->findOrFail($sessionSet->session->exercise_id); $bestOneRepMax = SessionSet::join('sessions', 'sessions.id', '=', 'session_sets.session_id')->where('sessions.exercise_id', '=', $exercise->id)->max('one_rep_max'); $exercise->update(['best_one_rep_max' => $bestOneRepMax]); return back()->with('status', 'Successful Update!'); }