From d7e870720f30fd4a15e9de80f7ce8dfcaaf8647b Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Sun, 14 Dec 2025 16:12:24 +0700 Subject: [PATCH] refactor(forms): Update store methods in various forms to set sort_order to 1 and increment existing entries for improved ordering consistency. --- app/Livewire/Forms/Studio/Catalog/BrandForm.php | 5 ++++- .../Forms/Studio/Catalog/CategoryForm.php | 16 +++++++++++----- .../Forms/Studio/Feature/ChooseUsForm.php | 16 +++++++++++----- .../Forms/Studio/Feature/PerfumeFeatureForm.php | 16 +++++++++++----- .../Forms/Studio/Information/FaqForm.php | 16 +++++++++++----- .../Livewire/Studio/Catalog/CategoryTest.php | 8 ++++---- 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/app/Livewire/Forms/Studio/Catalog/BrandForm.php b/app/Livewire/Forms/Studio/Catalog/BrandForm.php index b7f9d73..b063608 100644 --- a/app/Livewire/Forms/Studio/Catalog/BrandForm.php +++ b/app/Livewire/Forms/Studio/Catalog/BrandForm.php @@ -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'); diff --git a/app/Livewire/Forms/Studio/Catalog/CategoryForm.php b/app/Livewire/Forms/Studio/Catalog/CategoryForm.php index 71fbfb3..c444db6 100644 --- a/app/Livewire/Forms/Studio/Catalog/CategoryForm.php +++ b/app/Livewire/Forms/Studio/Catalog/CategoryForm.php @@ -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 diff --git a/app/Livewire/Forms/Studio/Feature/ChooseUsForm.php b/app/Livewire/Forms/Studio/Feature/ChooseUsForm.php index a1f1f05..af35e70 100644 --- a/app/Livewire/Forms/Studio/Feature/ChooseUsForm.php +++ b/app/Livewire/Forms/Studio/Feature/ChooseUsForm.php @@ -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 diff --git a/app/Livewire/Forms/Studio/Feature/PerfumeFeatureForm.php b/app/Livewire/Forms/Studio/Feature/PerfumeFeatureForm.php index 326f5a1..e7edd55 100644 --- a/app/Livewire/Forms/Studio/Feature/PerfumeFeatureForm.php +++ b/app/Livewire/Forms/Studio/Feature/PerfumeFeatureForm.php @@ -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 diff --git a/app/Livewire/Forms/Studio/Information/FaqForm.php b/app/Livewire/Forms/Studio/Information/FaqForm.php index b89f53c..7177a82 100644 --- a/app/Livewire/Forms/Studio/Information/FaqForm.php +++ b/app/Livewire/Forms/Studio/Information/FaqForm.php @@ -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 diff --git a/tests/Feature/Livewire/Studio/Catalog/CategoryTest.php b/tests/Feature/Livewire/Studio/Catalog/CategoryTest.php index 50ac8bc..dcee950 100644 --- a/tests/Feature/Livewire/Studio/Catalog/CategoryTest.php +++ b/tests/Feature/Livewire/Studio/Catalog/CategoryTest.php @@ -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);