Add support for sodium and cholesterol tracking

This commit is contained in:
Christopher C. Wells 2020-12-22 19:55:33 -08:00
parent e3e84c2e13
commit 3060bdbb7c
4 changed files with 67 additions and 50 deletions

View File

@ -9,10 +9,12 @@ use Illuminate\Database\Eloquent\Model;
* @property int id
* @property string name Ingredient base name.
* @property ?string detail Some additional detail about the ingredient (e.g. "small" with the name "onion").
* @property float calories (per 100g).
* @property float protein (per 100g).
* @property float fat (per 100g).
* @property float carbohydrates (per 100g).
* @property float calories (per 100g).
* @property float cholesterol (per 100g).
* @property float fat (per 100g).
* @property float protein (per 100g).
* @property float sodium (per 100g).
* @property ?float unit_weight Weight of one cup of the ingredient.
* @property ?float cup_weight Weight of one "unit" (e.g. an egg, onion, etc.) of the ingredient.
*/
@ -27,9 +29,11 @@ class Ingredient extends Model
'name',
'detail',
'calories',
'protein',
'fat',
'carbohydrates',
'cholesterol',
'fat',
'protein',
'sodium',
'unitWeight',
'cupWeight',
];
@ -39,10 +43,12 @@ class Ingredient extends Model
*/
protected array $casts = [
'calories' => 'float',
'protein' => 'float',
'fat' => 'float',
'carbohydrates' => 'float',
'unit_weight' => 'float',
'cholesterol' => 'float',
'cup_weight' => 'float',
'fat' => 'float',
'protein' => 'float',
'sodium' => 'float',
'unit_weight' => 'float',
];
}

View File

@ -13,6 +13,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property int weight Weight of ingredient in full ingredient list (lowest first).
* @property \App\Models\Ingredient ingredient
* @property \App\Models\Recipe recipe
* @method float calories Get total calories.
* @method float carbohydrates Get total carbohydrates.
* @method float cholesterol Get total cholesterol.
* @method float fat Get total fat.
* @method float protein Get total protein.
* @method float sodium Get total sodium.
*/
class IngredientAmount extends Model
{
@ -40,6 +46,18 @@ class IngredientAmount extends Model
*/
protected array $with = ['ingredient'];
/**
* Nutrient calculation methods.
*/
private array $nutrientMethods = [
'calories',
'carbohydrates',
'cholesterol',
'fat',
'protein',
'sodium',
];
/**
* Get the Ingredient this amount belongs to.
*/
@ -55,31 +73,22 @@ class IngredientAmount extends Model
}
/**
* Get total calories for the ingredient amount.
* Add nutrient calculations handling overloading.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*
* @noinspection PhpMissingParamTypeInspection
*/
public function calories(): float {
return $this->ingredient->calories * $this->unitMultiplier();
}
/**
* Get total protein for the ingredient amount.
*/
public function protein(): float {
return $this->ingredient->protein * $this->unitMultiplier();
}
/**
* Get total fat for the ingredient amount.
*/
public function fat(): float {
return $this->ingredient->fat * $this->unitMultiplier();
}
/**
* Get total carbohydrates for the ingredient amount.
*/
public function carbohydrates(): float {
return $this->ingredient->carbohydrates * $this->unitMultiplier();
public function __call($method, $parameters): mixed {
if (in_array($method, $this->nutrientMethods)) {
return $this->ingredient->{$method} * $this->unitMultiplier();
}
else {
return parent::__call($method, $parameters);
}
}
/**

View File

@ -16,9 +16,11 @@ class CreateIngredientsTable extends Migration
$table->string('name');
$table->string('detail')->nullable();
$table->unsignedFloat('calories')->default(0);
$table->unsignedFloat('protein')->default(0);
$table->unsignedFloat('fat')->default(0);
$table->unsignedFloat('carbohydrates')->default(0);
$table->unsignedFloat('cholesterol')->default(0);
$table->unsignedFloat('fat')->default(0);
$table->unsignedFloat('protein')->default(0);
$table->unsignedFloat('sodium')->default(0);
$table->unsignedFloat('unit_weight')->nullable();
$table->unsignedFloat('cup_weight')->nullable();
$table->timestamps();

View File

@ -16,62 +16,62 @@ class IngredientSeeder extends Seeder
[
'name' => 'baking powder',
'calories' => 53,
'protein' => 0,
'fat' => 0,
'carbohydrates' => 27.7,
'sodium' => 10.6,
'cup_weight' => 220.8,
],
[
'name' => 'egg',
'detail' => 'large',
'calories' => 147,
'protein' => 12.4,
'fat' => 9.96,
'carbohydrates' => 0.96,
'cholesterol' => 0.411,
'fat' => 9.96,
'protein' => 12.4,
'sodium' => 0.129,
'unit_weight' => 50.3,
],
[
'name' => 'flour',
'detail' => 'all-purpose',
'calories' => 364,
'protein' => 10.33,
'fat' => 0.98,
'carbohydrates' => 76.31,
'fat' => 0.98,
'protein' => 10.33,
'sodium' => 0.004,
'cup_weight' => 125,
],
[
'name' => 'milk',
'detail' => 'whole',
'calories' => 60,
'protein' => 3.28,
'fat' => 3.2,
'carbohydrates' => 4.67,
'cholesterol' => 0.012,
'fat' => 3.2,
'protein' => 3.28,
'sodium' => 0.038,
'cup_weight' => 244,
],
[
'name' => 'salt',
'detail' => 'table',
'calories' => 0,
'protein' => 0,
'fat' => 0,
'carbohydrates' => 0,
'sodium' => 38.758,
'cup_weight' => 292,
],
[
'name' => 'sugar',
'detail' => 'white',
'calories' => 385,
'protein' => 0,
'fat' => 0.32,
'carbohydrates' => 99.6,
'fat' => 0.32,
'protein' => 0,
'sodium' => 0.001,
'cup_weight' => 200,
],
[
'name' => 'vegetable oil',
'calories' => 886,
'protein' => 0,
'fat' => 100,
'carbohydrates' => 0,
'cup_weight' => 224,
],
];