adrema/app/Group/Actions/GroupBulkstoreAction.php

45 lines
1.1 KiB
PHP
Raw Normal View History

2023-12-30 02:02:07 +01:00
<?php
namespace App\Group\Actions;
use App\Group;
use App\Group\Enums\Level;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class GroupBulkstoreAction
{
use AsAction;
2023-12-30 19:02:15 +01:00
/**
* @return array<string, string|array<int, string|Rule>>
*/
2023-12-30 02:02:07 +01:00
public function rules(): array
{
return [
'*.id' => 'required|integer|exists:groups,id',
'*.inner_name' => 'required|string|max:255',
'*.level' => ['required', 'string', Rule::in(Level::values())],
];
}
2023-12-30 19:02:15 +01:00
/**
2024-01-26 15:06:16 +01:00
* @param array<array-key, mixed> $groups
2023-12-30 19:02:15 +01:00
*/
public function handle(array $groups): void
2023-12-30 02:02:07 +01:00
{
foreach ($groups as $payload) {
Group::find($payload['id'])->update(['level' => $payload['level'], 'inner_name' => $payload['inner_name']]);
}
}
public function asController(ActionRequest $request): JsonResponse
{
$this->handle($request->validated());
return response()->json([]);
}
}