toby/app/Infrastructure/Http/Controllers/HolidayController.php
Adrian Hopek 474707e691 Merge branch 'main' into #43-vacation-summary-for-employee
# Conflicts:
#	app/Infrastructure/Http/Controllers/HolidayController.php
#	app/Infrastructure/Http/Controllers/VacationLimitController.php
#	app/Infrastructure/Http/Controllers/VacationRequestController.php
#	composer.lock
#	resources/js/Pages/Holidays/Index.vue
#	resources/js/Pages/VacationRequest/Create.vue
2022-03-02 10:44:07 +01:00

100 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
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(Request $request, YearPeriodRetriever $yearPeriodRetriever): Response
{
$yearPeriod = $yearPeriodRetriever->selected();
$holidays = $yearPeriod
->holidays()
->orderBy("date")
->get();
return inertia("Holidays/Index", [
"holidays" => HolidayResource::collection($holidays),
"can" => [
"manageHolidays" => $request->user()->can("manageHolidays"),
],
]);
}
/**
* @throws AuthorizationException
*/
public function create(): Response
{
$this->authorize("manageHolidays");
return inertia("Holidays/Create");
}
/**
* @throws AuthorizationException
*/
public function store(HolidayRequest $request): RedirectResponse
{
$this->authorize("manageHolidays");
Holiday::query()->create($request->data());
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been created."));
}
/**
* @throws AuthorizationException
*/
public function edit(Holiday $holiday): Response
{
$this->authorize("manageHolidays");
return inertia("Holidays/Edit", [
"holiday" => new HolidayFormDataResource($holiday),
]);
}
/**
* @throws AuthorizationException
*/
public function update(HolidayRequest $request, Holiday $holiday): RedirectResponse
{
$this->authorize("manageHolidays");
$holiday->update($request->data());
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been updated."));
}
/**
* @throws AuthorizationException
*/
public function destroy(Holiday $holiday): RedirectResponse
{
$this->authorize("manageHolidays");
$holiday->delete();
return redirect()
->route("holidays.index")
->with("success", __("Holiday has been deleted."));
}
}