Replace meals static array with Collection

This commit is contained in:
Christopher C. Wells 2021-03-30 16:14:31 -07:00 committed by Christopher Charbonneau Wells
parent 2fcabb949c
commit 29638c711f
3 changed files with 40 additions and 12 deletions

View File

@ -96,7 +96,7 @@ class JournalEntryController extends Controller
return view('journal-entries.create')
->with('ingredients', $ingredients)
->with('meals', JournalEntry::$meals)
->with('meals', JournalEntry::meals()->toArray())
->with('units', Nutrients::units()->toArray())
->with('default_date', Carbon::createFromFormat('Y-m-d', $date));
}
@ -108,7 +108,7 @@ class JournalEntryController extends Controller
{
$date = $request->date ?? Carbon::now()->toDateString();
return view('journal-entries.create-from-nutrients')
->with('meals', JournalEntry::$meals)
->with('meals', JournalEntry::meals()->toArray())
->with('units', Nutrients::units()->toArray())
->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.*' => ['nullable', 'date', 'required_with:ingredients.id.*'],
'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_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction],
'ingredients.unit' => ['required', 'array'],
@ -225,7 +230,11 @@ class JournalEntryController extends Controller
public function storeFromNutrients(Request $request): RedirectResponse {
$attributes = $request->validate([
'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'],
'calories' => ['nullable', 'required_without_all:fat,cholesterol,sodium,carbohydrates,protein', 'numeric'],
'fat' => ['nullable', 'required_without_all:calories,cholesterol,sodium,carbohydrates,protein', 'numeric'],

View File

@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;
/**
* App\Models\JournalEntry
@ -88,14 +89,31 @@ final class JournalEntry extends Model
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 = [
['value' => 'breakfast', 'label' => 'Breakfast'],
['value' => 'lunch', 'label' => 'Lunch'],
['value' => 'dinner', 'label' => 'Dinner'],
['value' => 'snacks', 'label' => 'Snacks'],
];
public static function meals(): Collection {
return new Collection([
[
'value' => 'breakfast',
'label' => 'Breakfast'
],
[
'value' => 'lunch',
'label' => 'Lunch'],
[
'value' => 'dinner',
'label' => 'Dinner'],
[
'value' => 'snacks',
'label' => 'Snacks'],
]);
}
/**
* Get the User this entry belongs to.

View File

@ -1,5 +1,6 @@
<?php
use App\Models\JournalEntry;
use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
@ -25,7 +26,7 @@ class CreateJournalEntriesTable extends Migration
$table->unsignedFloat('sodium')->default(0);
$table->unsignedFloat('carbohydrates')->default(0);
$table->unsignedFloat('protein')->default(0);
$table->enum('meal', ['breakfast', 'lunch', 'dinner', 'snacks']);
$table->enum('meal', JournalEntry::meals()->pluck('value')->toArray());
$table->timestamps();
});
}