allowedIncludes(), trash: Product::class) ->paginate($perPage) ->withQueryString(); return ProductResource::collection($items); } /** * A response rather than the resource: it carries the `ETag` writes need, * and is a 304 when the client already holds this version. */ #[Authorize('view', 'product')] public function show(Request $request, Product $product): JsonResponse { $includes = $this->includes($request); return Precondition::tagged( (new ProductResource($product->load($includes)))->response(), $request, $product, ['tags'], embeds: $includes !== [], ); } /** * Store a newly created resource in storage. */ #[Authorize('create', Product::class)] public function store(StoreProductRequest $request, StoreProduct $action): JsonResponse { /** @var Product $product */ $product = $action->handle($request->validated(), $this->uploadedPhotos($request), $this->uploadedSizeChart($request)); // Read back as stored, with where it lives and its version, so the first // conditional write needs no read. return (new ProductResource($product->refresh()->load($this->includes($request)))) ->response() ->setStatusCode(201) ->header('Location', route('api.v1.products.show', $product)) ->header('ETag', Precondition::etag($product, ['tags'])); } /** * The form's lost-update guard over HTTP: `If-Match` against the ETag, * answered before the body is read and again under the write's row lock. */ #[Authorize('update', 'product')] #[Middleware(EvaluatePreconditions::class.':product,tags')] public function update(UpdateProductRequest $request, Product $product, UpdateProduct $action): JsonResponse { $data = $request->validated(); if (! $this->canEditRestricted()) { unset($data['price']); } $action->handle($product, $data, $this->uploadedPhotos($request), $this->uploadedSizeChart($request), expected: Precondition::expected($request)); // Read back as stored, once: the body and the ETag describe the same // state, and neither the request's raw values. $product->refresh(); return (new ProductResource($product->load($this->includes($request)))) ->response() ->header('ETag', Precondition::etag($product, ['tags'])); } /** Conditional too: deleting a record somebody just edited destroys their change. */ #[Authorize('delete', 'product')] #[Middleware(EvaluatePreconditions::class.':product,tags')] public function destroy(Request $request, Product $product, DeleteProduct $action): Response { $action->handle($product, expected: Precondition::expected($request)); return response()->noContent(); } /** * Relations a client may embed with `?include`. An allow-list, so a caller * cannot walk the object graph. * * @return list */ private function allowedIncludes(): array { return ['category', 'publishedByRef', 'productVariants', 'reviews', 'coupons', 'tags']; } /** * The requested subset of {@see allowedIncludes()}, for single-record responses. * * @return list */ private function includes(Request $request): array { return Includes::requested($request, $this->allowedIncludes()); } /** * Whether the current user may change this module's restricted fields. */ private function canEditRestricted(): bool { return Gate::allows('updateRestricted', Product::class); } /** * Get the files uploaded for the field, in the order they were sent. * * @return list */ private function uploadedPhotos(Request $request): array { return array_values(array_filter( (array) $request->file('photos_new', []), static fn (mixed $file): bool => $file instanceof UploadedFile, )); } private function uploadedSizeChart(Request $request): ?UploadedFile { $file = $request->file('size_chart'); return $file instanceof UploadedFile ? $file : null; } }