toby/app/Infrastructure/Http/Controllers/HolidayController.php
Adrian Hopek a37be18da6 #43 - wip
2022-03-01 12:36:00 +01:00

70 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Http\RedirectResponse;
use Inertia\Response;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\Holiday;
use Toby\Infrastructure\Http\Requests\HolidayRequest;
use Toby\Infrastructure\Http\Resources\HolidayFormDataResource;
use Toby\Infrastructure\Http\Resources\HolidayResource;
class HolidayController extends Controller
{
public function index(YearPeriodRetriever $yearPeriodRetriever): Response
{
$yearPeriod = $yearPeriodRetriever->selected();
$holidays = $yearPeriod
->holidays()
->orderBy("date")
->get();
return inertia("Holidays/Index", [
"holidays" => HolidayResource::collection($holidays),
]);
}
public function create(): Response
{
return inertia("Holidays/Create");
}
public function store(HolidayRequest $request): RedirectResponse
{
Holiday::query()->create($request->data());
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been created."));
}
public function edit(Holiday $holiday): Response
{
return inertia("Holidays/Edit", [
"holiday" => new HolidayFormDataResource($holiday),
]);
}
public function update(HolidayRequest $request, Holiday $holiday): RedirectResponse
{
$holiday->update($request->data());
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been updated."));
}
public function destroy(Holiday $holiday): RedirectResponse
{
$holiday->delete();
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been deleted."));
}
}