forked from eatvio/eatvio-infra
52 lines
1.2 KiB
PHP
Executable File
52 lines
1.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Recipe;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class RecipeController extends Controller
|
|
{
|
|
public function index(): View|ViewFactory
|
|
{
|
|
return view('recipe/index');
|
|
}
|
|
|
|
public function create(): View|ViewFactory
|
|
{
|
|
return view('recipe/edit_form');
|
|
}
|
|
|
|
public function show(Recipe $recipe): View|ViewFactory
|
|
{
|
|
$data = [
|
|
'author' => $recipe->author()->first(),
|
|
'tags' => $recipe->tags()->get(),
|
|
'recipe' => $recipe,
|
|
];
|
|
|
|
$expiresAt = now()->addHours(24);
|
|
|
|
views($recipe)
|
|
->cooldown($expiresAt)
|
|
->record();
|
|
|
|
return view('recipe/show', $data);
|
|
}
|
|
|
|
public function edit(Recipe $recipe): View|ViewFactory
|
|
{
|
|
if (! $this->canEditRecipe($recipe)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
return view('recipe/edit_form', ['recipe' => $recipe]);
|
|
}
|
|
|
|
private function canEditRecipe(Recipe $recipe): bool
|
|
{
|
|
return Gate::allows('edit', $recipe);
|
|
}
|
|
}
|