Supper gram-based data entry

This commit is contained in:
Christopher C. Wells 2021-01-30 05:50:04 -08:00
parent 7f3ed5b704
commit 87311b9ec4
3 changed files with 16 additions and 24 deletions

View File

@ -70,14 +70,7 @@ class JournalEntryController extends Controller
return view('journal-entries.create') return view('journal-entries.create')
->with('ingredients', $ingredients) ->with('ingredients', $ingredients)
->with('meals', JournalEntry::$meals) ->with('meals', JournalEntry::$meals)
->with('units', [ ->with('units', Nutrients::$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('default_date', Carbon::createFromFormat('Y-m-d', $date)); ->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
} }
@ -88,14 +81,7 @@ class JournalEntryController extends Controller
{ {
return view('journal-entries.create-from-nutrients') return view('journal-entries.create-from-nutrients')
->with('meals', JournalEntry::$meals) ->with('meals', JournalEntry::$meals)
->with('units', [ ->with('units', Nutrients::$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'],
]);
} }
/** /**

View File

@ -10,6 +10,7 @@ use App\Rules\ArrayNotEmpty;
use App\Rules\StringIsDecimalOrFraction; use App\Rules\StringIsDecimalOrFraction;
use App\Rules\UsesIngredientTrait; use App\Rules\UsesIngredientTrait;
use App\Support\Number; use App\Support\Number;
use App\Support\Nutrients;
use Illuminate\Contracts\View\View; use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -128,14 +129,7 @@ class RecipeController extends Controller
->with('recipe', $recipe) ->with('recipe', $recipe)
->with('ingredients', $ingredients) ->with('ingredients', $ingredients)
->with('steps', $steps) ->with('steps', $steps)
->with('ingredients_units', new Collection([ ->with('ingredients_units', Nutrients::$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'],
]));
} }
/** /**

View File

@ -18,6 +18,15 @@ class Nutrients
'protein', '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( public static function calculateFoodNutrientMultiplier(
Food $food, Food $food,
float $amount, float $amount,
@ -29,6 +38,9 @@ class Nutrients
elseif ($fromUnit === 'serving') { elseif ($fromUnit === 'serving') {
return $amount; return $amount;
} }
elseif ($fromUnit === 'gram') {
return $amount / $food->serving_weight;
}
if ( if (
empty($fromUnit) empty($fromUnit)