adrema/app/Membership/Actions/MembershipUpdateAction.php

70 lines
1.9 KiB
PHP
Raw Normal View History

2022-12-11 21:00:48 +01:00
<?php
namespace App\Membership\Actions;
use App\Activity;
use App\Maildispatcher\Actions\ResyncAction;
2022-12-11 21:00:48 +01:00
use App\Member\Membership;
use App\Subactivity;
use Carbon\Carbon;
2023-09-12 16:54:13 +02:00
use Illuminate\Http\JsonResponse;
2022-12-11 21:00:48 +01:00
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
class MembershipUpdateAction
{
use AsAction;
public function handle(Membership $membership, Activity $activity, ?Subactivity $subactivity, ?Carbon $promisedAt): Membership
{
$membership->update([
'activity_id' => $activity->id,
'subactivity_id' => $subactivity ? $subactivity->id : null,
'promised_at' => $promisedAt,
]);
return $membership;
}
/**
* @return array<string, array<int, string|In>>
*/
public function rules(): array
{
$subactivityRule = request()->activity_id ? ['nullable', Rule::exists('activity_subactivity', 'subactivity_id')->where('activity_id', request()->activity_id)] : ['nullable'];
return [
'activity_id' => ['bail', 'required', 'exists:activities,id'],
'subactivity_id' => $subactivityRule,
'promised_at' => ['nullable', 'date'],
];
}
/**
* @return array<string, string>
*/
public function getValidationAttributes(): array
{
return [
'activity_id' => 'Tätigkeit',
'subactivity_id' => 'Untertätigkeit',
];
}
2023-09-12 16:54:13 +02:00
public function asController(Membership $membership, ActionRequest $request): JsonResponse
2022-12-11 21:00:48 +01:00
{
$this->handle(
$membership,
Activity::find($request->activity_id),
$request->subactivity_id ? Subactivity::find($request->subactivity_id) : null,
$request->promised_at ? Carbon::parse($request->promised_at) : null,
);
ResyncAction::dispatch();
2023-09-12 16:54:13 +02:00
return response()->json([]);
2022-12-11 21:00:48 +01:00
}
}