Improve general relevancy of database seeder

This commit is contained in:
Christopher C. Wells 2021-04-07 16:36:46 -07:00
parent 6920d21896
commit 3521f36134
10 changed files with 50 additions and 499 deletions

View File

@ -22,22 +22,22 @@ class FoodFactory extends Factory
public function definition(): array
{
return [
'name' => Words::randomWords(Arr::random(['n', 'an'])),
'name' => Words::randomWords(Arr::random(['n', 'an']), TRUE),
'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_size' => $this->faker->numberBetween(1, 3),
'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,
'calories' => $this->faker->randomFloat(1, 0, 100),
'fat' => $this->faker->randomFloat(1, 0, 10),
'cholesterol' => $this->faker->randomFloat(1, 0, 100),
'sodium' => $this->faker->randomFloat(1, 0, 500),
'carbohydrates' => $this->faker->randomFloat(1, 0, 20),
'protein' => $this->faker->randomFloat(1, 0, 20),
'tags' => Words::randomWords(Arr::random(['a', 'aa', 'aaa'])),
];
}

View File

@ -6,6 +6,7 @@ use App\Models\Food;
use App\Models\IngredientAmount;
use App\Models\Recipe;
use App\Support\Nutrients;
use Database\Support\Words;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
@ -37,7 +38,7 @@ class IngredientAmountFactory extends Factory
'ingredient_type' => $ingredient_type,
'amount' => $this->faker->randomFloat(1, 1/3, 5),
'unit' => $ingredient_unit,
'detail' => $this->faker->optional(0.8)->realText(),
'detail' => $this->faker->boolean() ?: Words::randomWords('a'),
'weight' => $this->faker->numberBetween(0, 50),
'parent_id' => Recipe::factory(),
'parent_type' => Recipe::class,

View File

@ -22,12 +22,12 @@ class JournalEntryFactory extends Factory
'user_id' => 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),
'calories' => $this->faker->randomFloat(1, 0, 500),
'fat' => $this->faker->randomFloat(1, 0, 50),
'cholesterol' => $this->faker->randomFloat(1, 0, 2000),
'sodium' => $this->faker->randomFloat(1, 0, 2000),
'carbohydrates' => $this->faker->randomFloat(1, 0, 100),
'protein' => $this->faker->randomFloat(1, 0, 100),
'meal' => $this->faker->randomElement(['breakfast', 'lunch', 'dinner', 'snacks']),
];
}

View File

@ -24,7 +24,7 @@ class RecipeFactory extends Factory
{
$description = htmlspecialchars($this->faker->realText(500));
return [
'name' => Words::randomWords(Arr::random(['npan', 'npn', 'anpn'])),
'name' => Words::randomWords(Arr::random(['npan', 'npn', 'anpn']), TRUE),
'description' => "<p>{$description}</p>",
'description_delta' => '{"ops":[{"insert":"' . $description . '\n"}]}"',
'time_prep' => $this->faker->numberBetween(0, 20),
@ -32,7 +32,7 @@ class RecipeFactory extends Factory
'source' => $this->faker->optional()->url,
'servings' => $this->faker->numberBetween(1, 10),
'weight' => $this->faker->randomFloat(1, 60, 2000),
'tags' => $this->faker->words,
'tags' => Words::randomWords(Arr::random(['a', 'aa', 'aaa'])),
];
}

View File

@ -2,7 +2,14 @@
namespace Database\Seeders;
use App\Models\Food;
use App\Models\Goal;
use App\Models\JournalEntry;
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class DatabaseSeeder extends Seeder
{
@ -11,9 +18,22 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
$this->call(UserSeeder::class);
$this->call(FoodSeeder::class);
$this->call(RecipeSeeder::class);
$this->call(JournalEntrySeeder::class);
$user = User::factory()->create([
'username' => 'kcal',
'password' => Hash::make('kcal'),
'name' => 'Admin',
'remember_token' => Str::random(10),
]);
// @todo Make this more fine tuned for different macros.
Goal::factory()->for($user)->count(5)->create();
Food::factory()->count(100)->create();
// @todo Create with media.
Recipe::factory()
->hasIngredientAmounts(rand(2, 20))
->hasSteps(rand(5, 20))
->hasIngredientSeparators(rand(0, 5))
->count(50)
->create();
JournalEntry::factory()->for($user)->count(100)->create();
}
}

View File

@ -1,162 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\Food;
use Illuminate\Database\Seeder;
class FoodSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$default_foods = [
[
'name' => '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);
}
}

View File

@ -1,128 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\Food;
use App\Models\JournalEntry;
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class JournalEntrySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
/** @var \App\Models\Food[] $foods */
foreach (['peanut butter', 'milk', 'egg', 'raisins'] as $name) {
$foods[$name] = Food::where('name', $name)->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,
]);
}
}

View File

@ -1,159 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\Food;
use App\Models\IngredientAmount;
use App\Models\Recipe;
use App\Models\RecipeStep;
use Illuminate\Database\Seeder;
class RecipeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
/** @var \App\Models\Recipe $recipe */
$recipe = Recipe::factory()->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);
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::factory()->create([
'username' => 'admin',
'password' => Hash::make('admin'),
'name' => 'Admin',
'remember_token' => Str::random(10),
]);
}
}

View File

@ -47,10 +47,10 @@ class Words
* - p: preposition, and
* - v: verb.
*/
public static function randomWords(string $format = 'an'): string {
$name = [];
public static function randomWords(string $format = 'an', $asText = false): array|string {
$words = [];
foreach (str_split($format) as $type) {
$name[] = match ($type) {
$words[] = match ($type) {
'a' => self::adjectives()->random(),
'n' => self::nouns()->random(),
'p' => self::prepositions()->random(),
@ -58,7 +58,10 @@ class Words
default => NULL
};
}
return implode(' ', $name);
if ($asText) {
$words = implode(' ', $words);
}
return $words;
}
}