mirror of https://github.com/kcal-app/kcal.git
Change journal entry meal field to integer
This commit is contained in:
parent
7f3cde04ef
commit
d495130037
|
|
@ -122,7 +122,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()->toArray())
|
->with('meals', Auth::user()->meals)
|
||||||
->with('units', Nutrients::units()->toArray())
|
->with('units', Nutrients::units()->toArray())
|
||||||
->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
|
->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
|
||||||
}
|
}
|
||||||
|
|
@ -134,7 +134,7 @@ class JournalEntryController extends Controller
|
||||||
{
|
{
|
||||||
$date = $request->date ?? Carbon::now()->toDateString();
|
$date = $request->date ?? Carbon::now()->toDateString();
|
||||||
return view('journal-entries.create-from-nutrients')
|
return view('journal-entries.create-from-nutrients')
|
||||||
->with('meals', JournalEntry::meals()->toArray())
|
->with('meals', Auth::user()->meals)
|
||||||
->with('units', Nutrients::units()->toArray())
|
->with('units', Nutrients::units()->toArray())
|
||||||
->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
|
->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,9 @@
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
use App\Models\JournalEntry;
|
|
||||||
use App\Rules\ArrayNotEmpty;
|
|
||||||
use App\Rules\InArray;
|
use App\Rules\InArray;
|
||||||
use App\Rules\StringIsPositiveDecimalOrFraction;
|
|
||||||
use App\Rules\UsesIngredientTrait;
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
class StoreFromNutrientsJournalEntryRequest extends FormRequest
|
class StoreFromNutrientsJournalEntryRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
|
@ -21,8 +18,7 @@ class StoreFromNutrientsJournalEntryRequest extends FormRequest
|
||||||
'date' => ['required', 'date'],
|
'date' => ['required', 'date'],
|
||||||
'meal' => [
|
'meal' => [
|
||||||
'required',
|
'required',
|
||||||
'string',
|
new InArray(Auth::user()->meals->pluck('value')->toArray())
|
||||||
new InArray(JournalEntry::meals()->pluck('value')->toArray())
|
|
||||||
],
|
],
|
||||||
'summary' => ['required', 'string'],
|
'summary' => ['required', 'string'],
|
||||||
'calories' => ['nullable', 'numeric', 'min:0', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein'],
|
'calories' => ['nullable', 'numeric', 'min:0', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein'],
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use App\Rules\InArray;
|
||||||
use App\Rules\StringIsPositiveDecimalOrFraction;
|
use App\Rules\StringIsPositiveDecimalOrFraction;
|
||||||
use App\Rules\UsesIngredientTrait;
|
use App\Rules\UsesIngredientTrait;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
class StoreJournalEntryRequest extends FormRequest
|
class StoreJournalEntryRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
|
@ -22,10 +23,9 @@ class StoreJournalEntryRequest extends FormRequest
|
||||||
'ingredients.date.*' => ['nullable', 'date', 'required_with:ingredients.id.*'],
|
'ingredients.date.*' => ['nullable', 'date', 'required_with:ingredients.id.*'],
|
||||||
'ingredients.meal' => ['required', 'array', new ArrayNotEmpty],
|
'ingredients.meal' => ['required', 'array', new ArrayNotEmpty],
|
||||||
'ingredients.meal.*' => [
|
'ingredients.meal.*' => [
|
||||||
'nullable',
|
'required',
|
||||||
'string',
|
|
||||||
'required_with:ingredients.id.*',
|
'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', 'array', new ArrayNotEmpty],
|
||||||
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsPositiveDecimalOrFraction],
|
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsPositiveDecimalOrFraction],
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,10 @@ class ArrayNotEmpty implements Rule
|
||||||
*/
|
*/
|
||||||
public function passes($attribute, $value): bool
|
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;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -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');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -29,7 +29,7 @@
|
||||||
|
|
||||||
<x-inputs.select name="meal"
|
<x-inputs.select name="meal"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:options="$meals"
|
:options="$meals->toArray()"
|
||||||
:selectedValue="old('meal')"
|
:selectedValue="old('meal')"
|
||||||
:hasError="$errors->has('meal')"
|
:hasError="$errors->has('meal')"
|
||||||
required>
|
required>
|
||||||
|
|
|
||||||
|
|
@ -135,21 +135,21 @@
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
<div class="w-full sm:w-3/5 md:w-2/3 lg:w-3/4 flex flex-col space-y-4">
|
<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>
|
<div>
|
||||||
<h3 class="font-semibold text-lg text-gray-800">
|
<h3 class="font-semibold text-lg text-gray-800">
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<div>{{ Str::ucfirst($meal) }}</div>
|
<div>{{ $meal['label'] }}</div>
|
||||||
<div class="ml-2 w-full"><hr/></div>
|
<div class="ml-2 w-full"><hr/></div>
|
||||||
</div>
|
</div>
|
||||||
<span class="text-sm text-gray-500">
|
<span class="text-sm text-gray-500">
|
||||||
@foreach(\App\Support\Nutrients::all()->sortBy('weight') as $nutrient)
|
@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
|
{{ $nutrient['value'] }}@if(!$loop->last), @endif
|
||||||
@endforeach
|
@endforeach
|
||||||
</span>
|
</span>
|
||||||
</h3>
|
</h3>
|
||||||
@forelse($entries->where('meal', $meal) as $entry)
|
@forelse($entries->where('meal', $meal['value']) as $entry)
|
||||||
<details>
|
<details>
|
||||||
<summary>{{ $entry->summary }}</summary>
|
<summary>{{ $entry->summary }}</summary>
|
||||||
<div class="border-blue-100 border-2 p-2 ml-4">
|
<div class="border-blue-100 border-2 p-2 ml-4">
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
<x-inputs.label for="ingredients[meal][]" value="Meal" class="md:hidden"/>
|
<x-inputs.label for="ingredients[meal][]" value="Meal" class="md:hidden"/>
|
||||||
<x-inputs.select name="ingredients[meal][]"
|
<x-inputs.select name="ingredients[meal][]"
|
||||||
class="block w-full"
|
class="block w-full"
|
||||||
:options="$meals"
|
:options="$meals->toArray()"
|
||||||
:selectedValue="$meal ?? null"
|
:selectedValue="$meal ?? null"
|
||||||
:hasError="$errors->has('ingredients.meal.' . $key)"
|
:hasError="$errors->has('ingredients.meal.' . $key)"
|
||||||
required>
|
required>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue