mirror of https://github.com/kcal-app/kcal.git
Add basic journal entries index (WIP)
This commit is contained in:
parent
dfad48c71d
commit
c1ddb061df
|
@ -3,18 +3,28 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\JournalEntry;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class JournalEntryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function index()
|
||||
public function index(Request $request): View
|
||||
{
|
||||
//
|
||||
$date = $request->date ?? Carbon::now()->toDateString();
|
||||
return view('journal.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']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -54,7 +54,7 @@ class JournalEntry extends Model
|
|||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $with = ['user', 'foods', 'recipes'];
|
||||
protected $with = ['user', 'foods:id,name', 'recipes:id,name'];
|
||||
|
||||
/**
|
||||
* Get the User this entry belongs to.
|
||||
|
|
|
@ -47,11 +47,6 @@ class Recipe extends JournalableModel
|
|||
'servings' => 'int',
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected $with = ['steps', 'foodAmounts'];
|
||||
|
||||
/**
|
||||
* Nutrient total calculation methods.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __(":name's Journal", ['name' => Auth::user()->name]) }}
|
||||
</h2>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
@if(session()->has('message'))
|
||||
<div class="bg-green-200 border-2 border-green-600 p-2 mb-2">
|
||||
{{ session()->get('message') }}
|
||||
</div>
|
||||
@endif
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 bg-white border-b border-gray-200">
|
||||
<div class="flex flex-row justify-between items-center mb-4">
|
||||
<div><a class="text-gray-500 hover:text-gray-700 hover:border-gray-300" href="{{ route(Route::current()->getName(), ['date' => $date->copy()->subDay(1)->toDateString()]) }}">previous</a></div>
|
||||
<div class="font-extrabold text-lg">
|
||||
{{ $date->format('D, j M Y') }}
|
||||
</div>
|
||||
<div><a class="text-gray-500 hover:text-gray-700 hover:border-gray-300" href="{{ route(Route::current()->getName(), ['date' => $date->copy()->addDay(1)->toDateString()]) }}">next</a></div>
|
||||
</div>
|
||||
<div class="flex flex-col space-y-4">
|
||||
<div>
|
||||
<h3 class="font-semibold text-lg text-gray-800">Totals</h3>
|
||||
@foreach($nutrients as $nutrient)
|
||||
{{ round($entries->sum($nutrient), 2) }}g
|
||||
{{ $nutrient }}@if(!$loop->last), @endif
|
||||
@endforeach
|
||||
</div>
|
||||
@foreach(['breakfast', 'lunch', 'dinner', 'snacks'] as $meal)
|
||||
<div>
|
||||
<h3 class="font-semibold text-lg text-gray-800">
|
||||
{{ Str::ucfirst($meal) }}
|
||||
</h3>
|
||||
<div class="text-xs">
|
||||
@foreach($nutrients as $nutrient)
|
||||
{{ round($entries->where('meal', $meal)->sum($nutrient), 2) }}g
|
||||
{{ $nutrient }}@if(!$loop->last), @endif
|
||||
@endforeach
|
||||
</div>
|
||||
@forelse($entries->where('meal', $meal) as $entry)
|
||||
<details>
|
||||
<summary>{{ $entry->summary }}</summary>
|
||||
{{ $entry->calories }}
|
||||
</details>
|
||||
@empty
|
||||
<em>No entries.</em>
|
||||
@endforelse
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
|
@ -17,6 +17,32 @@
|
|||
</x-nav-link>
|
||||
</div>
|
||||
|
||||
<!-- Journal Dropdown -->
|
||||
<div class="hidden sm:flex sm:items-center sm:ml-6">
|
||||
<x-dropdown align="left" width="48">
|
||||
<x-slot name="trigger">
|
||||
<button class="flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300 focus:outline-none focus:text-gray-700 focus:border-gray-300 transition duration-150 ease-in-out">
|
||||
<div>Jorunal</div>
|
||||
|
||||
<div class="ml-1">
|
||||
<svg class="fill-current h-4 w-4" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
|
||||
<path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</div>
|
||||
</button>
|
||||
</x-slot>
|
||||
|
||||
<x-slot name="content">
|
||||
<x-dropdown-link :href="route('journal-entries.index')">
|
||||
{{ __('View Journal') }}
|
||||
</x-dropdown-link>
|
||||
<x-dropdown-link :href="route('journal-entries.create')">
|
||||
{{ __('Add Entry') }}
|
||||
</x-dropdown-link>
|
||||
</x-slot>
|
||||
</x-dropdown>
|
||||
</div>
|
||||
|
||||
<!-- Recipes Dropdown -->
|
||||
<div class="hidden sm:flex sm:items-center sm:ml-6">
|
||||
<x-dropdown align="left" width="48">
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
use App\Http\Controllers\FoodController;
|
||||
use App\Http\Controllers\JournalEntryController;
|
||||
use App\Http\Controllers\RecipeController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
@ -25,5 +26,6 @@ Route::get('/dashboard', function () {
|
|||
|
||||
Route::resource('foods', FoodController::class);
|
||||
Route::resource('recipes', RecipeController::class);
|
||||
Route::resource('journal-entries', JournalEntryController::class);
|
||||
|
||||
require __DIR__.'/auth.php';
|
||||
|
|
Loading…
Reference in New Issue