From d4951300379e2a8a149f595932c9f0bcfce92e90 Mon Sep 17 00:00:00 2001 From: "Christopher C. Wells" Date: Wed, 26 May 2021 06:06:03 -0700 Subject: [PATCH] Change journal entry meal field to integer --- .../Controllers/JournalEntryController.php | 4 +- .../StoreFromNutrientsJournalEntryRequest.php | 8 +-- .../Requests/StoreJournalEntryRequest.php | 6 +- app/Rules/ArrayNotEmpty.php | 5 +- ..._change_meal_to_int_in_journal_entries.php | 56 +++++++++++++++++++ .../create-from-nutrients.blade.php | 2 +- .../views/journal-entries/index.blade.php | 8 +-- .../partials/entry-item-input.blade.php | 2 +- 8 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 database/migrations/2021_05_26_052938_change_meal_to_int_in_journal_entries.php diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index baf4613..b054552 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -122,7 +122,7 @@ class JournalEntryController extends Controller return view('journal-entries.create') ->with('ingredients', $ingredients) - ->with('meals', JournalEntry::meals()->toArray()) + ->with('meals', Auth::user()->meals) ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } @@ -134,7 +134,7 @@ class JournalEntryController extends Controller { $date = $request->date ?? Carbon::now()->toDateString(); return view('journal-entries.create-from-nutrients') - ->with('meals', JournalEntry::meals()->toArray()) + ->with('meals', Auth::user()->meals) ->with('units', Nutrients::units()->toArray()) ->with('default_date', Carbon::createFromFormat('Y-m-d', $date)); } diff --git a/app/Http/Requests/StoreFromNutrientsJournalEntryRequest.php b/app/Http/Requests/StoreFromNutrientsJournalEntryRequest.php index 8fe450a..a0e9ae8 100644 --- a/app/Http/Requests/StoreFromNutrientsJournalEntryRequest.php +++ b/app/Http/Requests/StoreFromNutrientsJournalEntryRequest.php @@ -2,12 +2,9 @@ namespace App\Http\Requests; -use App\Models\JournalEntry; -use App\Rules\ArrayNotEmpty; use App\Rules\InArray; -use App\Rules\StringIsPositiveDecimalOrFraction; -use App\Rules\UsesIngredientTrait; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Support\Facades\Auth; class StoreFromNutrientsJournalEntryRequest extends FormRequest { @@ -21,8 +18,7 @@ class StoreFromNutrientsJournalEntryRequest extends FormRequest 'date' => ['required', 'date'], 'meal' => [ 'required', - 'string', - new InArray(JournalEntry::meals()->pluck('value')->toArray()) + new InArray(Auth::user()->meals->pluck('value')->toArray()) ], 'summary' => ['required', 'string'], 'calories' => ['nullable', 'numeric', 'min:0', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein'], diff --git a/app/Http/Requests/StoreJournalEntryRequest.php b/app/Http/Requests/StoreJournalEntryRequest.php index 3d55538..5933cbe 100644 --- a/app/Http/Requests/StoreJournalEntryRequest.php +++ b/app/Http/Requests/StoreJournalEntryRequest.php @@ -8,6 +8,7 @@ use App\Rules\InArray; use App\Rules\StringIsPositiveDecimalOrFraction; use App\Rules\UsesIngredientTrait; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Support\Facades\Auth; class StoreJournalEntryRequest extends FormRequest { @@ -22,10 +23,9 @@ class StoreJournalEntryRequest extends FormRequest 'ingredients.date.*' => ['nullable', 'date', 'required_with:ingredients.id.*'], 'ingredients.meal' => ['required', 'array', new ArrayNotEmpty], 'ingredients.meal.*' => [ - 'nullable', - 'string', + 'required', 'required_with:ingredients.id.*', - new InArray(JournalEntry::meals()->pluck('value')->toArray()) + new InArray(Auth::user()->meals->pluck('value')->toArray()) ], 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], 'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsPositiveDecimalOrFraction], diff --git a/app/Rules/ArrayNotEmpty.php b/app/Rules/ArrayNotEmpty.php index f584c16..a682e90 100644 --- a/app/Rules/ArrayNotEmpty.php +++ b/app/Rules/ArrayNotEmpty.php @@ -11,7 +11,10 @@ class ArrayNotEmpty implements Rule */ public function passes($attribute, $value): bool { - return !empty(array_filter($value)); + return !empty(array_filter($value, function ($value) { + // Allow other "empty-y" values like false and 0. + return $value !== null; + })); } /** diff --git a/database/migrations/2021_05_26_052938_change_meal_to_int_in_journal_entries.php b/database/migrations/2021_05_26_052938_change_meal_to_int_in_journal_entries.php new file mode 100644 index 0000000..be2a7f5 --- /dev/null +++ b/database/migrations/2021_05_26_052938_change_meal_to_int_in_journal_entries.php @@ -0,0 +1,56 @@ +integer('meal_int')->unsigned()->nullable()->after('protein'); + }); + DB::update('UPDATE `journal_entries` SET meal_int = + IF(meal = "breakfast", 0, + IF(meal = "lunch", 1, + IF(meal = "dinner", 2, 3) + ) + )'); + Schema::table('journal_entries', function (Blueprint $table) { + $table->dropColumn('meal'); + $table->renameColumn('meal_int', 'meal'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('journal_entries', function (Blueprint $table) { + $table->renameColumn('meal', 'meal_int'); + }); + Schema::table('journal_entries', function (Blueprint $table) { + $table->enum('meal', JournalEntry::meals()->pluck('value')->toArray())->after('protein'); + }); + DB::update('UPDATE `journal_entries` SET meal = + IF(meal_int = 0, "breakfast", + IF(meal_int = 1, "lunch", + IF(meal_int = 2, "dinner", "snacks") + ) + )'); + Schema::table('journal_entries', function (Blueprint $table) { + $table->dropColumn('meal_int'); + }); + } +} diff --git a/resources/views/journal-entries/create-from-nutrients.blade.php b/resources/views/journal-entries/create-from-nutrients.blade.php index 41b7c4b..fc7313c 100644 --- a/resources/views/journal-entries/create-from-nutrients.blade.php +++ b/resources/views/journal-entries/create-from-nutrients.blade.php @@ -29,7 +29,7 @@ diff --git a/resources/views/journal-entries/index.blade.php b/resources/views/journal-entries/index.blade.php index 08a0686..aaf9d91 100644 --- a/resources/views/journal-entries/index.blade.php +++ b/resources/views/journal-entries/index.blade.php @@ -135,21 +135,21 @@
- @foreach(['breakfast', 'lunch', 'dinner', 'snacks'] as $meal) + @foreach(\Illuminate\Support\Facades\Auth::user()->meals as $meal)

-
{{ Str::ucfirst($meal) }}
+
{{ $meal['label'] }}

@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient) - {{ \App\Support\Nutrients::round($entries->where('meal', $meal)->sum($nutrient['value']), $nutrient['value']) }}{{ $nutrient['unit'] }} + {{ \App\Support\Nutrients::round($entries->where('meal', $meal['value'])->sum($nutrient['value']), $nutrient['value']) }}{{ $nutrient['unit'] }} {{ $nutrient['value'] }}@if(!$loop->last), @endif @endforeach

- @forelse($entries->where('meal', $meal) as $entry) + @forelse($entries->where('meal', $meal['value']) as $entry)
{{ $entry->summary }}
diff --git a/resources/views/journal-entries/partials/entry-item-input.blade.php b/resources/views/journal-entries/partials/entry-item-input.blade.php index 27a429f..baf49c4 100644 --- a/resources/views/journal-entries/partials/entry-item-input.blade.php +++ b/resources/views/journal-entries/partials/entry-item-input.blade.php @@ -25,7 +25,7 @@