adrema/app/Form/Actions/ParticipantIndexAction.php

39 lines
1.3 KiB
PHP
Raw Normal View History

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;
use App\Form\Scopes\ParticipantFilterScope;
2024-02-08 21:04:00 +01:00
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Laravel\Scout\Builder;
2024-02-08 21:04:00 +01:00
use Lorisleiva\Actions\Concerns\AsAction;
class ParticipantIndexAction
{
use AsAction;
/**
2024-12-12 00:36:56 +01:00
* @return Builder<Participant>
*/
protected function getQuery(Form $form, ParticipantFilterScope $filter): Builder
{
return $filter->setForm($form)->getQuery()
->query(fn ($q) => $q->withCount('children')->with('form'));
}
public function asController(Form $form, ?int $parent = null): AnonymousResourceCollection
2024-02-08 21:04:00 +01:00
{
2024-12-12 00:30:59 +01:00
$filter = ParticipantFilterScope::fromRequest(request()->input('filter', ''))->parent($parent);
$data = match ($parent) {
2024-12-12 00:30:59 +01:00
null => $this->getQuery($form, $filter)->paginate(15), // initial all elements - paginate
-1 => $this->getQuery($form, $filter)->paginate(15), // initial root elements - parinate
default => $this->getQuery($form, $filter)->get(), // specific parent element - show all
};
return ParticipantResource::collection($data)->additional(['meta' => ParticipantResource::meta($form)]);
2024-02-08 21:04:00 +01:00
}
}