kcal/app/Http/Controllers/JournalEntryController.php

224 lines
8.8 KiB
PHP

<?php
/**
* @noinspection PhpDocSignatureInspection
*/
namespace App\Http\Controllers;
use App\Models\Food;
use App\Models\JournalEntry;
use App\Models\Recipe;
use App\Rules\ArrayNotEmpty;
use App\Rules\InArray;
use App\Rules\StringIsDecimalOrFraction;
use App\Rules\UsesIngredientTrait;
use App\Support\ArrayFormat;
use App\Support\Number;
use App\Support\Nutrients;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
class JournalEntryController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request): View
{
$date = $request->date ?? Carbon::now()->toDateString();
return view('journal-entries.index')
->with('entries', JournalEntry::where([
'user_id' => Auth::user()->id,
'date' => $date,
])->get())
->with('date', Carbon::createFromFormat('Y-m-d', $date))
->with('nutrients', ['calories', 'fat', 'cholesterol', 'carbohydrates', 'sodium', 'protein']);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
$ingredients = [];
if ($old = old('ingredients')) {
foreach ($old['amount'] as $key => $amount) {
if (
empty($old['date'][$key])
&& empty($old['meal'][$key])
&& empty($amount)
&& empty($old['unit'][$key])
&& empty($old['id'][$key])
) {
continue;
}
$ingredients[] = [
'date' => $old['date'][$key],
'meal' => $old['meal'][$key],
'amount' => $amount,
'unit' => $old['unit'][$key],
'id' => $old['id'][$key],
'type' => $old['type'][$key],
'name' => $old['name'][$key],
];
}
}
return view('journal-entries.create')
->with('ingredients', $ingredients)
->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'],
]);
}
/**
* 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.
*/
public function store(Request $request): RedirectResponse
{
$input = $request->validate([
'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.amount' => ['required', 'array', new ArrayNotEmpty],
'ingredients.amount.*' => ['required_with:ingredients.id.*', 'nullable', new StringIsDecimalOrFraction],
'ingredients.unit' => ['required', 'array'],
'ingredients.unit.*' => ['nullable', 'string'],
'ingredients.id' => ['required', 'array', new ArrayNotEmpty],
'ingredients.id.*' => 'required_with:ingredients.amount.*|nullable',
'ingredients.type' => ['required', 'array', new ArrayNotEmpty],
'ingredients.type.*' => ['required_with:ingredients.id.*', 'nullable', new UsesIngredientTrait()],
]);
$ingredients = ArrayFormat::flipTwoDimensionalKeys($input['ingredients']);
/** @var \App\Models\JournalEntry[] $entries */
$entries = [];
// TODO: Improve efficiency? Potential for lots of queries here...
foreach ($ingredients as $ingredient) {
// Prepare entry values.
/**
* @var string $date
* @var string $meal
* @var float $amount
* @var string $unit
* @var int $id
* @var string $type
*/
extract($ingredient, EXTR_OVERWRITE);
$entry_key = "{$date}{$meal}";
$entries[$entry_key] = $entries[$entry_key] ?? JournalEntry::make([
'date' => $date,
'meal' => $meal,
])->user()->associate(Auth::user());
// Calculate amounts based on ingredient type.
if ($type == Food::class) {
$item = Food::whereId($id)->first();
$nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier($item, Number::floatFromString($amount), $unit,);
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient} =+ $item->{$nutrient} * $nutrient_multiplier;
}
$entries[$entry_key]->foods->add($item);
}
elseif ($type == Recipe::class) {
$item = Recipe::whereId($id)->first();
foreach (Nutrients::$all as $nutrient) {
$entries[$entry_key]->{$nutrient} += $item->{"{$nutrient}PerServing"}() * Number::floatFromString($amount);
}
$entries[$entry_key]->recipes->add($item);
}
else {
return back()->withInput()->withErrors("Invalid ingredient type {$type}.");
}
// Update summary
$entries[$entry_key]->summary .= (!empty($entries[$entry_key]->summary) ? ', ' : null) . "{$amount} {$unit} {$item->name}";
}
foreach ($entries as $entry) {
$entry->save();
$entry->user->save();
$entry->foods()->saveMany($entry->foods);
$entry->recipes()->saveMany($entry->recipes);
}
$count = count($entries);
session()->flash('message', "Added {$count} journal entries!");
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.
*/
public function delete(JournalEntry $journalEntry): View
{
return view('journal-entries.delete')->with('entry', $journalEntry);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(JournalEntry $journalEntry): RedirectResponse
{
$journalEntry->delete();
session()->flash('message', 'Journal entry deleted!');
return redirect(route('journal-entries.index', [
'date' => $journalEntry->date->toDateString()
]));
}
}