mirror of https://github.com/kcal-app/kcal.git
Remove remaining FoodAmount references
This commit is contained in:
parent
670d6127ca
commit
1c420bc624
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,127 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Support\Number;
|
||||
use App\Support\Nutrients;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* App\Models\FoodAmount
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $food_id
|
||||
* @property float $amount
|
||||
* @property string|null $unit
|
||||
* @property int $recipe_id
|
||||
* @property int $weight
|
||||
* @property \Illuminate\Support\Carbon|null $created_at
|
||||
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||
* @property string|null $detail
|
||||
* @property-read \App\Models\Food $food
|
||||
* @property-read \App\Models\Recipe $recipe
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount newModelQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount newQuery()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount query()
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereAmount($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereDetail($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereFoodId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereRecipeId($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereUnit($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Eloquent\Builder|FoodAmount whereWeight($value)
|
||||
* @mixin \Eloquent
|
||||
* @property-read string $amount_formatted
|
||||
*/
|
||||
class FoodAmount extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $fillable = [
|
||||
'amount',
|
||||
'unit',
|
||||
'detail',
|
||||
'weight',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast.
|
||||
*/
|
||||
protected $casts = [
|
||||
'amount' => '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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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.
|
|
@ -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 = [
|
||||
[
|
||||
|
|
Loading…
Reference in New Issue