toby/app/Infrastructure/Http/Controllers/KeysController.php
Adrian Hopek d60dc75f99
#118 - keys (#128)
* #118 - wip

* #118 - keys

* #118 - fix

* #118 - fix menu

* #118 - fix to policies and added translations

* #118 - wip

* #118 - tests

* #118 - fix

* #118 - fix

* #118 - fix

* Update resources/lang/pl.json

Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>

* #118 - cr fix

Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl>
Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
2022-04-21 08:16:31 +02:00

105 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Toby\Infrastructure\Http\Controllers;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Inertia\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Toby\Eloquent\Models\Key;
use Toby\Eloquent\Models\User;
use Toby\Infrastructure\Http\Requests\GiveKeyRequest;
use Toby\Infrastructure\Http\Resources\KeyResource;
use Toby\Infrastructure\Http\Resources\SimpleUserResource;
class KeysController extends Controller
{
public function index(Request $request): Response
{
$keys = Key::query()
->oldest()
->get();
$users = User::query()
->orderByProfileField("last_name")
->orderByProfileField("first_name")
->get();
return inertia("Keys", [
"keys" => KeyResource::collection($keys),
"users" => SimpleUserResource::collection($users),
"can" => [
"manageKeys" => $request->user()->can("manage", Key::class),
],
]);
}
/**
* @throws AuthorizationException
*/
public function store(Request $request): RedirectResponse
{
$this->authorize("manage", Key::class);
$key = $request->user()->keys()->create();
return redirect()
->back()
->with("success", __("Key no :number has been created.", [
"number" => $key->id,
]));
}
public function take(Key $key, Request $request): RedirectResponse
{
$previousUser = $key->user;
$key->user()->associate($request->user());
$key->save();
return redirect()
->back()
->with("success", __("Key no :number has been taken from :user.", [
"number" => $key->id,
"user" => $previousUser->profile->full_name,
]));
}
/**
* @throws AuthorizationException
*/
public function give(Key $key, GiveKeyRequest $request): RedirectResponse
{
$this->authorize("give", $key);
$recipient = $request->recipient();
$key->user()->associate($recipient);
$key->save();
return redirect()
->back()
->with("success", __("Key no :number has been given to :user.", [
"number" => $key->id,
"user" => $recipient->profile->full_name,
]));
}
public function destroy(Key $key): RedirectResponse
{
$this->authorize("manage", Key::class);
$key->delete();
return redirect()
->back()
->with("success", __("Key no :number has been deleted.", [
"number" => $key->id,
]));
}
}