From 6920d21896d3d59f0c474a42c1b43a352147b439 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 7 Apr 2021 08:58:20 -0700 Subject: [PATCH] Re-add Seeders and Factories with correct case --- database/Factories/FoodFactory.php | 95 ++++++++++ database/Factories/GoalFactory.php | 30 ++++ .../Factories/IngredientAmountFactory.php | 72 ++++++++ database/Factories/JournalEntryFactory.php | 35 ++++ database/Factories/RecipeFactory.php | 54 ++++++ database/Factories/RecipeSeparatorFactory.php | 26 +++ database/Factories/RecipeStepFactory.php | 25 +++ database/Factories/TagFactory.php | 25 +++ database/Factories/UserFactory.php | 29 ++++ database/Seeders/DatabaseSeeder.php | 19 ++ database/Seeders/FoodSeeder.php | 162 ++++++++++++++++++ database/Seeders/JournalEntrySeeder.php | 128 ++++++++++++++ database/Seeders/RecipeSeeder.php | 159 +++++++++++++++++ database/Seeders/UserSeeder.php | 24 +++ 14 files changed, 883 insertions(+) create mode 100644 database/Factories/FoodFactory.php create mode 100644 database/Factories/GoalFactory.php create mode 100644 database/Factories/IngredientAmountFactory.php create mode 100644 database/Factories/JournalEntryFactory.php create mode 100644 database/Factories/RecipeFactory.php create mode 100644 database/Factories/RecipeSeparatorFactory.php create mode 100644 database/Factories/RecipeStepFactory.php create mode 100644 database/Factories/TagFactory.php create mode 100644 database/Factories/UserFactory.php create mode 100644 database/Seeders/DatabaseSeeder.php create mode 100644 database/Seeders/FoodSeeder.php create mode 100644 database/Seeders/JournalEntrySeeder.php create mode 100644 database/Seeders/RecipeSeeder.php create mode 100644 database/Seeders/UserSeeder.php diff --git a/database/Factories/FoodFactory.php b/database/Factories/FoodFactory.php new file mode 100644 index 0000000..0c14307 --- /dev/null +++ b/database/Factories/FoodFactory.php @@ -0,0 +1,95 @@ + Words::randomWords(Arr::random(['n', 'an'])), + 'detail' => $this->faker->optional()->sentence(2), + 'brand' => $this->faker->optional()->word, + 'source' => $this->faker->optional()->url, + 'notes' => $this->faker->optional(0.25)->paragraph, + 'serving_size' => $this->faker->randomFloat(2, 1/2, 5), + 'serving_unit' => $this->faker->randomElement(['tsp', 'tbsp', 'cup']), + 'serving_weight' => $this->faker->numberBetween(5, 500), + 'serving_unit_name' => $this->faker->optional(0.25)->word, + 'calories' => $this->faker->randomFloat(2, 0, 100), + 'fat' => $this->faker->randomFloat(2, 0, 10), + 'cholesterol' => $this->faker->randomFloat(2, 0, 100), + 'sodium' => $this->faker->randomFloat(2, 0, 500), + 'carbohydrates' => $this->faker->randomFloat(2, 0, 20), + 'protein' => $this->faker->randomFloat(2, 0, 20), + 'tags' => $this->faker->words, + ]; + } + + /** + * Define a "tsp" serving unit. + */ + public function tspServingUnit() + { + return $this->state(function (array $attributes) { + return [ + 'serving_unit' => 'tsp', + 'serving_size' => 1, + ]; + }); + } + + /** + * Define a "tbsp" serving unit. + */ + public function tbspServingUnit() + { + return $this->state(function (array $attributes) { + return [ + 'serving_unit' => 'tbsp', + 'serving_size' => 1, + ]; + }); + } + + /** + * Define a "cup" serving unit. + */ + public function cupServingUnit() + { + return $this->state(function (array $attributes) { + return [ + 'serving_unit' => 'cup', + 'serving_size' => 1, + ]; + }); + } + + /** + * Define no serving unit. + */ + public function noServingUnit() + { + return $this->state(function (array $attributes) { + return [ + 'serving_unit' => null, + 'serving_unit_name' => 'head' + ]; + }); + } +} diff --git a/database/Factories/GoalFactory.php b/database/Factories/GoalFactory.php new file mode 100644 index 0000000..2878c7b --- /dev/null +++ b/database/Factories/GoalFactory.php @@ -0,0 +1,30 @@ +faker->dateTimeThisMonth; + $to = $this->faker->dateTimeBetween($from, '+1 year'); + return [ + 'from' => $this->faker->randomElement([$from, null]), + 'to' => $this->faker->randomElement([$to, null]), + 'frequency' => $this->faker->randomElement(Goal::$frequencyOptions)['value'], + 'name' => $this->faker->randomElement(Goal::getNameOptions())['value'], + 'goal' => $this->faker->numberBetween(0, 2000), + ]; + } +} diff --git a/database/Factories/IngredientAmountFactory.php b/database/Factories/IngredientAmountFactory.php new file mode 100644 index 0000000..2e97342 --- /dev/null +++ b/database/Factories/IngredientAmountFactory.php @@ -0,0 +1,72 @@ +faker->boolean(90)) { + $ingredient_factory = Food::factory(); + $ingredient_type = Food::class; + $ingredient_unit = Nutrients::units()->pluck('value')->random(1)->first(); + } + else { + $ingredient_factory = Recipe::factory(); + $ingredient_type = Recipe::class; + $ingredient_unit = 'serving'; + } + + return [ + 'ingredient_id' => $ingredient_factory, + 'ingredient_type' => $ingredient_type, + 'amount' => $this->faker->randomFloat(1, 1/3, 5), + 'unit' => $ingredient_unit, + 'detail' => $this->faker->optional(0.8)->realText(), + 'weight' => $this->faker->numberBetween(0, 50), + 'parent_id' => Recipe::factory(), + 'parent_type' => Recipe::class, + ]; + } + + /** + * Define a specific parent. + */ + public function parent(Model $parent): static + { + return $this->state(function (array $attributes) use ($parent) { + return [ + 'parent_id' => $parent->id, + 'parent_type' => $parent::class, + ]; + }); + } + + /** + * Define a specific ingredient. + */ + public function ingredient(Model $ingredient): static + { + return $this->state(function (array $attributes) use ($ingredient) { + return [ + 'ingredient_id' => $ingredient->id, + 'ingredient_type' => $ingredient::class, + ]; + }); + } +} diff --git a/database/Factories/JournalEntryFactory.php b/database/Factories/JournalEntryFactory.php new file mode 100644 index 0000000..3f4e188 --- /dev/null +++ b/database/Factories/JournalEntryFactory.php @@ -0,0 +1,35 @@ + User::factory(), + 'date' => $this->faker->dateTimeThisMonth, + 'summary' => $this->faker->realText(50), + 'calories' => $this->faker->randomFloat(2, 0, 500), + 'fat' => $this->faker->randomFloat(2, 0, 50), + 'cholesterol' => $this->faker->randomFloat(2, 0, 2000), + 'sodium' => $this->faker->randomFloat(2, 0, 2000), + 'carbohydrates' => $this->faker->randomFloat(2, 0, 100), + 'protein' => $this->faker->randomFloat(2, 0, 100), + 'meal' => $this->faker->randomElement(['breakfast', 'lunch', 'dinner', 'snacks']), + ]; + } + +} diff --git a/database/Factories/RecipeFactory.php b/database/Factories/RecipeFactory.php new file mode 100644 index 0000000..ad2b21c --- /dev/null +++ b/database/Factories/RecipeFactory.php @@ -0,0 +1,54 @@ +faker->realText(500)); + return [ + 'name' => Words::randomWords(Arr::random(['npan', 'npn', 'anpn'])), + 'description' => "

{$description}

", + 'description_delta' => '{"ops":[{"insert":"' . $description . '\n"}]}"', + 'time_prep' => $this->faker->numberBetween(0, 20), + 'time_cook' => $this->faker->numberBetween(0, 90), + 'source' => $this->faker->optional()->url, + 'servings' => $this->faker->numberBetween(1, 10), + 'weight' => $this->faker->randomFloat(1, 60, 2000), + 'tags' => $this->faker->words, + ]; + } + + /** + * Create a recipe and add fake media to it. + */ + public function createOneWithMedia(array $attributes = []): Recipe { + Storage::fake('tests'); + /** @var \App\Models\Recipe $recipe */ + $recipe = $this->createOne($attributes); + $file = UploadedFile::fake()->image('recipe.jpg', 1600, 900); + $path = $file->store('tests'); + $recipe->addMediaFromDisk($path) + ->usingName($recipe->name) + ->usingFileName("{$recipe->slug}.jpg") + ->toMediaCollection(); + return $recipe; + } +} diff --git a/database/Factories/RecipeSeparatorFactory.php b/database/Factories/RecipeSeparatorFactory.php new file mode 100644 index 0000000..db1ec52 --- /dev/null +++ b/database/Factories/RecipeSeparatorFactory.php @@ -0,0 +1,26 @@ + 'ingredients', + 'weight' => $this->faker->numberBetween(0, 100), + 'text' => $this->faker->optional()->realText(20), + ]; + } +} diff --git a/database/Factories/RecipeStepFactory.php b/database/Factories/RecipeStepFactory.php new file mode 100644 index 0000000..1b45723 --- /dev/null +++ b/database/Factories/RecipeStepFactory.php @@ -0,0 +1,25 @@ + $this->faker->numberBetween(1, 50), + 'step' => $this->faker->realText(500), + ]; + } +} diff --git a/database/Factories/TagFactory.php b/database/Factories/TagFactory.php new file mode 100644 index 0000000..40712ef --- /dev/null +++ b/database/Factories/TagFactory.php @@ -0,0 +1,25 @@ + Words::randomWords('a'), + ]; + } +} diff --git a/database/Factories/UserFactory.php b/database/Factories/UserFactory.php new file mode 100644 index 0000000..f2d1bb8 --- /dev/null +++ b/database/Factories/UserFactory.php @@ -0,0 +1,29 @@ + $this->faker->unique()->userName, + 'password' => Hash::make('password'), + 'name' => $this->faker->name, + 'remember_token' => Str::random(10), + ]; + } +} diff --git a/database/Seeders/DatabaseSeeder.php b/database/Seeders/DatabaseSeeder.php new file mode 100644 index 0000000..8c378cc --- /dev/null +++ b/database/Seeders/DatabaseSeeder.php @@ -0,0 +1,19 @@ +call(UserSeeder::class); + $this->call(FoodSeeder::class); + $this->call(RecipeSeeder::class); + $this->call(JournalEntrySeeder::class); + } +} diff --git a/database/Seeders/FoodSeeder.php b/database/Seeders/FoodSeeder.php new file mode 100644 index 0000000..36ac983 --- /dev/null +++ b/database/Seeders/FoodSeeder.php @@ -0,0 +1,162 @@ + 'baking powder', + 'serving_size' => 1, + 'serving_unit' => 'tsp', + 'serving_weight' => 4.6, + 'calories' => 2.44, + 'fat' => 0, + 'cholesterol' => 0, + 'sodium' => 0.488, + 'carbohydrates' => 1.27, + 'protein' => 0, + ], + [ + 'name' => 'egg', + 'detail' => 'large', + 'serving_size' => 1, + 'serving_weight' => 50.3, + 'calories' => 71.9, + 'fat' => 5.01, + 'cholesterol' => 0.207, + 'sodium' => 0.0649, + 'carbohydrates' => 0.483, + 'protein' => 6.24, + ], + [ + 'name' => 'flour', + 'detail' => 'all-purpose', + 'serving_size' => 1, + 'serving_unit' => 'cup', + 'serving_weight' => 125, + 'calories' => 455, + 'fat' => 1.22, + 'cholesterol' => 0, + 'sodium' => 0.0025, + 'carbohydrates' => 95.4, + 'protein' => 12.9, + ], + [ + 'name' => 'milk', + 'detail' => 'whole', + 'serving_size' => 1, + 'serving_unit' => 'cup', + 'serving_weight' => 244, + 'calories' => 146, + 'fat' => 7.81, + 'cholesterol' => 0.0293, + 'sodium' => 0.0927, + 'carbohydrates' => 11.4, + 'protein' => 8, + ], + [ + 'name' => 'salt', + 'detail' => 'table', + 'serving_size' => 1, + 'serving_unit' => 'tsp', + 'serving_weight' => 6, + 'calories' => 0, + 'fat' => 0, + 'cholesterol' => 0, + 'sodium' => 2.33, + 'carbohydrates' => 0, + 'protein' => 0, + + ], + [ + 'name' => 'sugar', + 'detail' => 'white', + 'serving_size' => 1, + 'serving_unit' => 'cup', + 'serving_weight' => 200, + 'calories' => 770, + 'fat' => 0.64, + 'cholesterol' => 0, + 'sodium' => 0.002, + 'carbohydrates' => 199, + 'protein' => 0, + ], + [ + 'name' => 'vegetable oil', + 'serving_size' => 1, + 'serving_unit' => 'tbsp', + 'serving_weight' => 14, + 'calories' => 124, + 'fat' => 14, + 'cholesterol' => 0, + 'sodium' => 0, + 'carbohydrates' => 0, + 'protein' => 0, + ], + [ + 'name' => 'peanut butter', + 'detail' => 'organic creamy', + 'brand' => 'Kirkland', + 'serving_size' => 2, + 'serving_unit' => 'tbsp', + 'serving_weight' => 32, + 'calories' => 180, + 'fat' => 15, + 'cholesterol' => 0, + 'sodium' => 0.065, + 'carbohydrates' => 7, + 'protein' => 8, + ], + [ + 'name' => 'raisins', + 'brand' => 'Kroger', + 'serving_size' => 0.25, + 'serving_unit' => 'cup', + 'serving_weight' => 40, + 'calories' => 140, + 'fat' => 0, + 'cholesterol' => 0, + 'sodium' => 0.010, + 'carbohydrates' => 33, + 'protein' => 1, + ], + [ + 'name' => 'peanuts', + 'detail' => 'dry roasted, unsalted', + 'brand' => 'Kroger', + 'serving_size' => 0.25, + 'serving_unit' => 'cup', + 'serving_weight' => 28, + 'calories' => 160, + 'fat' => 14, + 'cholesterol' => 0, + 'sodium' => 0, + 'carbohydrates' => 6, + 'protein' => 7, + ], + [ + 'name' => 'canned corn', + 'detail' => 'golden sweet', + 'brand' => 'WinCo', + 'serving_size' => 0.5, + 'serving_unit' => 'cup', + 'serving_weight' => 125, + 'calories' => 60, + 'fat' => 0.5, + 'sodium' => 0.2, + 'carbohydrates' => 9, + 'protein' => 1, + ], + ]; + Food::factory()->createMany($default_foods); + } +} diff --git a/database/Seeders/JournalEntrySeeder.php b/database/Seeders/JournalEntrySeeder.php new file mode 100644 index 0000000..4cf0b85 --- /dev/null +++ b/database/Seeders/JournalEntrySeeder.php @@ -0,0 +1,128 @@ +first(); + } + + /** @var \App\Models\Recipe $recipe */ + $recipe = Recipe::all()->first(); + + /** @var \App\Models\User $user */ + $user = User::all()->first; + + $default_entries = [ + [ + 'user_id' => $user->id, + 'date' => Carbon::now()->toDateString(), + 'summary' => '2 egg, 1 serving milk', + 'calories' => $foods['egg']->calories * 2 + $foods['milk']->calories, + 'fat' => $foods['egg']->fat * 2 + $foods['milk']->fat, + 'cholesterol' => $foods['egg']->cholesterol * 2 + $foods['milk']->cholesterol, + 'sodium' => $foods['egg']->sodium * 2 + $foods['milk']->sodium, + 'carbohydrates' => $foods['egg']->carbohydrates * 2 + $foods['milk']->carbohydrates, + 'protein' => $foods['egg']->protein * 2 + $foods['milk']->protein, + 'meal' => 'breakfast', + ], + [ + 'user_id' => $user->id, + 'date' => Carbon::now()->toDateString(), + 'summary' => '1 serving pancakes', + 'calories' => $recipe->caloriesPerServing(), + 'fat' => $recipe->fatPerServing(), + 'cholesterol' => $recipe->cholesterolPerServing(), + 'sodium' => $recipe->sodiumPerServing(), + 'carbohydrates' => $recipe->carbohydratesPerServing(), + 'protein' => $recipe->proteinPerServing(), + 'meal' => 'lunch', + ], + [ + 'user_id' => $user->id, + 'date' => Carbon::now()->toDateString(), + 'summary' => '1 serving pancakes', + 'calories' => $recipe->caloriesPerServing(), + 'fat' => $recipe->fatPerServing(), + 'cholesterol' => $recipe->cholesterolPerServing(), + 'sodium' => $recipe->sodiumPerServing(), + 'carbohydrates' => $recipe->carbohydratesPerServing(), + 'protein' => $recipe->proteinPerServing(), + 'meal' => 'dinner', + ], + [ + 'user_id' => $user->id, + 'date' => Carbon::now()->toDateString(), + 'summary' => '1 serving peanut butter', + 'calories' => $foods['peanut butter']->calories, + 'fat' => $foods['peanut butter']->fat, + 'cholesterol' => $foods['peanut butter']->cholesterol, + 'sodium' => $foods['peanut butter']->sodium, + 'carbohydrates' => $foods['peanut butter']->carbohydrates, + 'protein' => $foods['peanut butter']->protein, + 'meal' => 'snacks', + ], + [ + 'user_id' => $user->id, + 'date' => Carbon::now()->toDateString(), + 'summary' => '2 servings raisins', + 'calories' => $foods['raisins']->calories * 2, + 'fat' => $foods['raisins']->fat * 2, + 'cholesterol' => $foods['raisins']->cholesterol * 2, + 'sodium' => $foods['raisins']->sodium * 2, + 'carbohydrates' => $foods['raisins']->carbohydrates * 2, + 'protein' => $foods['raisins']->protein * 2, + 'meal' => 'snacks', + ], + ]; + JournalEntry::factory()->createMany($default_entries); + + DB::table('journalables')->insert([ + 'journal_entry_id' => 1, + 'journalable_id' => $foods['egg']->id, + 'journalable_type' => Food::class, + ]); + DB::table('journalables')->insert([ + 'journal_entry_id' => 1, + 'journalable_id' => $foods['milk']->id, + 'journalable_type' => Food::class, + ]); + DB::table('journalables')->insert([ + 'journal_entry_id' => 2, + 'journalable_id' => $recipe->id, + 'journalable_type' => Recipe::class, + ]); + DB::table('journalables')->insert([ + 'journal_entry_id' => 3, + 'journalable_id' => $recipe->id, + 'journalable_type' => Recipe::class, + ]); + DB::table('journalables')->insert([ + 'journal_entry_id' => 4, + 'journalable_id' => $foods['peanut butter']->id, + 'journalable_type' => Food::class, + ]); + DB::table('journalables')->insert([ + 'journal_entry_id' => 5, + 'journalable_id' => $foods['raisins']->id, + 'journalable_type' => Food::class, + ]); + } +} diff --git a/database/Seeders/RecipeSeeder.php b/database/Seeders/RecipeSeeder.php new file mode 100644 index 0000000..aa296c3 --- /dev/null +++ b/database/Seeders/RecipeSeeder.php @@ -0,0 +1,159 @@ +create([ + 'name' => 'pancakes', + 'description' => 'Basic pancakes in two steps.', + 'servings' => 4, + ]); + + $weight = 0; + $amounts = [ + [ + 'ingredient_id' => Food::where('name', 'flour') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 4.25, + 'unit' => 'oz', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'sugar') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 2, + 'unit' => 'tbsp', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'baking powder') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 2, + 'unit' => 'tsp', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'salt') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 1, + 'unit' => 'tsp', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'egg') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 1, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'milk') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 1, + 'unit' => 'cup', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'vegetable oil') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 2, + 'unit' => 'tbsp', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight, + ], + ]; + IngredientAmount::factory()->createMany($amounts); + + $steps = [ + [ + 'recipe_id' => $recipe->id, + 'number' => 1, + 'step' => 'In a large bowl, mix flour, sugar, baking powder and salt. Make a well in the center, and pour in milk, egg and oil. Mix until smooth.', + ], + [ + 'recipe_id' => $recipe->id, + 'number' => 2, + 'step' => 'Heat a lightly oiled griddle or frying pan over medium high heat. Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake. Brown on both sides and serve hot.', + ] + ]; + RecipeStep::factory()->createMany($steps); + + /** @var \App\Models\Recipe $recipe */ + $recipe = Recipe::factory()->create([ + 'name' => 'peanut butter corn', + 'description' => 'Peanut butter and corn -- YUM', + 'servings' => 4, + ]); + + $weight = 0; + $amounts = [ + [ + 'ingredient_id' => Food::where('name', 'peanut butter') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 2, + 'unit' => 'cup', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight++, + ], + [ + 'ingredient_id' => Food::where('name', 'canned corn') + ->first()->id, + 'ingredient_type' => Food::class, + 'amount' => 15.25, + 'unit' => 'oz', + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight, + ], + ]; + IngredientAmount::factory()->createMany($amounts); + + $steps = [ + [ + 'recipe_id' => $recipe->id, + 'number' => 1, + 'step' => 'Mix it together.', + ], + [ + 'recipe_id' => $recipe->id, + 'number' => 2, + 'step' => 'Eat it.', + ] + ]; + RecipeStep::factory()->createMany($steps); + } +} diff --git a/database/Seeders/UserSeeder.php b/database/Seeders/UserSeeder.php new file mode 100644 index 0000000..7f209ea --- /dev/null +++ b/database/Seeders/UserSeeder.php @@ -0,0 +1,24 @@ +create([ + 'username' => 'admin', + 'password' => Hash::make('admin'), + 'name' => 'Admin', + 'remember_token' => Str::random(10), + ]); + } +}