adrema/app/Activity/Api/SubactivityStoreAction.php

57 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2023-03-04 12:02:17 +01:00
<?php
namespace App\Activity\Api;
use App\Subactivity;
2023-03-07 00:25:11 +01:00
use DB;
2023-03-04 12:02:17 +01:00
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Arr;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class SubactivityStoreAction
{
use AsAction;
/**
* @param array<string, string|array<int, int>> $payload
*/
public function handle(array $payload): Subactivity
{
2023-03-07 00:25:11 +01:00
return DB::transaction(function () use ($payload) {
$subactivity = Subactivity::create(Arr::except($payload, 'activities'));
$subactivity->activities()->sync($payload['activities']);
2023-03-04 12:02:17 +01:00
2023-03-07 00:25:11 +01:00
return $subactivity;
});
2023-03-04 12:02:17 +01:00
}
/**
* @return array<string, string>
*/
public function rules(): array
{
return [
'name' => 'required|unique:subactivities,name',
'activities' => 'present|array|min:1',
2023-03-07 01:35:39 +01:00
'activities.*' => 'integer',
2023-03-04 12:02:17 +01:00
'is_filterable' => 'present|boolean',
];
}
/**
* @return array<string, string>
*/
public function getValidationAttributes(): array
{
return [
'activities' => 'Tätigkeiten',
];
}
public function asController(ActionRequest $request): JsonResponse
{
return response()->json($this->handle($request->validated()));
}
}