/**
  *
  * @param Request $request
  * @return Response
  */
 public function store(Request $request)
 {
     $exercise = new Exercise($request->only('name', 'description', 'step_number', 'default_quantity', 'target', 'priority', 'stretch', 'frequency'));
     $exercise->user()->associate(Auth::user());
     $exercise->program()->associate(ExerciseProgram::find($request->get('program_id')));
     $exercise->series()->associate(Series::find($request->get('series_id')));
     $exercise->defaultUnit()->associate(Unit::find($request->get('default_unit_id')));
     $exercise->save();
     $exercise = $this->transform($this->createItem($exercise, new ExerciseTransformer()))['data'];
     return response($exercise, Response::HTTP_CREATED);
 }
 /**
  * Entry can be either just a food, or part of a recipe.
  * When part of a recipe, the store method inserts just one food at a time,
  * so that the store method is RESTful.
  * So lots of ajax requests will be made to insert
  * all the entries for a whole recipe.
  * POST /api/menuEntries
  * @param Request $request
  * @return Response
  */
 public function store(Request $request)
 {
     $entry = new Entry($request->only(['date', 'quantity']));
     $entry->user()->associate(Auth::user());
     $entry->food()->associate(Food::find($request->get('food_id')));
     if ($request->get('recipe_id')) {
         $entry->recipe()->associate(Recipe::find($request->get('recipe_id')));
     }
     $entry->unit()->associate(Unit::find($request->get('unit_id')));
     $entry->save();
     $entry = $this->transform($this->createItem($entry, new MenuEntryTransformer()))['data'];
     return response($entry, Response::HTTP_CREATED);
 }
 public function run()
 {
     Exercise::truncate();
     $this->faker = Faker::create();
     $pushups = [['name' => 'kneeling pushups', 'defaultQuantity' => 20, 'description' => '', 'priority' => 2, 'frequency' => 7], ['name' => 'pushups', 'defaultQuantity' => 10, 'description' => 'hands shoulder width', 'priority' => 1], ['name' => 'one-arm pushups', 'defaultQuantity' => 2, 'description' => 'free hand behind back', 'priority' => 1]];
     $squats = [['name' => 'assisted squats', 'defaultQuantity' => 50, 'description' => 'hold onto something', 'priority' => 3, 'frequency' => 3], ['name' => 'squats', 'defaultQuantity' => 30, 'description' => 'feet shoulder width', 'priority' => 2], ['name' => 'one-legged-squats', 'defaultQuantity' => 5, 'description' => '', 'priority' => 1]];
     $gymnasticRings = [['name' => 'back lever', 'defaultQuantity' => 30, 'description' => '', 'priority' => 1, 'frequency' => 4]];
     $flexibility = [['name' => 'hamstrings', 'defaultQuantity' => 20, 'description' => '', 'priority' => 2, 'stretch' => 1, 'frequency' => 7], ['name' => 'calves', 'defaultQuantity' => 10, 'description' => 'great stretch', 'priority' => 1, 'stretch' => 1]];
     $users = User::all();
     foreach ($users as $user) {
         $this->user = $user;
         $exercise_unit_ids = Unit::where('user_id', $this->user->id)->where('for', 'exercise')->lists('id')->all();
         $this->insertExercisesInSeries($pushups, Unit::find($exercise_unit_ids[0]), Series::where('user_id', $this->user->id)->where('name', 'pushup')->first());
         $this->insertExercisesInSeries($squats, Unit::find($exercise_unit_ids[1]), Series::where('user_id', $this->user->id)->where('name', 'squat')->first());
         $this->insertExercisesInSeries($gymnasticRings, Unit::find($exercise_unit_ids[1]), Series::where('user_id', $this->user->id)->where('name', 'gymnastic rings')->first());
         $this->insertExercisesInSeries($flexibility, Unit::find($exercise_unit_ids[1]), Series::where('user_id', $this->user->id)->where('name', 'flexibility')->first());
     }
 }
 /**
  * Insert an exercise entry.
  * It can be an exercise set.
  * @param Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $exercise = Exercise::find($request->get('exercise_id'));
     if ($request->get('exerciseSet')) {
         // We are inserting an exercise set
         $quantity = $exercise->default_quantity;
         $unit = Unit::find($exercise->default_unit_id);
     } else {
         $quantity = $request->get('quantity');
         $unit = Unit::find($request->get('unit_id'));
     }
     $entry = new Entry(['date' => $request->get('date'), 'quantity' => $quantity]);
     $entry->user()->associate(Auth::user());
     $entry->exercise()->associate($exercise);
     $entry->unit()->associate($unit);
     $entry->save();
     //Return the entries for the day
     $entries = transform(createCollection($this->exerciseEntriesRepository->getEntriesForTheDay($request->get('date')), new ExerciseEntryTransformer()))['data'];
     return response($entries, Response::HTTP_CREATED);
 }
 /**
  *
  * @param Entry $entry
  * @param $index
  */
 private function attachUnit(Entry $entry, $index)
 {
     $unit_ids = collect($entry->food->units)->lists('id')->all();
     $entry->unit()->associate(Unit::find($unit_ids[$index]));
 }
 /**
  *
  * @param $ingredient
  * @return \League\Fractal\Resource\Collection
  */
 public function includeUnit($ingredient)
 {
     return $this->item(Unit::find($ingredient->unit_id), new UnitTransformer());
 }
 /**
  * @test
  * @return void
  */
 public function it_can_delete_an_exercise_entry()
 {
     $this->logInUser();
     $date = Carbon::today()->format('Y-m-d');
     $entry = new Entry(['date' => $date, 'quantity' => 501]);
     $entry->user()->associate($this->user);
     $entry->exercise()->associate(Exercise::find(1));
     $entry->unit()->associate(Unit::find(1));
     $entry->save();
     $this->seeInDatabase('exercise_entries', ['date' => $date, 'exercise_id' => 1, 'quantity' => 501, 'exercise_unit_id' => 1]);
     $response = $this->call('DELETE', '/api/exerciseEntries/' . $entry->id);
     $this->assertEquals(204, $response->getStatusCode());
     $this->missingFromDatabase('exercise_entries', ['date' => $date, 'exercise_id' => 1, 'quantity' => 501, 'exercise_unit_id' => 1]);
     $response = $this->call('DELETE', '/api/exerciseEntries/' . $entry->id);
     $this->assertEquals(404, $response->getStatusCode());
 }
 /**
  * Create the entries for the same exercise but with different units
  * for today, so that I can test out the getSpecificExerciseEntries
  * for a day where the exercise is entered with different units
  */
 private function createEntriesForToday()
 {
     $exercise = Exercise::where('user_id', $this->user->id)->first();
     $date = Carbon::today();
     $this->createEntry(5, $exercise, Unit::find($this->unit_ids[0]), $date);
     $this->createEntry(5, $exercise, Unit::find($this->unit_ids[0]), $date);
     $this->createEntry(10, $exercise, Unit::find($this->unit_ids[1]), $date);
 }