Add unit support to IngredientAmount

This commit is contained in:
Christopher C. Wells 2020-12-21 20:24:47 -08:00
parent 790e1af828
commit 978a3088c4
5 changed files with 43 additions and 4 deletions

View File

@ -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;

View File

@ -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
};
}
}

View File

@ -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);

View File

@ -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();

View File

@ -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++,
],