diff --git a/app/Models/Ingredient.php b/app/Models/Ingredient.php index 60946b7..4e255d9 100644 --- a/app/Models/Ingredient.php +++ b/app/Models/Ingredient.php @@ -5,6 +5,14 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +/** + * @property string name + * @property string unit + * @property float calories + * @property float protein + * @property float fat + * @property float carbohydrates + */ class Ingredient extends Model { use HasFactory; diff --git a/app/Models/IngredientAmount.php b/app/Models/IngredientAmount.php index 02d97d0..af366ab 100644 --- a/app/Models/IngredientAmount.php +++ b/app/Models/IngredientAmount.php @@ -5,7 +5,15 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use JetBrains\PhpStorm\Pure; +/** + * @property float amount + * @property float unit + * @property int weight + * @property \App\Models\Ingredient ingredient + * @property \App\Models\Recipe recipe + */ class IngredientAmount extends Model { use HasFactory; @@ -15,6 +23,7 @@ class IngredientAmount extends Model */ protected array $fillable = [ 'amount', + 'unit', 'weight', ]; @@ -40,7 +49,22 @@ class IngredientAmount extends Model /** * Get total calories for the ingredient amount. */ - public function calories(): float { - return $this->ingredient->calories * $this->amount; + #[Pure] public function calories(): float { + return $this->ingredient->calories * $this->amount * $this->unitMultiplier(); + } + + /** + * Get the multiplier for the ingredient unit and ingredient amount unit. + */ + private function unitMultiplier(): float { + return match (true) { + $this->ingredient->unit === 'tsp' && $this->unit === 'tbsp' => 3, + $this->ingredient->unit === 'tsp' && $this->unit === 'cup' => 48, + $this->ingredient->unit === 'tbsp' && $this->unit === 'tsp' => 1/3, + $this->ingredient->unit === 'tbsp' && $this->unit === 'cup' => 16, + $this->ingredient->unit === 'cup' && $this->unit === 'tsp' => 1/48, + $this->ingredient->unit === 'cup' && $this->unit === 'tbsp' => 1/16, + default => 1 + }; } } diff --git a/database/migrations/2020_12_21_214128_create_ingredients_table.php b/database/migrations/2020_12_21_214128_create_ingredients_table.php index f805fff..b49a1f9 100644 --- a/database/migrations/2020_12_21_214128_create_ingredients_table.php +++ b/database/migrations/2020_12_21_214128_create_ingredients_table.php @@ -16,7 +16,7 @@ class CreateIngredientsTable extends Migration Schema::create('ingredients', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('unit')->nullable(); + $table->enum('unit', ['tsp', 'tbsp', 'cup'])->nullable(); $table->unsignedFloat('calories')->default(0); $table->unsignedFloat('protein')->default(0); $table->unsignedFloat('fat')->default(0); diff --git a/database/migrations/2020_12_21_215527_create_ingredient_amounts_table.php b/database/migrations/2020_12_21_215527_create_ingredient_amounts_table.php index 2951c2b..dfe74d6 100644 --- a/database/migrations/2020_12_21_215527_create_ingredient_amounts_table.php +++ b/database/migrations/2020_12_21_215527_create_ingredient_amounts_table.php @@ -19,6 +19,7 @@ class CreateIngredientAmountsTable extends Migration $table->id(); $table->foreignIdFor(Ingredient::class); $table->unsignedFloat('amount'); + $table->enum('unit', ['tsp', 'tbsp', 'cup'])->nullable(); $table->foreignIdFor(Recipe::class); $table->unsignedInteger('weight'); $table->timestamps(); diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index 550c8fa..eab3cc3 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -23,13 +23,15 @@ class RecipeSeeder extends Seeder 'ingredient_id' => Ingredient::where('name', 'flour, all-purpose') ->first()->id, 'amount' => 1, + 'unit' => 'cup', 'recipe_id' => $recipe->id, 'weight' => $weight++, ], [ 'ingredient_id' => Ingredient::where('name', 'sugar, white') ->first()->id, - 'amount' => 0.125, + 'amount' => 2, + 'unit' => 'tbsp', 'recipe_id' => $recipe->id, 'weight' => $weight++, ], @@ -37,6 +39,7 @@ class RecipeSeeder extends Seeder 'ingredient_id' => Ingredient::where('name', 'baking powder') ->first()->id, 'amount' => 2, + 'unit' => 'tsp', 'recipe_id' => $recipe->id, 'weight' => $weight++, ], @@ -44,6 +47,7 @@ class RecipeSeeder extends Seeder 'ingredient_id' => Ingredient::where('name', 'salt') ->first()->id, 'amount' => 1, + 'unit' => 'tsp', 'recipe_id' => $recipe->id, 'weight' => $weight++, ], @@ -58,6 +62,7 @@ class RecipeSeeder extends Seeder 'ingredient_id' => Ingredient::where('name', 'milk, whole') ->first()->id, 'amount' => 1, + 'unit' => 'cup', 'recipe_id' => $recipe->id, 'weight' => $weight++, ], @@ -65,6 +70,7 @@ class RecipeSeeder extends Seeder 'ingredient_id' => Ingredient::where('name', 'vegetable oil') ->first()->id, 'amount' => 2, + 'unit' => 'tbsp', 'recipe_id' => $recipe->id, 'weight' => $weight++, ],