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')
->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);
}
/**

View File

@ -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);
}
/**

View File

@ -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)