add persisted set ordering

This commit is contained in:
Yisroel Baum 2026-07-31 11:00:41 +03:00
parent fde12ad561
commit dc034bf393
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
9 changed files with 340 additions and 6 deletions

View file

@ -11,6 +11,8 @@ use App\Set\UseCases\CreateSetWithRoot\CreateSetWithRoot;
use App\Set\UseCases\CreateSetWithRoot\CreateSetWithRootRequest;
use App\Set\UseCases\DeleteSet\DeleteSet;
use App\Set\UseCases\DeleteSet\DeleteSetRequest;
use App\Set\UseCases\ReorderSets\ReorderSets;
use App\Set\UseCases\ReorderSets\ReorderSetsRequest;
use App\Set\UseCases\UpdateSet\UpdateSet;
use App\Set\UseCases\UpdateSet\UpdateSetRequest;
use App\Shared\Files\Filesystem;
@ -26,19 +28,34 @@ class SetController
private CreateSetWithRoot $createSetWithRoot,
private UpdateSet $updateSet,
private DeleteSet $deleteSet,
private ReorderSets $reorderSets,
private Filesystem $filesystem,
) {
}
public function index(): JsonResponse
{
$sets = [];
foreach ($this->setRepository->getAll() as $set) {
$sets[] = $this->buildSetPayload($set);
return new JsonResponse([
'sets' => $this->buildSetPayloads(
$this->setRepository->getAll(),
),
], 200);
}
public function reorder(Request $request): JsonResponse
{
try {
$sets = $this->reorderSets->execute(new ReorderSetsRequest(
setIds: $this->intArrayInput($request, 'setIds'),
));
} catch (BadRequestException $exception) {
return new JsonResponse([
'error' => $exception->getMessage(),
], 400);
}
return new JsonResponse([
'sets' => $sets,
'sets' => $this->buildSetPayloads($sets),
], 200);
}
@ -141,6 +158,58 @@ class SetController
];
}
/**
* @param DomainSet[] $sets
* @return array<int, array{
* id: int,
* name: string,
* description: string,
* iconImageUrl: string,
* rootElementId: int|null
* }>
*/
private function buildSetPayloads(array $sets): array
{
$setPayloads = [];
foreach ($sets as $set) {
$setPayloads[] = $this->buildSetPayload($set);
}
return $setPayloads;
}
/**
* @return int[]|null
*/
private function intArrayInput(Request $request, string $key): ?array
{
if (! $request->exists($key)) {
return null;
}
$value = $request->input($key);
if (! is_array($value)) {
return null;
}
$integerValues = [];
foreach ($value as $item) {
if (is_int($item)) {
$integerValues[] = $item;
continue;
}
if (is_string($item) && ctype_digit($item)) {
$integerValues[] = (int) $item;
continue;
}
return null;
}
return $integerValues;
}
private function stringInput(Request $request, string $key): ?string
{
if (! $request->exists($key)) {