From 461796fe21ed3f53ba06295e721cabbcc51e8aef Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Fri, 22 Jan 2021 23:57:41 -0800 Subject: [PATCH] Remove remaining FoodAmount references --- app/Http/Controllers/FoodController.php | 2 +- app/Models/Food.php | 11 +- app/Models/FoodAmount.php | 127 ------------------ app/Models/Recipe.php | 4 +- app/Models/Traits/Ingredient.php | 7 + ...actory.php => IngredientAmountFactory.php} | 6 +- database/seeders/RecipeSeeder.php | 64 +++++---- 7 files changed, 56 insertions(+), 165 deletions(-) delete mode 100644 app/Models/FoodAmount.php rename database/factories/{FoodAmountFactory.php => IngredientAmountFactory.php} (74%) diff --git a/app/Http/Controllers/FoodController.php b/app/Http/Controllers/FoodController.php index 22fc980..604e8a0 100644 --- a/app/Http/Controllers/FoodController.php +++ b/app/Http/Controllers/FoodController.php @@ -103,7 +103,7 @@ class FoodController extends Controller */ public function destroy(Food $food): RedirectResponse { - if ($food->foodAmounts()->count()) { + if ($food->ingredientAmountChildren()->count()) { return back()->withErrors('Cannot delete: this food is used in recipes.'); } $food->delete(); diff --git a/app/Models/Food.php b/app/Models/Food.php index 86f6539..1bf1b9c 100644 --- a/app/Models/Food.php +++ b/app/Models/Food.php @@ -29,8 +29,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property float $protein * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\FoodAmount[] $foodAmounts - * @property-read int|null $food_amounts_count * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\JournalEntry[] $journalEntries * @property-read int|null $journal_entries_count * @method static \Illuminate\Database\Eloquent\Builder|Food findSimilarSlugs(string $attribute, array $config, string $slug) @@ -57,6 +55,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property-read string $serving_size_formatted * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmounts * @property-read int|null $ingredient_amounts_count + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren + * @property-read int|null $ingredient_amount_children_count */ class Food extends Model { @@ -111,11 +111,4 @@ class Food extends Model return Number::fractionStringFromFloat($this->serving_size); } - /** - * Get the food amounts using this food. - */ - public function foodAmounts(): HasMany { - return $this->hasMany(FoodAmount::class); - } - } diff --git a/app/Models/FoodAmount.php b/app/Models/FoodAmount.php deleted file mode 100644 index 85e32bf..0000000 --- a/app/Models/FoodAmount.php +++ /dev/null @@ -1,127 +0,0 @@ - 'float', - 'weight' => 'int', - ]; - - /** - * @inheritdoc - */ - protected $with = ['food']; - - /** - * Nutrient calculation methods. - */ - private array $nutrientMethods = [ - 'calories', - 'carbohydrates', - 'cholesterol', - 'fat', - 'protein', - 'sodium', - ]; - - /** - * @inheritdoc - */ - protected $appends = ['amount_formatted']; - - /** - * Get the amount as a formatted string (e.g. 0.5 = 1/2). - */ - public function getAmountFormattedAttribute(): string { - return Number::fractionStringFromFloat($this->amount); - } - - /** - * Get the Food this amount belongs to. - */ - public function food(): BelongsTo { - return $this->belongsTo(Food::class); - } - - /** - * Get the Recipe this amount belongs to. - */ - public function recipe(): BelongsTo { - return $this->belongsTo(Recipe::class); - } - - /** - * Add nutrient calculations handling to overloading. - * - * @param string $method - * @param array $parameters - * - * @return mixed - * - * @noinspection PhpMissingParamTypeInspection - */ - public function __call($method, $parameters): mixed { - if (in_array($method, $this->nutrientMethods)) { - return $this->food->{$method} * Nutrients::calculateFoodNutrientMultiplier( - $this->food, - $this->amount, - $this->unit - ); - } - else { - return parent::__call($method, $parameters); - } - } -} diff --git a/app/Models/Recipe.php b/app/Models/Recipe.php index 64f35cd..a77575f 100644 --- a/app/Models/Recipe.php +++ b/app/Models/Recipe.php @@ -21,8 +21,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property \Illuminate\Support\Carbon|null $updated_at * @property string|null $description * @property string|null $source - * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\FoodAmount[] $foodAmounts - * @property-read int|null $food_amounts_count * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\JournalEntry[] $journalEntries * @property-read int|null $journal_entries_count * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\RecipeStep[] $steps @@ -44,6 +42,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; * @property-read int|null $ingredient_amounts_count * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredients * @property-read int|null $ingredients_count + * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IngredientAmount[] $ingredientAmountChildren + * @property-read int|null $ingredient_amount_children_count */ class Recipe extends Model { diff --git a/app/Models/Traits/Ingredient.php b/app/Models/Traits/Ingredient.php index d07912d..07e05ef 100644 --- a/app/Models/Traits/Ingredient.php +++ b/app/Models/Traits/Ingredient.php @@ -8,6 +8,13 @@ use Illuminate\Support\Collection; trait Ingredient { + /** + * Get all of the ingredient amounts associated with the ingredient. + */ + public function ingredientAmountChildren(): MorphToMany { + return $this->morphToMany(IngredientAmount::class, 'ingredient'); + } + /** * Gets search results for a term. */ diff --git a/database/factories/FoodAmountFactory.php b/database/factories/IngredientAmountFactory.php similarity index 74% rename from database/factories/FoodAmountFactory.php rename to database/factories/IngredientAmountFactory.php index 0ad8d10..4735222 100644 --- a/database/factories/FoodAmountFactory.php +++ b/database/factories/IngredientAmountFactory.php @@ -2,17 +2,17 @@ namespace Database\Factories; -use App\Models\FoodAmount; +use App\Models\IngredientAmount; use Illuminate\Database\Eloquent\Factories\Factory; -class FoodAmountFactory extends Factory +class IngredientAmountFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ - protected $model = FoodAmount::class; + protected $model = IngredientAmount::class; /** * Define the model's default state. diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index cc45e8b..aa296c3 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -3,7 +3,7 @@ namespace Database\Seeders; use App\Models\Food; -use App\Models\FoodAmount; +use App\Models\IngredientAmount; use App\Models\Recipe; use App\Models\RecipeStep; use Illuminate\Database\Seeder; @@ -25,62 +25,76 @@ class RecipeSeeder extends Seeder $weight = 0; $amounts = [ [ - 'food_id' => Food::where('name', 'flour') + 'ingredient_id' => Food::where('name', 'flour') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 4.25, 'unit' => 'oz', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'sugar') + 'ingredient_id' => Food::where('name', 'sugar') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 2, 'unit' => 'tbsp', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'baking powder') + 'ingredient_id' => Food::where('name', 'baking powder') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 2, 'unit' => 'tsp', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'salt') + 'ingredient_id' => Food::where('name', 'salt') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 1, 'unit' => 'tsp', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'egg') + 'ingredient_id' => Food::where('name', 'egg') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 1, - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'milk') + 'ingredient_id' => Food::where('name', 'milk') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 1, 'unit' => 'cup', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'vegetable oil') + 'ingredient_id' => Food::where('name', 'vegetable oil') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 2, 'unit' => 'tbsp', - 'recipe_id' => $recipe->id, - 'weight' => $weight++, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight, ], ]; - FoodAmount::factory()->createMany($amounts); + IngredientAmount::factory()->createMany($amounts); $steps = [ [ @@ -106,23 +120,27 @@ class RecipeSeeder extends Seeder $weight = 0; $amounts = [ [ - 'food_id' => Food::where('name', 'peanut butter') + 'ingredient_id' => Food::where('name', 'peanut butter') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 2, 'unit' => 'cup', - 'recipe_id' => $recipe->id, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, 'weight' => $weight++, ], [ - 'food_id' => Food::where('name', 'canned corn') + 'ingredient_id' => Food::where('name', 'canned corn') ->first()->id, + 'ingredient_type' => Food::class, 'amount' => 15.25, 'unit' => 'oz', - 'recipe_id' => $recipe->id, - 'weight' => $weight++, + 'parent_id' => $recipe->id, + 'parent_type' => Recipe::class, + 'weight' => $weight, ], ]; - FoodAmount::factory()->createMany($amounts); + IngredientAmount::factory()->createMany($amounts); $steps = [ [