add child element ordering

This commit is contained in:
Yisroel Baum 2026-06-22 09:51:21 +03:00
parent 7323925319
commit 62dc119b11
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
12 changed files with 554 additions and 11 deletions

View file

@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Model;
* @property string|null $long_pdf_path
* @property string|null $youtube_url
* @property int|null $parent_element_id
* @property int $sort_order
*
* @method static Builder<static>|ElementModel newModelQuery()
* @method static Builder<static>|ElementModel newQuery()
@ -30,6 +31,7 @@ use Illuminate\Database\Eloquent\Model;
* @method static Builder<static>|ElementModel whereShortPdfPath($value)
* @method static Builder<static>|ElementModel whereLongPdfPath($value)
* @method static Builder<static>|ElementModel whereYoutubeUrl($value)
* @method static Builder<static>|ElementModel whereSortOrder($value)
*
* @mixin \Eloquent
*/
@ -49,10 +51,12 @@ class ElementModel extends Model
'long_pdf_path',
'youtube_url',
'parent_element_id',
'sort_order',
];
protected $casts = [
'set_id' => 'integer',
'parent_element_id' => 'integer',
'sort_order' => 'integer',
];
}

View file

@ -25,4 +25,13 @@ interface ElementRepository
* @return Element[]
*/
public function findByParentElement(Element $parentElement): array;
/**
* @param int[] $childElementIds
* @return Element[]
*/
public function reorderChildren(
Element $parentElement,
array $childElementIds,
): array;
}

View file

@ -5,6 +5,7 @@ namespace App\Element;
use App\Set\Set as DomainSet;
use App\Set\SetRepository;
use DomainException;
use Illuminate\Support\Facades\DB;
class EloquentElementRepository implements ElementRepository
{
@ -14,6 +15,7 @@ class EloquentElementRepository implements ElementRepository
public function create(CreateElementDto $dto): Element
{
$parentElementId = $dto->parentElement?->getId();
$model = ElementModel::create([
'set_id' => $dto->set->getId(),
'title' => $dto->title,
@ -23,7 +25,8 @@ class EloquentElementRepository implements ElementRepository
'short_pdf_path' => $dto->shortPdfPath,
'long_pdf_path' => $dto->longPdfPath,
'youtube_url' => $dto->youtubeUrl,
'parent_element_id' => $dto->parentElement?->getId(),
'parent_element_id' => $parentElementId,
'sort_order' => $this->nextSortOrder($dto->set, $parentElementId),
]);
return new Element(
@ -86,6 +89,7 @@ class EloquentElementRepository implements ElementRepository
{
$model = ElementModel::where('set_id', $set->getId())
->whereNull('parent_element_id')
->orderBy('sort_order')
->orderBy('id')
->first();
@ -117,6 +121,7 @@ class EloquentElementRepository implements ElementRepository
'parent_element_id',
$parentElement->getId(),
)
->orderBy('sort_order')
->orderBy('id')
->get();
$elements = [];
@ -127,6 +132,46 @@ class EloquentElementRepository implements ElementRepository
return $elements;
}
/**
* @param int[] $childElementIds
* @return Element[]
*/
public function reorderChildren(
Element $parentElement,
array $childElementIds,
): array {
DB::transaction(function () use ($parentElement, $childElementIds) {
$sortOrder = 1;
foreach ($childElementIds as $childElementId) {
ElementModel::where('id', $childElementId)
->where('parent_element_id', $parentElement->getId())
->update(['sort_order' => $sortOrder]);
$sortOrder++;
}
});
return $this->findByParentElement($parentElement);
}
private function nextSortOrder(
DomainSet $set,
?int $parentElementId,
): int {
$query = ElementModel::where('set_id', $set->getId());
if ($parentElementId === null) {
$query->whereNull('parent_element_id');
} else {
$query->where('parent_element_id', $parentElementId);
}
$currentMaxSortOrder = $query->max('sort_order');
if ($currentMaxSortOrder === null) {
return 1;
}
return (int) $currentMaxSortOrder + 1;
}
private function toDomain(ElementModel $model): Element
{
$set = $this->setRepo->find($model->set_id);

View file

@ -0,0 +1,154 @@
<?php
namespace App\Element\UseCases\ReorderChildElements;
use App\Element\Element;
use App\Element\ElementRepository;
use App\Exceptions\BadRequestException;
use App\Exceptions\NotFoundException;
class ReorderChildElements
{
public function __construct(private ElementRepository $elementRepository)
{
}
/**
* @return Element[]
* @throws BadRequestException
* @throws NotFoundException
*/
public function execute(ReorderChildElementsRequest $request): array
{
if ($request->parentElementId === null) {
throw new BadRequestException('parentElementId is required');
}
if ($request->childElementIds === null) {
throw new BadRequestException('childElementIds is required');
}
$parentElement = $this->elementRepository->find(
$request->parentElementId,
);
if ($parentElement === null) {
throw new NotFoundException('Parent element not found');
}
$childElementIds = $this->validatedChildElementIds(
$request->childElementIds,
);
$directChildElementIds = $this->elementIds(
$this->elementRepository->findByParentElement($parentElement),
);
$this->validateNoDuplicateIds($childElementIds);
$this->validateAllIdsAreDirectChildren(
$childElementIds,
$directChildElementIds,
);
$this->validateEveryDirectChildWasSubmitted(
$childElementIds,
$directChildElementIds,
);
return $this->elementRepository->reorderChildren(
$parentElement,
$childElementIds,
);
}
/**
* @param mixed[] $childElementIds
* @return int[]
* @throws BadRequestException
*/
private function validatedChildElementIds(array $childElementIds): array
{
$validatedChildElementIds = [];
foreach ($childElementIds as $childElementId) {
if (! is_int($childElementId)) {
throw new BadRequestException(
'childElementIds must contain integers',
);
}
$validatedChildElementIds[] = $childElementId;
}
return $validatedChildElementIds;
}
/**
* @param int[] $childElementIds
* @throws BadRequestException
*/
private function validateNoDuplicateIds(array $childElementIds): void
{
$seenChildElementIds = [];
foreach ($childElementIds as $childElementId) {
if (isset($seenChildElementIds[$childElementId])) {
throw new BadRequestException(
'Child order contains duplicate ids',
);
}
$seenChildElementIds[$childElementId] = true;
}
}
/**
* @param int[] $childElementIds
* @param int[] $directChildElementIds
* @throws BadRequestException
*/
private function validateAllIdsAreDirectChildren(
array $childElementIds,
array $directChildElementIds,
): void {
$directChildElementIdsById = [];
foreach ($directChildElementIds as $directChildElementId) {
$directChildElementIdsById[$directChildElementId] = true;
}
foreach ($childElementIds as $childElementId) {
if (! isset($directChildElementIdsById[$childElementId])) {
throw new BadRequestException(
'Child order contains invalid child',
);
}
}
}
/**
* @param int[] $childElementIds
* @param int[] $directChildElementIds
* @throws BadRequestException
*/
private function validateEveryDirectChildWasSubmitted(
array $childElementIds,
array $directChildElementIds,
): void {
if (count($childElementIds) === count($directChildElementIds)) {
return;
}
throw new BadRequestException(
'Child order must include every direct child',
);
}
/**
* @param Element[] $elements
* @return int[]
*/
private function elementIds(array $elements): array
{
$elementIds = [];
foreach ($elements as $element) {
$elementIds[] = $element->getId();
}
return $elementIds;
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace App\Element\UseCases\ReorderChildElements;
class ReorderChildElementsRequest
{
/**
* @param int[]|null $childElementIds
*/
public function __construct(
public ?int $parentElementId,
public ?array $childElementIds,
) {
}
}