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:
parent
a37135da2d
commit
d7e870720f
@ -48,9 +48,12 @@ public function store(): void
|
|||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
DB::transaction(function () {
|
DB::transaction(function () {
|
||||||
|
// Increment all existing brands' sort_order
|
||||||
|
Brand::query()->increment('sort_order');
|
||||||
|
|
||||||
$brand = Brand::create([
|
$brand = Brand::create([
|
||||||
'name' => $this->name,
|
'name' => $this->name,
|
||||||
'sort_order' => Brand::count() + 1,
|
'sort_order' => 1,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->uploadMedia($this->image, $brand, 'image');
|
$this->uploadMedia($this->image, $brand, 'image');
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Forms\Studio\Catalog;
|
namespace App\Livewire\Forms\Studio\Catalog;
|
||||||
|
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
use Livewire\Form;
|
use Livewire\Form;
|
||||||
|
|
||||||
@ -42,11 +43,16 @@ public function store(): void
|
|||||||
{
|
{
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
Category::create([
|
DB::transaction(function () {
|
||||||
'name' => $this->name,
|
// Increment all existing categories' sort_order
|
||||||
'description' => $this->description,
|
Category::query()->increment('sort_order');
|
||||||
'sort_order' => Category::count() + 1,
|
|
||||||
]);
|
Category::create([
|
||||||
|
'name' => $this->name,
|
||||||
|
'description' => $this->description,
|
||||||
|
'sort_order' => 1,
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(): void
|
public function update(): void
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Forms\Studio\Feature;
|
namespace App\Livewire\Forms\Studio\Feature;
|
||||||
|
|
||||||
use App\Models\ChooseUs;
|
use App\Models\ChooseUs;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Livewire\Form;
|
use Livewire\Form;
|
||||||
|
|
||||||
class ChooseUsForm extends Form
|
class ChooseUsForm extends Form
|
||||||
@ -41,11 +42,16 @@ public function store(): void
|
|||||||
{
|
{
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
ChooseUs::create([
|
DB::transaction(function () {
|
||||||
'name' => $this->name,
|
// Increment all existing choose us items' sort_order
|
||||||
'description' => $this->description,
|
ChooseUs::query()->increment('sort_order');
|
||||||
'sort_order' => ChooseUs::count() + 1,
|
|
||||||
]);
|
ChooseUs::create([
|
||||||
|
'name' => $this->name,
|
||||||
|
'description' => $this->description,
|
||||||
|
'sort_order' => 1,
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(): void
|
public function update(): void
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Forms\Studio\Feature;
|
namespace App\Livewire\Forms\Studio\Feature;
|
||||||
|
|
||||||
use App\Models\PerfumeFeature;
|
use App\Models\PerfumeFeature;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Livewire\Form;
|
use Livewire\Form;
|
||||||
|
|
||||||
class PerfumeFeatureForm extends Form
|
class PerfumeFeatureForm extends Form
|
||||||
@ -41,11 +42,16 @@ public function store(): void
|
|||||||
{
|
{
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
PerfumeFeature::create([
|
DB::transaction(function () {
|
||||||
'name' => $this->name,
|
// Increment all existing perfume features' sort_order
|
||||||
'description' => $this->description,
|
PerfumeFeature::query()->increment('sort_order');
|
||||||
'sort_order' => PerfumeFeature::count() + 1,
|
|
||||||
]);
|
PerfumeFeature::create([
|
||||||
|
'name' => $this->name,
|
||||||
|
'description' => $this->description,
|
||||||
|
'sort_order' => 1,
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(): void
|
public function update(): void
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Forms\Studio\Information;
|
namespace App\Livewire\Forms\Studio\Information;
|
||||||
|
|
||||||
use App\Models\Faq;
|
use App\Models\Faq;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
use Livewire\Form;
|
use Livewire\Form;
|
||||||
|
|
||||||
class FaqForm extends Form
|
class FaqForm extends Form
|
||||||
@ -41,11 +42,16 @@ public function store(): void
|
|||||||
{
|
{
|
||||||
$this->validate();
|
$this->validate();
|
||||||
|
|
||||||
Faq::create([
|
DB::transaction(function () {
|
||||||
'question' => $this->question,
|
// Increment all existing faqs' sort_order
|
||||||
'answer' => $this->answer,
|
Faq::query()->increment('sort_order');
|
||||||
'sort_order' => Faq::count() + 1,
|
|
||||||
]);
|
Faq::create([
|
||||||
|
'question' => $this->question,
|
||||||
|
'answer' => $this->answer,
|
||||||
|
'sort_order' => 1,
|
||||||
|
]);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(): void
|
public function update(): void
|
||||||
|
|||||||
@ -139,7 +139,7 @@ function mountCategoryComponent(User $user)
|
|||||||
expect($category->sort_order)->toBe(1);
|
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();
|
CategoryModel::factory()->count(3)->create();
|
||||||
|
|
||||||
mountCategoryComponent($this->user)
|
mountCategoryComponent($this->user)
|
||||||
@ -149,7 +149,7 @@ function mountCategoryComponent(User $user)
|
|||||||
->assertHasNoErrors();
|
->assertHasNoErrors();
|
||||||
|
|
||||||
$newCategory = CategoryModel::where('name', 'Fourth Category')->first();
|
$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 () {
|
it('creates category with null description', function () {
|
||||||
@ -877,7 +877,7 @@ function mountCategoryComponent(User $user)
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('handles concurrent category creation correctly', function () {
|
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++) {
|
for ($i = 1; $i <= 5; $i++) {
|
||||||
mountCategoryComponent($this->user)
|
mountCategoryComponent($this->user)
|
||||||
->call('openModal', 'create', 'Tambah Kategori')
|
->call('openModal', 'create', 'Tambah Kategori')
|
||||||
@ -886,7 +886,7 @@ function mountCategoryComponent(User $user)
|
|||||||
->assertHasNoErrors();
|
->assertHasNoErrors();
|
||||||
|
|
||||||
$category = CategoryModel::where('name', "Category {$i}")->first();
|
$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);
|
expect(CategoryModel::count())->toBe(5);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user