mirror of https://github.com/kcal-app/kcal.git
Replace meals static array with Collection
This commit is contained in:
parent
2fcabb949c
commit
29638c711f
|
@ -96,7 +96,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()->toArray())
|
||||||
->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));
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,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)
|
->with('meals', JournalEntry::meals()->toArray())
|
||||||
->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));
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,12 @@ class JournalEntryController extends Controller
|
||||||
'ingredients.date' => ['required', 'array', new ArrayNotEmpty],
|
'ingredients.date' => ['required', 'array', new ArrayNotEmpty],
|
||||||
'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.*' => ['nullable', 'string', 'required_with:ingredients.id.*', new InArray(array_column(JournalEntry::$meals, 'value'))],
|
'ingredients.meal.*' => [
|
||||||
|
'nullable',
|
||||||
|
'string',
|
||||||
|
'required_with:ingredients.id.*',
|
||||||
|
new InArray(JournalEntry::meals()->pluck('value')->toArray())
|
||||||
|
],
|
||||||
'ingredients.amount' => ['required', 'array', new ArrayNotEmpty],
|
'ingredients.amount' => ['required', 'array', new ArrayNotEmpty],
|
||||||
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction],
|
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction],
|
||||||
'ingredients.unit' => ['required', 'array'],
|
'ingredients.unit' => ['required', 'array'],
|
||||||
|
@ -225,7 +230,11 @@ class JournalEntryController extends Controller
|
||||||
public function storeFromNutrients(Request $request): RedirectResponse {
|
public function storeFromNutrients(Request $request): RedirectResponse {
|
||||||
$attributes = $request->validate([
|
$attributes = $request->validate([
|
||||||
'date' => ['required', 'date'],
|
'date' => ['required', 'date'],
|
||||||
'meal' => ['required', 'string', new InArray(array_column(JournalEntry::$meals, 'value'))],
|
'meal' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
new InArray(JournalEntry::meals()->pluck('value')->toArray())
|
||||||
|
],
|
||||||
'summary' => ['required', 'string'],
|
'summary' => ['required', 'string'],
|
||||||
'calories' => ['nullable', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein', 'numeric'],
|
'calories' => ['nullable', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein', 'numeric'],
|
||||||
'fat' => ['nullable', 'required_without_all:calories,cholesterol,sodium,carbohydrates,protein', 'numeric'],
|
'fat' => ['nullable', 'required_without_all:calories,cholesterol,sodium,carbohydrates,protein', 'numeric'],
|
||||||
|
|
|
@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* App\Models\JournalEntry
|
* App\Models\JournalEntry
|
||||||
|
@ -88,14 +89,31 @@ final class JournalEntry extends Model
|
||||||
protected $with = ['user', 'foods:id,name,slug', 'recipes:id,name,slug'];
|
protected $with = ['user', 'foods:id,name,slug', 'recipes:id,name,slug'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Valid meal options.
|
* Get all supported meals and metadata.
|
||||||
|
*
|
||||||
|
* Each entry has two keys:
|
||||||
|
* - value: Machine name for the meal.
|
||||||
|
* - label: Human-readable name for the meal.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
*/
|
*/
|
||||||
public static array $meals = [
|
public static function meals(): Collection {
|
||||||
['value' => 'breakfast', 'label' => 'Breakfast'],
|
return new Collection([
|
||||||
['value' => 'lunch', 'label' => 'Lunch'],
|
[
|
||||||
['value' => 'dinner', 'label' => 'Dinner'],
|
'value' => 'breakfast',
|
||||||
['value' => 'snacks', 'label' => 'Snacks'],
|
'label' => 'Breakfast'
|
||||||
];
|
],
|
||||||
|
[
|
||||||
|
'value' => 'lunch',
|
||||||
|
'label' => 'Lunch'],
|
||||||
|
[
|
||||||
|
'value' => 'dinner',
|
||||||
|
'label' => 'Dinner'],
|
||||||
|
[
|
||||||
|
'value' => 'snacks',
|
||||||
|
'label' => 'Snacks'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the User this entry belongs to.
|
* Get the User this entry belongs to.
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\JournalEntry;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
@ -25,7 +26,7 @@ class CreateJournalEntriesTable extends Migration
|
||||||
$table->unsignedFloat('sodium')->default(0);
|
$table->unsignedFloat('sodium')->default(0);
|
||||||
$table->unsignedFloat('carbohydrates')->default(0);
|
$table->unsignedFloat('carbohydrates')->default(0);
|
||||||
$table->unsignedFloat('protein')->default(0);
|
$table->unsignedFloat('protein')->default(0);
|
||||||
$table->enum('meal', ['breakfast', 'lunch', 'dinner', 'snacks']);
|
$table->enum('meal', JournalEntry::meals()->pluck('value')->toArray());
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue