mirror of https://github.com/kcal-app/kcal.git
Add unit support to IngredientAmount
This commit is contained in:
parent
790e1af828
commit
978a3088c4
|
|
@ -5,6 +5,14 @@ namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
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
|
class Ingredient extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,15 @@ namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
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
|
class IngredientAmount extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
@ -15,6 +23,7 @@ class IngredientAmount extends Model
|
||||||
*/
|
*/
|
||||||
protected array $fillable = [
|
protected array $fillable = [
|
||||||
'amount',
|
'amount',
|
||||||
|
'unit',
|
||||||
'weight',
|
'weight',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
@ -40,7 +49,22 @@ class IngredientAmount extends Model
|
||||||
/**
|
/**
|
||||||
* Get total calories for the ingredient amount.
|
* Get total calories for the ingredient amount.
|
||||||
*/
|
*/
|
||||||
public function calories(): float {
|
#[Pure] public function calories(): float {
|
||||||
return $this->ingredient->calories * $this->amount;
|
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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class CreateIngredientsTable extends Migration
|
||||||
Schema::create('ingredients', function (Blueprint $table) {
|
Schema::create('ingredients', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('unit')->nullable();
|
$table->enum('unit', ['tsp', 'tbsp', 'cup'])->nullable();
|
||||||
$table->unsignedFloat('calories')->default(0);
|
$table->unsignedFloat('calories')->default(0);
|
||||||
$table->unsignedFloat('protein')->default(0);
|
$table->unsignedFloat('protein')->default(0);
|
||||||
$table->unsignedFloat('fat')->default(0);
|
$table->unsignedFloat('fat')->default(0);
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ class CreateIngredientAmountsTable extends Migration
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->foreignIdFor(Ingredient::class);
|
$table->foreignIdFor(Ingredient::class);
|
||||||
$table->unsignedFloat('amount');
|
$table->unsignedFloat('amount');
|
||||||
|
$table->enum('unit', ['tsp', 'tbsp', 'cup'])->nullable();
|
||||||
$table->foreignIdFor(Recipe::class);
|
$table->foreignIdFor(Recipe::class);
|
||||||
$table->unsignedInteger('weight');
|
$table->unsignedInteger('weight');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
|
||||||
|
|
@ -23,13 +23,15 @@ class RecipeSeeder extends Seeder
|
||||||
'ingredient_id' => Ingredient::where('name', 'flour, all-purpose')
|
'ingredient_id' => Ingredient::where('name', 'flour, all-purpose')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 1,
|
'amount' => 1,
|
||||||
|
'unit' => 'cup',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'ingredient_id' => Ingredient::where('name', 'sugar, white')
|
'ingredient_id' => Ingredient::where('name', 'sugar, white')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 0.125,
|
'amount' => 2,
|
||||||
|
'unit' => 'tbsp',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
|
|
@ -37,6 +39,7 @@ class RecipeSeeder extends Seeder
|
||||||
'ingredient_id' => Ingredient::where('name', 'baking powder')
|
'ingredient_id' => Ingredient::where('name', 'baking powder')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 2,
|
'amount' => 2,
|
||||||
|
'unit' => 'tsp',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
|
|
@ -44,6 +47,7 @@ class RecipeSeeder extends Seeder
|
||||||
'ingredient_id' => Ingredient::where('name', 'salt')
|
'ingredient_id' => Ingredient::where('name', 'salt')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 1,
|
'amount' => 1,
|
||||||
|
'unit' => 'tsp',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
|
|
@ -58,6 +62,7 @@ class RecipeSeeder extends Seeder
|
||||||
'ingredient_id' => Ingredient::where('name', 'milk, whole')
|
'ingredient_id' => Ingredient::where('name', 'milk, whole')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 1,
|
'amount' => 1,
|
||||||
|
'unit' => 'cup',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
|
|
@ -65,6 +70,7 @@ class RecipeSeeder extends Seeder
|
||||||
'ingredient_id' => Ingredient::where('name', 'vegetable oil')
|
'ingredient_id' => Ingredient::where('name', 'vegetable oil')
|
||||||
->first()->id,
|
->first()->id,
|
||||||
'amount' => 2,
|
'amount' => 2,
|
||||||
|
'unit' => 'tbsp',
|
||||||
'recipe_id' => $recipe->id,
|
'recipe_id' => $recipe->id,
|
||||||
'weight' => $weight++,
|
'weight' => $weight++,
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue