/** * @test * @return void */ public function it_can_delete_a_recipe() { $this->logInUser(); $recipe = new Recipe(['name' => 'echidna']); $recipe->user()->associate($this->user); $recipe->save(); $this->seeInDatabase('recipes', ['name' => 'echidna']); $response = $this->call('DELETE', '/api/recipes/' . $recipe->id); $this->assertEquals(204, $response->getStatusCode()); $this->missingFromDatabase('recipes', ['name' => 'echidna']); $response = $this->call('DELETE', '/api/recipes/' . $recipe->id); $this->assertEquals(404, $response->getStatusCode()); }
/** * Delete the existing method before adding the updated method * Todo: fix this after refactor * @param Request $request * @return array */ public function updateRecipeMethod(Request $request) { $recipe = Recipe::find($request->get('recipe_id')); $steps = $request->get('steps'); RecipeMethod::deleteRecipeMethod($recipe); RecipeMethod::insertRecipeMethod($recipe, $steps); return Recipe::getRecipeInfo($recipe); }
/** * Attach recipe for the last entry if the date is today * @param $date * @param Entry $entry * @param $index */ private function attachRecipe($date, Entry $entry, $index) { if ($date === Carbon::today()->format('Y-m-d') && $index === 1) { $recipe_ids = Recipe::where('user_id', $this->user->id)->lists('id')->all(); if ($recipe_ids) { $entry->recipe()->associate(Recipe::find($recipe_ids[0])); } } }
/** * 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() { RecipeMethod::truncate(); $faker = Faker::create(); $users = User::all(); foreach ($users as $user) { $recipe_ids = Recipe::where('user_id', $user->id)->lists('id'); foreach ($recipe_ids as $recipe_id) { // $counter = 0; foreach (range(1, 5) as $index) { // $counter++; DB::table('recipe_methods')->insert(['recipe_id' => $recipe_id, 'step' => $index, 'text' => $faker->sentence, 'user_id' => $user->id]); } } } }
/** * @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(); }
/** * */ 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(); } }
/** * * @param $typing * @return mixed */ private function recipes($typing) { $recipes = Recipe::where('user_id', Auth::user()->id)->where('name', 'LIKE', $typing)->select('id', 'name')->get(); return $recipes; }
/** * Get all recipes, along with their tags, for the user, and that match the $name filter * @param $name * @param $tag_ids * @return array */ public function filterRecipes($name, $tag_ids) { $recipes = Recipe::forCurrentUser(); //filter by name if ($name !== '') { $name = '%' . $name . '%'; $recipes = $recipes->where('name', 'LIKE', $name); } //filter by tags if (count($tag_ids) > 0) { foreach ($tag_ids as $tag_id) { $recipes = $recipes->whereHas('tags', function ($q) use($tag_id) { $q->where('tags.id', $tag_id); }); } } $recipes = $recipes->with('tags')->orderBy('name', 'asc')->get(); return transform(createCollection($recipes, new RecipeTransformer()))['data']; }
/** * * @param Recipe $recipe * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Recipe $recipe) { $recipe->delete(); return $this->responseNoContent(); }
/** * * @param Recipe $recipe * @return \League\Fractal\Resource\Collection */ public function includeIngredients(Recipe $recipe) { $ingredients = $recipe->getIngredients(); return createCollection($ingredients, new IngredientTransformer()); }
/** * 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; }); }