forked from eatvio/eatvio-infra
57 lines
1.3 KiB
PHP
Executable File
57 lines
1.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Plan;
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
class PlanController extends Controller
|
|
{
|
|
public function index(Request $request): View|ViewFactory
|
|
{
|
|
if ($request->has('author')) {
|
|
$author = $request->get('author');
|
|
$user = User::find($author);
|
|
$plans = $user ? $user->author_of_plans : [];
|
|
} else {
|
|
$plans = Plan::all();
|
|
}
|
|
|
|
return view('plan/index', ['plans' => $plans]);
|
|
}
|
|
|
|
public function create(): View|ViewFactory
|
|
{
|
|
return view('plan/edit_form');
|
|
}
|
|
|
|
public function show(Plan $plan): View|ViewFactory
|
|
{
|
|
$expiresAt = now()->addHours(24);
|
|
|
|
views($plan)
|
|
->cooldown($expiresAt)
|
|
->record();
|
|
|
|
return view('plan/show', ['plan' => $plan]);
|
|
}
|
|
|
|
public function edit(Plan $plan): View|ViewFactory
|
|
{
|
|
if (! $this->canEditPlan($plan)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
return view('plan/edit_form', ['plan' => $plan]);
|
|
}
|
|
|
|
private function canEditPlan(Plan $plan): bool
|
|
{
|
|
return Gate::allows('edit', $plan);
|
|
}
|
|
}
|