2024-02-08 21:04:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Actions;
|
|
|
|
|
|
|
|
use App\Form\Models\Form;
|
2024-02-08 23:13:59 +01:00
|
|
|
use App\Form\Models\Participant;
|
2024-02-08 21:04:00 +01:00
|
|
|
use App\Form\Resources\ParticipantResource;
|
2024-03-08 02:19:07 +01:00
|
|
|
use App\Form\Scopes\ParticipantFilterScope;
|
2024-06-19 23:51:20 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2024-02-08 21:04:00 +01:00
|
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2024-12-11 22:32:23 +01:00
|
|
|
use Laravel\Scout\Builder;
|
2024-02-08 21:04:00 +01:00
|
|
|
use Lorisleiva\Actions\Concerns\AsAction;
|
|
|
|
|
|
|
|
class ParticipantIndexAction
|
|
|
|
{
|
|
|
|
use AsAction;
|
|
|
|
|
2024-06-19 23:51:20 +02:00
|
|
|
/**
|
|
|
|
* @return HasMany<Participant>
|
|
|
|
*/
|
2024-12-11 22:32:23 +01:00
|
|
|
protected function getQuery(Form $form, ParticipantFilterScope $filter): Builder
|
2024-06-19 23:51:20 +02:00
|
|
|
{
|
2024-12-11 22:32:23 +01:00
|
|
|
return $filter->setForm($form)->getQuery()
|
|
|
|
->query(fn ($q) => $q->withCount('children')->with('form'));
|
2024-06-19 23:51:20 +02:00
|
|
|
}
|
|
|
|
|
2024-02-08 23:13:59 +01:00
|
|
|
/**
|
|
|
|
* @return LengthAwarePaginator<Participant>
|
|
|
|
*/
|
2024-03-08 02:19:07 +01:00
|
|
|
public function handle(Form $form, ParticipantFilterScope $filter): LengthAwarePaginator
|
2024-02-08 21:04:00 +01:00
|
|
|
{
|
2024-06-19 23:51:20 +02:00
|
|
|
return $this->getQuery($form, $filter)->paginate(15);
|
2024-02-08 21:04:00 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 23:51:20 +02:00
|
|
|
public function asController(Form $form, ?int $parent = null): AnonymousResourceCollection
|
2024-02-08 21:04:00 +01:00
|
|
|
{
|
2024-03-08 02:19:07 +01:00
|
|
|
$filter = ParticipantFilterScope::fromRequest(request()->input('filter'));
|
2024-06-19 23:51:20 +02:00
|
|
|
|
|
|
|
$data = match ($parent) {
|
|
|
|
null => $this->handle($form, $filter),
|
2024-06-20 23:25:47 +02:00
|
|
|
-1 => $this->getQuery($form, $filter)->where('parent_id', null)->paginate(15),
|
2024-06-19 23:51:20 +02:00
|
|
|
default => $this->getQuery($form, $filter)->where('parent_id', $parent)->get(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return ParticipantResource::collection($data)->additional(['meta' => ParticipantResource::meta($form)]);
|
2024-02-08 21:04:00 +01:00
|
|
|
}
|
|
|
|
}
|