/**
  * @test
  * @return void
  */
 public function it_can_update_a_unit()
 {
     $this->logInUser();
     $unit = Unit::forCurrentUser()->where('for', 'exercise')->first();
     $response = $this->call('PUT', '/api/exerciseUnits/' . $unit->id, ['name' => 'numbat']);
     $content = json_decode($response->getContent(), true)['data'];
     $this->assertArrayHasKey('id', $content);
     $this->assertArrayHasKey('name', $content);
     $this->assertArrayHasKey('for', $content);
     $this->assertEquals('numbat', $content['name']);
     $this->assertEquals(200, $response->getStatusCode());
 }
 /**
  * @test
  * @return void
  */
 public function it_can_update_a_unit()
 {
     DB::beginTransaction();
     $this->logInUser();
     $unit = Unit::forCurrentUser()->where('for', 'food')->first();
     $response = $this->call('PUT', '/api/foodUnits/' . $unit->id, ['name' => 'numbat']);
     $content = json_decode($response->getContent(), true);
     //dd($content);
     $this->checkFoodUnitKeysExist($content);
     $this->assertEquals('numbat', $content['name']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  * GET /api/units
  * @param Request $request
  * @return Response
  */
 public function index(Request $request)
 {
     $units = Unit::forCurrentUser()->where('for', 'food')->orderBy('name', 'asc')->get();
     $units = $this->transform($this->createCollection($units, new UnitTransformer()))['data'];
     if ($request->get('includeCaloriesForSpecificFood')) {
         //Add the calories for a specific food to each unit, if the calories exist
         foreach ($units as $index => $unit) {
             $calories = DB::table('food_unit')->where('food_id', $request->get('food_id'))->where('unit_id', $unit['id'])->pluck('calories');
             $units[$index]['calories'] = $calories;
         }
     }
     return response($units, Response::HTTP_OK);
 }
 /**
  * @test
  */
 public function it_can_add_an_ingredient_to_a_recipe()
 {
     DB::beginTransaction();
     $this->logInUser();
     $recipe = Recipe::forCurrentUser()->first();
     $food = Food::forCurrentUser()->offset(2)->first();
     $unit = Unit::forCurrentUser()->where('for', 'food')->offset(2)->first();
     $data = ['addIngredient' => true, 'food_id' => $food->id, 'unit_id' => $unit->id, 'quantity' => 9, 'description' => 'blah blah'];
     $foodCount = count($recipe->foods);
     $response = $this->call('PUT', '/api/recipes/' . $recipe->id, $data);
     $content = json_decode($response->getContent(), true);
     //        dd($content);
     $this->seeInDatabase('food_recipe', ['food_id' => $food->id, 'unit_id' => $unit->id, 'quantity' => 9, 'description' => 'blah blah']);
     $this->assertCount($foodCount + 1, $recipe->foods()->get());
     $this->checkRecipeKeysExist($content);
     $this->checkIngredientKeysExist($content['ingredients']['data'][0]);
     $this->assertCount($foodCount + 1, $content['ingredients']['data']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  *
  * @param $name
  * @return Unit
  */
 public function findOrInsertUnitIfNotExists($name)
 {
     $unit = Unit::forCurrentUser()->where('name', $name)->first();
     if (!$unit) {
         $unit = $this->unitsRepository->insert($name);
     }
     return $unit;
 }
 /**
  *
  * @return mixed
  */
 public function getExerciseUnits()
 {
     return Unit::forCurrentUser()->where('for', 'exercise')->orderBy('name', 'asc')->get();
 }
 /**
  * @test
  * @return void
  */
 public function it_can_update_a_food()
 {
     DB::beginTransaction();
     $this->logInUser();
     $food = Food::forCurrentUser()->first();
     $defaultUnit = Unit::forCurrentUser()->where('for', 'food')->where('id', '!=', $food->defaultUnit->id)->first();
     $unitIds = Unit::forCurrentUser()->where('for', 'food')->limit(2)->offset(1)->lists('id')->all();
     $response = $this->call('PUT', '/api/foods/' . $food->id, ['name' => 'numbat', 'default_unit_id' => $defaultUnit->id, 'unit_ids' => $unitIds]);
     $content = json_decode($response->getContent(), true);
     //        dd($content);
     $this->checkFoodKeysExist($content);
     $this->assertEquals('numbat', $content['name']);
     $this->assertEquals($unitIds, $content['unitIds']);
     $this->assertEquals($defaultUnit->id, $content['defaultUnit']['data']['id']);
     $this->assertEquals(200, $response->getStatusCode());
     DB::rollBack();
 }
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router  $router
  * @return void
  */
 public function boot(Router $router)
 {
     parent::boot($router);
     Route::bind('exercises', function ($id) {
         return Exercise::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseEntries', function ($id) {
         return ExerciseEntry::forCurrentUser()->findOrFail($id);
     });
     Route::bind('menuEntries', function ($id) {
         return MenuEntry::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseSeries', function ($id) {
         return ExerciseSeries::forCurrentUser()->findOrFail($id);
     });
     Route::bind('foods', function ($id) {
         return Food::forCurrentUser()->findOrFail($id);
     });
     Route::bind('recipes', function ($id) {
         return Recipe::forCurrentUser()->findOrFail($id);
     });
     Route::bind('exerciseTags', function ($id) {
         return Tag::forCurrentUser()->where('for', 'exercise')->findOrFail($id);
     });
     Route::bind('recipeTags', function ($id) {
         return Tag::forCurrentUser()->where('for', 'recipe')->findOrFail($id);
     });
     Route::bind('foodUnits', function ($id) {
         return Unit::forCurrentUser()->where('for', 'food')->findOrFail($id);
     });
     Route::bind('exerciseUnits', function ($id) {
         return Unit::forCurrentUser()->where('for', 'exercise')->findOrFail($id);
     });
     Route::bind('timers', function ($id) {
         return Timer::forCurrentUser()->findOrFail($id);
     });
     Route::bind('activities', function ($id) {
         return Activity::forCurrentUser()->findOrFail($id);
     });
     Route::bind('weights', function ($idOrDate) {
         if (strrpos($idOrDate, '-')) {
             //parameter is the date of the entry
             $weight = Weight::forCurrentUser()->where('date', $idOrDate)->first();
         } else {
             //parameter is the id of the entry
             $weight = Weight::forCurrentUser()->findOrFail($idOrDate);
         }
         return $weight;
     });
     /**
      * $parameter is either the id or the date
      */
     Route::bind('journal', function ($parameter) {
         /**
          * @VP:
          * Is there a better way to check if the $parameter is an
          * id or a date? When I tried using Carbon to create an object from
          * the parameter, it threw an exception when the $parameter was the id,
          * whereas I just wanted a boolean.
          */
         if (strrpos($parameter, '-')) {
             //$parameter is the date of the entry
             $journal = Journal::forCurrentUser()->where('date', $parameter)->first();
         } else {
             //$parameter is the id of the entry
             $journal = Journal::forCurrentUser()->findOrFail($parameter);
         }
         return $journal;
     });
 }