mirror of https://github.com/kcal-app/kcal.git
				
				
				
			Add support for sodium and cholesterol tracking
This commit is contained in:
		
							parent
							
								
									e3e84c2e13
								
							
						
					
					
						commit
						3060bdbb7c
					
				|  | @ -9,10 +9,12 @@ use Illuminate\Database\Eloquent\Model; | ||||||
|  * @property int id |  * @property int id | ||||||
|  * @property string name Ingredient base name. |  * @property string name Ingredient base name. | ||||||
|  * @property ?string detail Some additional detail about the ingredient (e.g. "small" with the name "onion"). |  * @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 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 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. |  * @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', |         'name', | ||||||
|         'detail', |         'detail', | ||||||
|         'calories', |         'calories', | ||||||
|         'protein', |  | ||||||
|         'fat', |  | ||||||
|         'carbohydrates', |         'carbohydrates', | ||||||
|  |         'cholesterol', | ||||||
|  |         'fat', | ||||||
|  |         'protein', | ||||||
|  |         'sodium', | ||||||
|         'unitWeight', |         'unitWeight', | ||||||
|         'cupWeight', |         'cupWeight', | ||||||
|     ]; |     ]; | ||||||
|  | @ -39,10 +43,12 @@ class Ingredient extends Model | ||||||
|      */ |      */ | ||||||
|     protected array $casts = [ |     protected array $casts = [ | ||||||
|         'calories' => 'float', |         'calories' => 'float', | ||||||
|         'protein' => 'float', |  | ||||||
|         'fat' => 'float', |  | ||||||
|         'carbohydrates' => 'float', |         'carbohydrates' => 'float', | ||||||
|         'unit_weight' => 'float', |         'cholesterol' => 'float', | ||||||
|         'cup_weight' => 'float', |         'cup_weight' => 'float', | ||||||
|  |         'fat' => 'float', | ||||||
|  |         'protein' => 'float', | ||||||
|  |         'sodium' => 'float', | ||||||
|  |         'unit_weight' => 'float', | ||||||
|     ]; |     ]; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -13,6 +13,12 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||||||
|  * @property int weight Weight of ingredient in full ingredient list (lowest first). |  * @property int weight Weight of ingredient in full ingredient list (lowest first). | ||||||
|  * @property \App\Models\Ingredient ingredient |  * @property \App\Models\Ingredient ingredient | ||||||
|  * @property \App\Models\Recipe recipe |  * @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 | class IngredientAmount extends Model | ||||||
| { | { | ||||||
|  | @ -40,6 +46,18 @@ class IngredientAmount extends Model | ||||||
|      */ |      */ | ||||||
|     protected array $with = ['ingredient']; |     protected array $with = ['ingredient']; | ||||||
| 
 | 
 | ||||||
|  |     /** | ||||||
|  |      * Nutrient calculation methods. | ||||||
|  |      */ | ||||||
|  |     private array $nutrientMethods = [ | ||||||
|  |         'calories', | ||||||
|  |         'carbohydrates', | ||||||
|  |         'cholesterol', | ||||||
|  |         'fat', | ||||||
|  |         'protein', | ||||||
|  |         'sodium', | ||||||
|  |     ]; | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * Get the Ingredient this amount belongs to. |      * 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 { |     public function __call($method, $parameters): mixed { | ||||||
|         return $this->ingredient->calories * $this->unitMultiplier(); |         if (in_array($method, $this->nutrientMethods)) { | ||||||
|  |             return $this->ingredient->{$method} * $this->unitMultiplier(); | ||||||
|         } |         } | ||||||
| 
 |         else { | ||||||
|     /** |             return parent::__call($method, $parameters); | ||||||
|      * 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(); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  |  | ||||||
|  | @ -16,9 +16,11 @@ class CreateIngredientsTable extends Migration | ||||||
|             $table->string('name'); |             $table->string('name'); | ||||||
|             $table->string('detail')->nullable(); |             $table->string('detail')->nullable(); | ||||||
|             $table->unsignedFloat('calories')->default(0); |             $table->unsignedFloat('calories')->default(0); | ||||||
|             $table->unsignedFloat('protein')->default(0); |  | ||||||
|             $table->unsignedFloat('fat')->default(0); |  | ||||||
|             $table->unsignedFloat('carbohydrates')->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('unit_weight')->nullable(); | ||||||
|             $table->unsignedFloat('cup_weight')->nullable(); |             $table->unsignedFloat('cup_weight')->nullable(); | ||||||
|             $table->timestamps(); |             $table->timestamps(); | ||||||
|  |  | ||||||
|  | @ -16,62 +16,62 @@ class IngredientSeeder extends Seeder | ||||||
|             [ |             [ | ||||||
|                 'name' => 'baking powder', |                 'name' => 'baking powder', | ||||||
|                 'calories' => 53, |                 'calories' => 53, | ||||||
|                 'protein' => 0, |  | ||||||
|                 'fat' => 0, |  | ||||||
|                 'carbohydrates' => 27.7, |                 'carbohydrates' => 27.7, | ||||||
|  |                 'sodium' => 10.6, | ||||||
|                 'cup_weight' => 220.8, |                 'cup_weight' => 220.8, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'egg', |                 'name' => 'egg', | ||||||
|                 'detail' => 'large', |                 'detail' => 'large', | ||||||
|                 'calories' => 147, |                 'calories' => 147, | ||||||
|                 'protein' => 12.4, |  | ||||||
|                 'fat' => 9.96, |  | ||||||
|                 'carbohydrates' => 0.96, |                 'carbohydrates' => 0.96, | ||||||
|  |                 'cholesterol' => 0.411, | ||||||
|  |                 'fat' => 9.96, | ||||||
|  |                 'protein' => 12.4, | ||||||
|  |                 'sodium' => 0.129, | ||||||
|                 'unit_weight' => 50.3, |                 'unit_weight' => 50.3, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'flour', |                 'name' => 'flour', | ||||||
|                 'detail' => 'all-purpose', |                 'detail' => 'all-purpose', | ||||||
|                 'calories' => 364, |                 'calories' => 364, | ||||||
|                 'protein' => 10.33, |  | ||||||
|                 'fat' => 0.98, |  | ||||||
|                 'carbohydrates' => 76.31, |                 'carbohydrates' => 76.31, | ||||||
|  |                 'fat' => 0.98, | ||||||
|  |                 'protein' => 10.33, | ||||||
|  |                 'sodium' => 0.004, | ||||||
|                 'cup_weight' => 125, |                 'cup_weight' => 125, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'milk', |                 'name' => 'milk', | ||||||
|                 'detail' => 'whole', |                 'detail' => 'whole', | ||||||
|                 'calories' => 60, |                 'calories' => 60, | ||||||
|                 'protein' => 3.28, |  | ||||||
|                 'fat' => 3.2, |  | ||||||
|                 'carbohydrates' => 4.67, |                 'carbohydrates' => 4.67, | ||||||
|  |                 'cholesterol' => 0.012, | ||||||
|  |                 'fat' => 3.2, | ||||||
|  |                 'protein' => 3.28, | ||||||
|  |                 'sodium' => 0.038, | ||||||
|                 'cup_weight' => 244, |                 'cup_weight' => 244, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'salt', |                 'name' => 'salt', | ||||||
|                 'detail' => 'table', |                 'detail' => 'table', | ||||||
|                 'calories' => 0, |                 'sodium' => 38.758, | ||||||
|                 'protein' => 0, |  | ||||||
|                 'fat' => 0, |  | ||||||
|                 'carbohydrates' => 0, |  | ||||||
|                 'cup_weight' => 292, |                 'cup_weight' => 292, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'sugar', |                 'name' => 'sugar', | ||||||
|                 'detail' => 'white', |                 'detail' => 'white', | ||||||
|                 'calories' => 385, |                 'calories' => 385, | ||||||
|                 'protein' => 0, |  | ||||||
|                 'fat' => 0.32, |  | ||||||
|                 'carbohydrates' => 99.6, |                 'carbohydrates' => 99.6, | ||||||
|  |                 'fat' => 0.32, | ||||||
|  |                 'protein' => 0, | ||||||
|  |                 'sodium' => 0.001, | ||||||
|                 'cup_weight' => 200, |                 'cup_weight' => 200, | ||||||
|             ], |             ], | ||||||
|             [ |             [ | ||||||
|                 'name' => 'vegetable oil', |                 'name' => 'vegetable oil', | ||||||
|                 'calories' => 886, |                 'calories' => 886, | ||||||
|                 'protein' => 0, |  | ||||||
|                 'fat' => 100, |                 'fat' => 100, | ||||||
|                 'carbohydrates' => 0, |  | ||||||
|                 'cup_weight' => 224, |                 'cup_weight' => 224, | ||||||
|             ], |             ], | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue