add persisted set ordering
This commit is contained in:
parent
fde12ad561
commit
dc034bf393
9 changed files with 340 additions and 6 deletions
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Set;
|
||||
|
||||
use DomainException;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EloquentSetRepository implements SetRepository
|
||||
{
|
||||
|
|
@ -12,6 +13,7 @@ class EloquentSetRepository implements SetRepository
|
|||
'name' => $dto->name,
|
||||
'description' => $dto->description,
|
||||
'icon_image_url' => $dto->iconImageUrl,
|
||||
'sort_order' => $this->nextSortOrder(),
|
||||
]);
|
||||
|
||||
return $this->toDomain($model);
|
||||
|
|
@ -55,7 +57,7 @@ class EloquentSetRepository implements SetRepository
|
|||
|
||||
public function getAll(): array
|
||||
{
|
||||
$models = SetModel::orderBy('id')->get();
|
||||
$models = SetModel::orderBy('sort_order')->orderBy('id')->get();
|
||||
$sets = [];
|
||||
foreach ($models as $model) {
|
||||
$sets[] = $this->toDomain($model);
|
||||
|
|
@ -64,6 +66,30 @@ class EloquentSetRepository implements SetRepository
|
|||
return $sets;
|
||||
}
|
||||
|
||||
public function reorder(array $setIds): array
|
||||
{
|
||||
DB::transaction(function () use ($setIds): void {
|
||||
$sortOrder = 1;
|
||||
foreach ($setIds as $setId) {
|
||||
SetModel::where('id', $setId)
|
||||
->update(['sort_order' => $sortOrder]);
|
||||
$sortOrder++;
|
||||
}
|
||||
});
|
||||
|
||||
return $this->getAll();
|
||||
}
|
||||
|
||||
private function nextSortOrder(): int
|
||||
{
|
||||
$currentMaxSortOrder = SetModel::max('sort_order');
|
||||
if ($currentMaxSortOrder === null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (int) $currentMaxSortOrder + 1;
|
||||
}
|
||||
|
||||
private function toDomain(SetModel $model): Set
|
||||
{
|
||||
return new Set(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue