AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollectionController.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Collection;
  4. use App\Models\Product;
  5. use App\Models\Store;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Validation\Rule;
  9. use Inertia\Inertia;
  10. use Inertia\Response;
  11. /**
  12. * The merchant's collections: groups of products such as "Sun care". Listed on the Products tab.
  13. */
  14. class CollectionController extends Controller
  15. {
  16. public const int MAX_PRODUCTS = 200;
  17. public function create(Request $request, string $slug): Response
  18. {
  19. $store = $this->ownedStore($request, $slug);
  20. return Inertia::render('stores/collections/form', [
  21. 'store' => ['name' => $store->name, 'slug' => $store->slug],
  22. 'collection' => null,
  23. 'products' => $this->productChoices($store),
  24. ]);
  25. }
  26. public function store(Request $request, string $slug): RedirectResponse
  27. {
  28. $store = $this->ownedStore($request, $slug);
  29. $data = $this->validated($request, $store);
  30. $collection = $store->collections()->create([
  31. 'name' => $data['name'],
  32. 'description' => $data['description'] ?? null,
  33. 'position' => (int) $store->collections()->max('position') + 1,
  34. ]);
  35. $this->syncProducts($collection, $data['product_ids'] ?? []);
  36. Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection ":name" created.', ['name' => $collection->name])]);
  37. return to_route('stores.products.index', $store->slug);
  38. }
  39. public function edit(Request $request, string $slug, int $collection): Response
  40. {
  41. $store = $this->ownedStore($request, $slug);
  42. $model = $this->ownedCollection($store, $collection);
  43. return Inertia::render('stores/collections/form', [
  44. 'store' => ['name' => $store->name, 'slug' => $store->slug],
  45. 'collection' => [
  46. 'id' => $model->id,
  47. 'name' => $model->name,
  48. 'slug' => $model->slug,
  49. 'description' => $model->description,
  50. 'product_ids' => $model->products->modelKeys(),
  51. ],
  52. 'products' => $this->productChoices($store),
  53. ]);
  54. }
  55. public function update(Request $request, string $slug, int $collection): RedirectResponse
  56. {
  57. $store = $this->ownedStore($request, $slug);
  58. $model = $this->ownedCollection($store, $collection);
  59. $data = $this->validated($request, $store);
  60. // The address (slug) stays the same, so menu links and shared links keep working after a rename.
  61. $model->fill(['name' => $data['name'], 'description' => $data['description'] ?? null])->save();
  62. $this->syncProducts($model, $data['product_ids'] ?? []);
  63. Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection saved.')]);
  64. return to_route('stores.products.index', $store->slug);
  65. }
  66. public function destroy(Request $request, string $slug, int $collection): RedirectResponse
  67. {
  68. $store = $this->ownedStore($request, $slug);
  69. // Products stay; only the group goes. A "shop by" section simply stops showing it.
  70. $this->ownedCollection($store, $collection)->delete();
  71. Inertia::flash('toast', ['type' => 'success', 'message' => __('Collection deleted.')]);
  72. return to_route('stores.products.index', $store->slug);
  73. }
  74. /**
  75. * @return array<string, mixed>
  76. */
  77. private function validated(Request $request, Store $store): array
  78. {
  79. return $request->validate([
  80. 'name' => ['required', 'string', 'min:2', 'max:60'],
  81. 'description' => ['nullable', 'string', 'max:500'],
  82. 'product_ids' => ['nullable', 'array', 'max:'.self::MAX_PRODUCTS],
  83. 'product_ids.*' => ['integer', Rule::exists('products', 'id')->where('store_id', $store->id)],
  84. ], [
  85. 'product_ids.*.exists' => __('Choose products from this store.'),
  86. ]);
  87. }
  88. /**
  89. * @param array<int, mixed> $ids
  90. */
  91. private function syncProducts(Collection $collection, array $ids): void
  92. {
  93. $positions = [];
  94. foreach (array_values(array_unique(array_map('intval', $ids))) as $position => $id) {
  95. $positions[$id] = ['position' => $position];
  96. }
  97. $collection->products()->sync($positions);
  98. }
  99. /**
  100. * @return list<array{id: int, name: string, image: string|null}>
  101. */
  102. private function productChoices(Store $store): array
  103. {
  104. return array_values($store->products()->with('images')->orderBy('name')->get()
  105. ->map(fn (Product $product) => [
  106. 'id' => $product->id,
  107. 'name' => $product->name,
  108. 'image' => $product->images->first()?->url(),
  109. ])
  110. ->all());
  111. }
  112. private function ownedStore(Request $request, string $slug): Store
  113. {
  114. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  115. }
  116. private function ownedCollection(Store $store, int $id): Collection
  117. {
  118. return $store->collections()->with('products')->whereKey($id)->firstOrFail();
  119. }
  120. }