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($amount) && empty($old['unit'][$key]) && empty($old['id'][$key])) { continue; } $ingredients[] = [ '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', [ ['value' => 'breakfast', 'label' => 'Breakfast'], ['value' => 'lunch', 'label' => 'Lunch'], ['value' => 'dinner', 'label' => 'Dinner'], ['value' => 'snacks', 'label' => 'Snacks'], ]) ->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([ 'date' => 'required|date', 'meal' => 'required|string', 'ingredients.amount' => ['required', 'array', new ArrayNotEmpty], 'ingredients.amount.*' => ['required_with:foods.*,recipes.*', '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()], ]); $summary = []; $nutrients = array_fill_keys(Nutrients::$all, 0); // TODO: Improve efficiency? Potential for lots of queries here... foreach ($input['ingredients']['id'] as $key => $id) { if ($input['ingredients']['type'][$key] == Food::class) { $food = Food::whereId($id)->first(); $nutrient_multiplier = Nutrients::calculateFoodNutrientMultiplier( $food, Number::floatFromString($input['ingredients']['amount'][$key]), $input['ingredients']['unit'][$key], ); foreach ($nutrients as $nutrient => $amount) { $nutrients[$nutrient] += $food->{$nutrient} * $nutrient_multiplier; } $summary[] = "{$input['ingredients']['amount'][$key]} {$input['ingredients']['unit'][$key]} {$food->name}"; } elseif ($input['ingredients']['type'][$key] == Recipe::class) { $recipe = Recipe::whereId($id)->first(); foreach ($nutrients as $nutrient => $amount) { $nutrients[$nutrient] += $recipe->{"{$nutrient}PerServing"}() * Number::floatFromString($input['ingredients']['amount'][$key]); } $summary[] = "{$input['ingredients']['amount'][$key]} {$input['ingredients']['unit'][$key]} {$recipe->name}"; } } $entry = new JournalEntry([ 'summary' => implode(', ', $summary), 'date' => $input['date'], 'meal' => $input['meal'], ] + $nutrients); $entry->user()->associate(Auth::user()); if ($entry->save()) { if (isset($foods)) { $entry->foods()->saveMany($foods); } if (isset($recipes)) { $entry->recipes()->saveMany($recipes); } } 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() ])); } }