refactor(forms): Update store methods in various forms to set sort_order to 1 and increment existing entries for improved ordering consistency.

This commit is contained in:
Yoga Pangestu 2025-12-14 16:12:24 +07:00
parent a37135da2d
commit d7e870720f
6 changed files with 52 additions and 25 deletions

View File

@ -48,9 +48,12 @@ public function store(): void
$this->validate();
DB::transaction(function () {
// Increment all existing brands' sort_order
Brand::query()->increment('sort_order');
$brand = Brand::create([
'name' => $this->name,
'sort_order' => Brand::count() + 1,
'sort_order' => 1,
]);
$this->uploadMedia($this->image, $brand, 'image');

View File

@ -3,6 +3,7 @@
namespace App\Livewire\Forms\Studio\Catalog;
use App\Models\Category;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;
@ -42,11 +43,16 @@ public function store(): void
{
$this->validate();
Category::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => Category::count() + 1,
]);
DB::transaction(function () {
// Increment all existing categories' sort_order
Category::query()->increment('sort_order');
Category::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => 1,
]);
});
}
public function update(): void

View File

@ -3,6 +3,7 @@
namespace App\Livewire\Forms\Studio\Feature;
use App\Models\ChooseUs;
use Illuminate\Support\Facades\DB;
use Livewire\Form;
class ChooseUsForm extends Form
@ -41,11 +42,16 @@ public function store(): void
{
$this->validate();
ChooseUs::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => ChooseUs::count() + 1,
]);
DB::transaction(function () {
// Increment all existing choose us items' sort_order
ChooseUs::query()->increment('sort_order');
ChooseUs::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => 1,
]);
});
}
public function update(): void

View File

@ -3,6 +3,7 @@
namespace App\Livewire\Forms\Studio\Feature;
use App\Models\PerfumeFeature;
use Illuminate\Support\Facades\DB;
use Livewire\Form;
class PerfumeFeatureForm extends Form
@ -41,11 +42,16 @@ public function store(): void
{
$this->validate();
PerfumeFeature::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => PerfumeFeature::count() + 1,
]);
DB::transaction(function () {
// Increment all existing perfume features' sort_order
PerfumeFeature::query()->increment('sort_order');
PerfumeFeature::create([
'name' => $this->name,
'description' => $this->description,
'sort_order' => 1,
]);
});
}
public function update(): void

View File

@ -3,6 +3,7 @@
namespace App\Livewire\Forms\Studio\Information;
use App\Models\Faq;
use Illuminate\Support\Facades\DB;
use Livewire\Form;
class FaqForm extends Form
@ -41,11 +42,16 @@ public function store(): void
{
$this->validate();
Faq::create([
'question' => $this->question,
'answer' => $this->answer,
'sort_order' => Faq::count() + 1,
]);
DB::transaction(function () {
// Increment all existing faqs' sort_order
Faq::query()->increment('sort_order');
Faq::create([
'question' => $this->question,
'answer' => $this->answer,
'sort_order' => 1,
]);
});
}
public function update(): void

View File

@ -139,7 +139,7 @@ function mountCategoryComponent(User $user)
expect($category->sort_order)->toBe(1);
});
it('creates category with auto-incremented sort_order', function () {
it('creates category with sort_order as first position', function () {
CategoryModel::factory()->count(3)->create();
mountCategoryComponent($this->user)
@ -149,7 +149,7 @@ function mountCategoryComponent(User $user)
->assertHasNoErrors();
$newCategory = CategoryModel::where('name', 'Fourth Category')->first();
expect($newCategory->sort_order)->toBe(4);
expect($newCategory->sort_order)->toBe(1);
});
it('creates category with null description', function () {
@ -877,7 +877,7 @@ function mountCategoryComponent(User $user)
});
it('handles concurrent category creation correctly', function () {
// Create multiple categories in sequence to test sort_order increment
// Create multiple categories in sequence to test sort_order as first position
for ($i = 1; $i <= 5; $i++) {
mountCategoryComponent($this->user)
->call('openModal', 'create', 'Tambah Kategori')
@ -886,7 +886,7 @@ function mountCategoryComponent(User $user)
->assertHasNoErrors();
$category = CategoryModel::where('name', "Category {$i}")->first();
expect($category->sort_order)->toBe($i);
expect($category->sort_order)->toBe(1); // New categories always get sort_order = 1 (first position)
}
expect(CategoryModel::count())->toBe(5);