/** * * @param $ingredient * @return \League\Fractal\Resource\Collection */ public function includeFood($ingredient) { /** * @VP: * Including the units here isn't working. */ $food = $this->item(Food::find($ingredient->food_id), new FoodTransformer(['units' => true])); return $food; }
/** * 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); }
/** * Transform food response * @param Food $food * @return array */ public function transform(Food $food) { $array = ['id' => $food->id, 'name' => $food->name, 'path' => $food->path, 'defaultCalories' => $food->getDefaultCalories(), 'unitIds' => $food->units()->lists('unit_id')]; /** * @VP: * I'm doing this here so I can include the units from my IngredientTransformer. * What's the proper way of doing this? * Actually, $array['units'] is an empty array here. Why? * It should be populated, and if I dd what includeUnits returns, * the data looks correct. */ if (isset($this->params['units'])) { $array['units'] = $this->includeUnits($food); } // if ($food->default_unit_id) { // $array['defaultUnit'] = [ // 'id' => $food->defaultUnit->id, // 'name' => $food->defaultUnit->name // ]; // $array['defaultUnit'] = $food->defaultUnit; // $array['defaultUnit'] = $this->includeDefaultUnit($food); // } return $array; }
/** * */ private function createRecipes() { foreach (Config::get('recipes') as $tempRecipe) { $recipe = new Recipe(['name' => $tempRecipe['name']]); $recipe->user()->associate($this->user); $recipe->save(); foreach ($tempRecipe['ingredients'] as $ingredient) { $food = Food::where('user_id', $this->user->id)->where('name', $ingredient['food'])->first(); $recipe->foods()->attach([$food->id => ['unit_id' => Unit::where('user_id', $this->user->id)->where('name', $ingredient['unit'])->first()->id, 'quantity' => $ingredient['quantity'], 'description' => $ingredient['description']]]); } foreach ($tempRecipe['tags'] as $tempTag) { $tag = Tag::where('user_id', $this->user->id)->where('name', $tempTag)->first(); $recipe->tags()->attach([$tag->id => ['taggable_type' => 'recipe']]); } $recipe->save(); } }
/** * @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 $typing * @return mixed */ private function foods($typing) { $foods = Food::where('user_id', Auth::user()->id)->where('name', 'LIKE', $typing)->with('defaultUnit')->with('units')->get(); return $foods; }
/** * Get recipe contents and steps. * Contents should include the foods that belong to the recipe, * along with the description, quantity, and unit * for the food when used in the recipe (from food_recipe table), * and with the tags for the recipe. * Redoing after refactor. Still need description, quantity, unit. * @param $recipe * @return array */ public function getRecipeInfo($recipe) { $recipe = transform(createItem($recipe, new RecipeWithIngredientsTransformer()))['data']; //For some reason the units for each food aren't being added to the food //from my IngredientTransformer, so add them here foreach ($recipe['ingredients']['data'] as $index => $ingredient) { $units = Food::find($ingredient['food']['data']['id'])->units; $units = transform(createCollection($units, new UnitTransformer())); $recipe['ingredients']['data'][$index]['food']['data']['units'] = $units; } return $recipe; }
/** * * @param Food $food * @param $tempFood */ private function attachDefaultUnit(Food $food, $tempFood) { $defaultUnit = Unit::where('user_id', $this->user->id)->where('name', $tempFood['defaultUnit'])->first(); $food->defaultUnit()->associate($defaultUnit); }
/** * Get all foods, along with their default unit, default calories, * and all their units. * Also, add the calories for each food's units. Todo? * @return mixed */ public function getFoods() { $foods = Food::forCurrentUser()->with('defaultUnit')->orderBy('foods.name', 'asc')->get(); $foods = transform(createCollection($foods, new FoodTransformer())); return $foods['data']; }
/** * * @param Entry $entry * @param $index */ private function attachFood(Entry $entry, $index) { $food_ids = Food::where('user_id', $this->user->id)->lists('id')->all(); $entry->food()->associate(Food::find($food_ids[$index])); }
/** * DELETE api/foods/{foods} * @param Food $food * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Food $food) { $food->delete(); return $this->responseNoContent(); }
/** * @test * @return void */ public function it_can_delete_a_food() { $this->logInUser(); $food = new Food(['name' => 'echidna']); $food->user()->associate($this->user); $food->save(); $this->seeInDatabase('foods', ['name' => 'echidna']); $response = $this->call('DELETE', '/api/foods/' . $food->id); $this->assertEquals(204, $response->getStatusCode()); $this->missingFromDatabase('foods', ['name' => 'echidna']); $response = $this->call('DELETE', '/api/foods/' . $food->id); $this->assertEquals(404, $response->getStatusCode()); }
/** * 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; }); }