AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ProductController.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\ProductStatus;
  4. use App\Http\Requests\Products\SaveProductRequest;
  5. use App\Models\Product;
  6. use App\Models\ProductImage;
  7. use App\Models\Store;
  8. use Illuminate\Http\RedirectResponse;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\UploadedFile;
  11. use Illuminate\Validation\ValidationException;
  12. use Inertia\Inertia;
  13. use Inertia\Response;
  14. /**
  15. * The merchant's product list for one store.
  16. * Every lookup goes through the user's own stores, so other people's products give "not found".
  17. */
  18. class ProductController extends Controller
  19. {
  20. public function index(Request $request, string $slug): Response
  21. {
  22. $store = $this->ownedStore($request, $slug);
  23. return Inertia::render('stores/products/index', [
  24. 'store' => $this->presentStore($store),
  25. 'products' => $store->products()
  26. ->with('images')
  27. ->latest('id')
  28. ->get()
  29. ->map(fn (Product $product) => $this->present($product)),
  30. ]);
  31. }
  32. public function create(Request $request, string $slug): Response
  33. {
  34. return Inertia::render('stores/products/create', [
  35. 'store' => $this->presentStore($this->ownedStore($request, $slug)),
  36. 'statuses' => ProductStatus::options(),
  37. 'maxImages' => SaveProductRequest::MAX_IMAGES,
  38. ]);
  39. }
  40. public function store(SaveProductRequest $request, string $slug): RedirectResponse
  41. {
  42. $store = $this->ownedStore($request, $slug);
  43. $product = $store->products()->create($request->productData());
  44. $this->addImages($product, $request->file('images', []));
  45. Inertia::flash('toast', ['type' => 'success', 'message' => __('Product added.')]);
  46. return to_route('stores.products.index', $store->slug);
  47. }
  48. public function edit(Request $request, string $slug, int $product): Response
  49. {
  50. $store = $this->ownedStore($request, $slug);
  51. return Inertia::render('stores/products/edit', [
  52. 'store' => $this->presentStore($store),
  53. 'product' => $this->present($this->ownedProduct($store, $product)),
  54. 'statuses' => ProductStatus::options(),
  55. 'maxImages' => SaveProductRequest::MAX_IMAGES,
  56. ]);
  57. }
  58. public function update(SaveProductRequest $request, string $slug, int $product): RedirectResponse
  59. {
  60. $store = $this->ownedStore($request, $slug);
  61. $product = $this->ownedProduct($store, $product);
  62. $files = $request->file('images', []);
  63. if ($product->images()->count() + count($files) > SaveProductRequest::MAX_IMAGES) {
  64. throw ValidationException::withMessages([
  65. 'images' => 'A product can have at most '.SaveProductRequest::MAX_IMAGES.' photos. Remove one first.',
  66. ]);
  67. }
  68. $product->update($request->productData());
  69. $this->addImages($product, $files);
  70. Inertia::flash('toast', ['type' => 'success', 'message' => __('Product saved.')]);
  71. return to_route('stores.products.index', $store->slug);
  72. }
  73. public function destroy(Request $request, string $slug, int $product): RedirectResponse
  74. {
  75. $store = $this->ownedStore($request, $slug);
  76. $this->ownedProduct($store, $product)->delete();
  77. Inertia::flash('toast', ['type' => 'success', 'message' => __('Product deleted.')]);
  78. return to_route('stores.products.index', $store->slug);
  79. }
  80. public function destroyImage(Request $request, string $slug, int $product, int $image): RedirectResponse
  81. {
  82. $store = $this->ownedStore($request, $slug);
  83. $product = $this->ownedProduct($store, $product);
  84. $product->images()->whereKey($image)->firstOrFail()->delete();
  85. return back();
  86. }
  87. /**
  88. * @param array<int, UploadedFile> $files
  89. */
  90. private function addImages(Product $product, array $files): void
  91. {
  92. $position = (int) $product->images()->max('position');
  93. foreach ($files as $file) {
  94. $product->images()->create([
  95. 'store_id' => $product->store_id,
  96. 'path' => $file->store("products/{$product->store_id}", 'public'),
  97. 'position' => ++$position,
  98. ]);
  99. }
  100. }
  101. /**
  102. * The product fields the dashboard is allowed to see. Prices are in ringgit, as text, such as "12.90".
  103. *
  104. * @return array<string, mixed>
  105. */
  106. private function present(Product $product): array
  107. {
  108. return [
  109. 'id' => $product->id,
  110. 'name' => $product->name,
  111. 'slug' => $product->slug,
  112. 'description' => $product->description,
  113. 'price' => number_format($product->price / 100, 2, '.', ''),
  114. 'compare_at_price' => $product->compare_at_price !== null
  115. ? number_format($product->compare_at_price / 100, 2, '.', '')
  116. : null,
  117. 'stock' => $product->stock,
  118. 'status' => $product->status->value,
  119. 'status_label' => $product->status->label(),
  120. 'images' => $product->images
  121. ->map(fn (ProductImage $image) => ['id' => $image->id, 'url' => $image->url()])
  122. ->all(),
  123. ];
  124. }
  125. /**
  126. * @return array{name: string, slug: string}
  127. */
  128. private function presentStore(Store $store): array
  129. {
  130. return ['name' => $store->name, 'slug' => $store->slug];
  131. }
  132. private function ownedStore(Request $request, string $slug): Store
  133. {
  134. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  135. }
  136. private function ownedProduct(Store $store, int $id): Product
  137. {
  138. return $store->products()->with('images')->whereKey($id)->firstOrFail();
  139. }
  140. }