Change journal entry meal field to integer

This commit is contained in:
Christopher C. Wells 2021-05-26 06:06:03 -07:00
parent 7f3cde04ef
commit d495130037
8 changed files with 73 additions and 18 deletions

View File

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

View File

@ -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'],

View File

@ -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],

View File

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

View File

@ -0,0 +1,56 @@
<?php
use App\Models\JournalEntry;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class ChangeMealToIntInJournalEntries extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('journal_entries', function (Blueprint $table) {
$table->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');
});
}
}

View File

@ -29,7 +29,7 @@
<x-inputs.select name="meal"
class="block w-full"
:options="$meals"
:options="$meals->toArray()"
:selectedValue="old('meal')"
:hasError="$errors->has('meal')"
required>

View File

@ -135,21 +135,21 @@
</section>
</div>
<div class="w-full sm:w-3/5 md:w-2/3 lg:w-3/4 flex flex-col space-y-4">
@foreach(['breakfast', 'lunch', 'dinner', 'snacks'] as $meal)
@foreach(\Illuminate\Support\Facades\Auth::user()->meals as $meal)
<div>
<h3 class="font-semibold text-lg text-gray-800">
<div class="flex items-center">
<div>{{ Str::ucfirst($meal) }}</div>
<div>{{ $meal['label'] }}</div>
<div class="ml-2 w-full"><hr/></div>
</div>
<span class="text-sm text-gray-500">
@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
</span>
</h3>
@forelse($entries->where('meal', $meal) as $entry)
@forelse($entries->where('meal', $meal['value']) as $entry)
<details>
<summary>{{ $entry->summary }}</summary>
<div class="border-blue-100 border-2 p-2 ml-4">

View File

@ -25,7 +25,7 @@
<x-inputs.label for="ingredients[meal][]" value="Meal" class="md:hidden"/>
<x-inputs.select name="ingredients[meal][]"
class="block w-full"
:options="$meals"
:options="$meals->toArray()"
:selectedValue="$meal ?? null"
:hasError="$errors->has('ingredients.meal.' . $key)"
required>