diff --git a/app/Http/Controllers/JournalEntryController.php b/app/Http/Controllers/JournalEntryController.php index 5106a3a..871bb35 100644 --- a/app/Http/Controllers/JournalEntryController.php +++ b/app/Http/Controllers/JournalEntryController.php @@ -80,6 +80,23 @@ class JournalEntryController extends Controller ]); } + /** + * Show the form for creating a journal entry from nutrients directly. + */ + public function createFromNutrients(): View + { + return view('journal-entries.create-from-nutrients') + ->with('meals', JournalEntry::$meals) + ->with('units', [ + ['value' => 'tsp', 'label' => 'tsp.'], + ['value' => 'tbsp', 'label' => 'tbsp.'], + ['value' => 'cup', 'label' => 'cup'], + ['value' => 'oz', 'label' => 'oz'], + ['value' => 'g', 'label' => 'grams'], + ['value' => 'servings', 'label' => 'servings'], + ]); + } + /** * Store a newly created resource in storage. */ @@ -158,6 +175,32 @@ class JournalEntryController extends Controller return redirect()->route('journal-entries.index'); } + /** + * Store an entry from nutrients. + */ + public function storeFromNutrients(Request $request): RedirectResponse { + $attributes = $request->validate([ + 'date' => ['required', 'date'], + 'meal' => ['required', 'string', new InArray(array_column(JournalEntry::$meals, 'value'))], + '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'], + 'cholesterol' => ['nullable', 'required_without_all:calories,fat,sodium,carbohydrates,protein', 'numeric'], + 'sodium' => ['nullable', 'required_without_all:calories,fat,cholesterol,carbohydrates,protein', 'numeric'], + 'carbohydrates' => ['nullable', 'required_without_all:calories,fat,cholesterol,sodium,protein', 'numeric'], + 'protein' => ['nullable', 'required_without_all:calories,fat,cholesterol,sodium,carbohydrates', 'numeric'], + ]); + + $entry = JournalEntry::make(array_filter($attributes)) + ->user()->associate(Auth::user()); + $entry->save(); + session()->flash('message', "Journal entry added!"); + return redirect()->route( + 'journal-entries.index', + ['date' => $entry->date->format('Y-m-d')] + ); + } + /** * Confirm removal of the specified resource. */ diff --git a/resources/views/journal-entries/create-from-nutrients.blade.php b/resources/views/journal-entries/create-from-nutrients.blade.php new file mode 100644 index 0000000..950b01d --- /dev/null +++ b/resources/views/journal-entries/create-from-nutrients.blade.php @@ -0,0 +1,76 @@ + + +

+ {{ __('Add Nutrient Journal Entry') }} +

+
+ +
+
+
+
+
+ @csrf +
+ +
+ + + +
+ + +
+ + + + + +
+ + +
+ + + +
+
+ +
+ @foreach (\App\Support\Nutrients::$all as $nutrient) + +
+ + + +
+ @endforeach +
+ +
+ Add Entry +
+
+
+
+
+
+
diff --git a/resources/views/journal-entries/create.blade.php b/resources/views/journal-entries/create.blade.php index 7a01f0c..b48d12c 100644 --- a/resources/views/journal-entries/create.blade.php +++ b/resources/views/journal-entries/create.blade.php @@ -1,7 +1,7 @@

- {{ __('Add Journal Entry') }} + {{ __('Add Journal Entries') }}

diff --git a/resources/views/layouts/navigation.blade.php b/resources/views/layouts/navigation.blade.php index 2472acb..ad05fa8 100644 --- a/resources/views/layouts/navigation.blade.php +++ b/resources/views/layouts/navigation.blade.php @@ -39,6 +39,9 @@ {{ __('Add Entries') }} + + {{ __('Add Nutrient Entry') }} + diff --git a/routes/web.php b/routes/web.php index bbfb901..a541d50 100644 --- a/routes/web.php +++ b/routes/web.php @@ -25,18 +25,21 @@ Route::get('/dashboard', function () { return view('dashboard'); })->middleware(['auth'])->name('dashboard'); +// Foods. Route::resource('foods', FoodController::class)->middleware(['auth']); -Route::get('/foods/{food}/delete', [FoodController::class, 'delete']) - ->middleware(['auth']) - ->name('foods.delete'); -Route::resource('recipes', RecipeController::class)->middleware(['auth']); -Route::resource('journal-entries', JournalEntryController::class)->middleware(['auth']); -Route::get('/journal-entries/{journalEntry}/delete', [JournalEntryController::class, 'delete']) - ->middleware(['auth']) - ->name('journal-entries.delete'); +Route::get('/foods/{food}/delete', [FoodController::class, 'delete'])->middleware(['auth'])->name('foods.delete'); -Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search']) - ->middleware(['auth']) - ->name('ingredient-picker.search'); +// Recipes. +Route::resource('recipes', RecipeController::class)->middleware(['auth']); + +// Journal entries. +Route::get('/journal-entries/create-from-nutrients', [JournalEntryController::class, 'createFromNutrients'])->middleware(['auth'])->name('journal-entries.create.from-nutrients'); +Route::post('/journal-entries/create-from-nutrients', [JournalEntryController::class, 'storeFromNutrients'])->middleware(['auth'])->name('journal-entries.store.from-nutrients'); +Route::resource('journal-entries', JournalEntryController::class)->middleware(['auth']); +Route::get('/journal-entries/{journalEntry}/delete', [JournalEntryController::class, 'delete'])->middleware(['auth'])->name('journal-entries.delete'); + +// Custom. +// TODO: Change this to a custom JSON API endpoint. +Route::get('/ingredient-picker/search', [IngredientPickerController::class, 'search'])->middleware(['auth'])->name('ingredient-picker.search'); require __DIR__.'/auth.php';