| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Enums\ProductStatus;
- use App\Http\Requests\Products\SaveProductRequest;
- use App\Models\Product;
- use App\Models\ProductImage;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Validation\ValidationException;
- use Inertia\Inertia;
- use Inertia\Response;
-
- /**
- * The merchant's product list for one store.
- * Every lookup goes through the user's own stores, so other people's products give "not found".
- */
- class ProductController extends Controller
- {
- public function index(Request $request, string $slug): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- return Inertia::render('stores/products/index', [
- 'store' => $this->presentStore($store),
- 'products' => $store->products()
- ->with('images')
- ->latest('id')
- ->get()
- ->map(fn (Product $product) => $this->present($product)),
- ]);
- }
-
- public function create(Request $request, string $slug): Response
- {
- return Inertia::render('stores/products/create', [
- 'store' => $this->presentStore($this->ownedStore($request, $slug)),
- 'statuses' => ProductStatus::options(),
- 'maxImages' => SaveProductRequest::MAX_IMAGES,
- ]);
- }
-
- public function store(SaveProductRequest $request, string $slug): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $product = $store->products()->create($request->productData());
- $this->addImages($product, $request->file('images', []));
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Product added.')]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- public function edit(Request $request, string $slug, int $product): Response
- {
- $store = $this->ownedStore($request, $slug);
-
- return Inertia::render('stores/products/edit', [
- 'store' => $this->presentStore($store),
- 'product' => $this->present($this->ownedProduct($store, $product)),
- 'statuses' => ProductStatus::options(),
- 'maxImages' => SaveProductRequest::MAX_IMAGES,
- ]);
- }
-
- public function update(SaveProductRequest $request, string $slug, int $product): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $product = $this->ownedProduct($store, $product);
- $files = $request->file('images', []);
-
- if ($product->images()->count() + count($files) > SaveProductRequest::MAX_IMAGES) {
- throw ValidationException::withMessages([
- 'images' => 'A product can have at most '.SaveProductRequest::MAX_IMAGES.' photos. Remove one first.',
- ]);
- }
-
- $product->update($request->productData());
- $this->addImages($product, $files);
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Product saved.')]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- public function destroy(Request $request, string $slug, int $product): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
-
- $this->ownedProduct($store, $product)->delete();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Product deleted.')]);
-
- return to_route('stores.products.index', $store->slug);
- }
-
- public function destroyImage(Request $request, string $slug, int $product, int $image): RedirectResponse
- {
- $store = $this->ownedStore($request, $slug);
- $product = $this->ownedProduct($store, $product);
-
- $product->images()->whereKey($image)->firstOrFail()->delete();
-
- return back();
- }
-
- /**
- * @param array<int, UploadedFile> $files
- */
- private function addImages(Product $product, array $files): void
- {
- $position = (int) $product->images()->max('position');
-
- foreach ($files as $file) {
- $product->images()->create([
- 'store_id' => $product->store_id,
- 'path' => $file->store("products/{$product->store_id}", 'public'),
- 'position' => ++$position,
- ]);
- }
- }
-
- /**
- * The product fields the dashboard is allowed to see. Prices are in ringgit, as text, such as "12.90".
- *
- * @return array<string, mixed>
- */
- private function present(Product $product): array
- {
- return [
- 'id' => $product->id,
- 'name' => $product->name,
- 'slug' => $product->slug,
- 'description' => $product->description,
- 'price' => number_format($product->price / 100, 2, '.', ''),
- 'compare_at_price' => $product->compare_at_price !== null
- ? number_format($product->compare_at_price / 100, 2, '.', '')
- : null,
- 'stock' => $product->stock,
- 'status' => $product->status->value,
- 'status_label' => $product->status->label(),
- 'images' => $product->images
- ->map(fn (ProductImage $image) => ['id' => $image->id, 'url' => $image->url()])
- ->all(),
- ];
- }
-
- /**
- * @return array{name: string, slug: string}
- */
- private function presentStore(Store $store): array
- {
- return ['name' => $store->name, 'slug' => $store->slug];
- }
-
- private function ownedStore(Request $request, string $slug): Store
- {
- return $request->user()->stores()->where('slug', $slug)->firstOrFail();
- }
-
- private function ownedProduct(Store $store, int $id): Product
- {
- return $store->products()->with('images')->whereKey($id)->firstOrFail();
- }
- }
|