From 87311b9ec47c6719422c1b0cf8ccca0c6103ecd9 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Sat, 30 Jan 2021 05:50:04 -0800 Subject: [PATCH] Supper gram-based data entry --- .../Controllers/JournalEntryController.php | 18 ++---------------- app/Http/Controllers/RecipeController.php | 10 ++-------- app/Support/Nutrients.php | 12 ++++++++++++ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 59b5d1a..f71a185 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -70,14 +70,7 @@ class JournalEntryController extends Controller return view('journal-entries.create') ->with('ingredients', $ingredients) ->with('meals', JournalEntry::$meals) - ->with('units', [ - ['value' => 'tsp', 'label' => 'tsp.'], - ['value' => 'tbsp', 'label' => 'tbsp.'], - ['value' => 'cup', 'label' => 'cup'], - ['value' => 'oz', 'label' => 'oz'], - ['value' => 'g', 'label' => 'grams'], - ['value' => 'serving', 'label' => 'servings'], - ]) + ->with('units', Nutrients::$units) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } @@ -88,14 +81,7 @@ class JournalEntryController extends Controller { return view('journal-entries.create-from-nutrients') ->with('meals', JournalEntry::$meals) - ->with('units', [ - ['value' => 'tsp', 'label' => 'tsp.'], - ['value' => 'tbsp', 'label' => 'tbsp.'], - ['value' => 'cup', 'label' => 'cup'], - ['value' => 'oz', 'label' => 'oz'], - ['value' => 'g', 'label' => 'grams'], - ['value' => 'serving', 'label' => 'servings'], - ]); + ->with('units', Nutrients::$units); } /** diff --git a/app/Http/Controllers/RecipeController.php b/app/Http/Controllers/RecipeController.php index fca807a..6506dc8 100644 --- a/app/Http/Controllers/RecipeController.php +++ b/app/Http/Controllers/RecipeController.php @@ -10,6 +10,7 @@ use App\Rules\ArrayNotEmpty; use App\Rules\StringIsDecimalOrFraction; use App\Rules\UsesIngredientTrait; use App\Support\Number; +use App\Support\Nutrients; use Illuminate\Contracts\View\View; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -128,14 +129,7 @@ class RecipeController extends Controller ->with('recipe', $recipe) ->with('ingredients', $ingredients) ->with('steps', $steps) - ->with('ingredients_units', new Collection([ - ['value' => 'tsp', 'label' => 'tsp.'], - ['value' => 'tbsp', 'label' => 'tbsp.'], - ['value' => 'cup', 'label' => 'cup'], - ['value' => 'oz', 'label' => 'oz'], - ['value' => 'g', 'label' => 'grams'], - ['value' => 'serving', 'label' => 'servings'], - ])); + ->with('ingredients_units', Nutrients::$units); } /** diff --git a/app/Support/Nutrients.php b/app/Support/Nutrients.php index 2a26e15..eec783f 100644 --- a/app/Support/Nutrients.php +++ b/app/Support/Nutrients.php @@ -18,6 +18,15 @@ class Nutrients 'protein', ]; + public static array $units = [ + ['value' => 'tsp', 'label' => 'tsp.'], + ['value' => 'tbsp', 'label' => 'tbsp.'], + ['value' => 'cup', 'label' => 'cup'], + ['value' => 'oz', 'label' => 'oz'], + ['value' => 'gram', 'label' => 'grams'], + ['value' => 'serving', 'label' => 'servings'], + ]; + public static function calculateFoodNutrientMultiplier( Food $food, float $amount, @@ -29,6 +38,9 @@ class Nutrients elseif ($fromUnit === 'serving') { return $amount; } + elseif ($fromUnit === 'gram') { + return $amount / $food->serving_weight; + } if ( empty($fromUnit)