* #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>
This commit is contained in:
104
app/Infrastructure/Http/Controllers/KeysController.php
Normal file
104
app/Infrastructure/Http/Controllers/KeysController.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?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,
|
||||
]));
|
||||
}
|
||||
}
|
23
app/Infrastructure/Http/Requests/GiveKeyRequest.php
Normal file
23
app/Infrastructure/Http/Requests/GiveKeyRequest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Infrastructure\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Toby\Eloquent\Models\User;
|
||||
|
||||
class GiveKeyRequest extends FormRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
"user" => ["required", "exists:users,id"],
|
||||
];
|
||||
}
|
||||
|
||||
public function recipient(): User
|
||||
{
|
||||
return User::find($this->get("user"));
|
||||
}
|
||||
}
|
25
app/Infrastructure/Http/Resources/KeyResource.php
Normal file
25
app/Infrastructure/Http/Resources/KeyResource.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Infrastructure\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class KeyResource extends JsonResource
|
||||
{
|
||||
public static $wrap = null;
|
||||
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
"id" => $this->id,
|
||||
"user" => new SimpleUserResource($this->user),
|
||||
"updatedAt" => $this->updated_at->toDisplayString(),
|
||||
"can" => [
|
||||
"give" => $request->user()->can("give", $this->resource),
|
||||
"take" => !$this->user()->is($request->user()),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user