wip
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Barcode;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Barcode\Requests\ProductBarcodeSearchIndexRequest;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ProductBarcodeSearchController extends Controller
|
||||
{
|
||||
/**
|
||||
* Search product via barcode
|
||||
*/
|
||||
public function index(ProductBarcodeSearchIndexRequest $request): JsonResponse
|
||||
{
|
||||
$product = Product::where('barcode', $request->barcode)->with('media')->first();
|
||||
|
||||
return response()->rest([
|
||||
'id' => $product->id,
|
||||
'name' => $product->name,
|
||||
'stock' => $product->stock,
|
||||
'price_amount' => $product->price_amount,
|
||||
'image' => [
|
||||
'thumbnail' => $product->thumbnail(),
|
||||
'images_400x400' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb400x400')),
|
||||
'images_720x720' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb720x720')),
|
||||
'images_800x800' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb800x800')),
|
||||
'images_1200x1200' => $product->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb1200x1200')),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Barcode\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ProductBarcodeSearchIndexRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'barcode' => ['required', 'string', 'max:255', 'exists:products,barcode'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Product\ProductCommentStore;
|
||||
use App\Http\Resources\Api\V1\Common\CommentResource;
|
||||
use App\Models\Common\Comment;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProductCommentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Product's comments
|
||||
*/
|
||||
public function index(Request $request, Product $product): JsonResponse
|
||||
{
|
||||
return response()->rest(
|
||||
CommentResource::collection(
|
||||
$product->comments()->with('user')->where('active', true)->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store
|
||||
*/
|
||||
public function store(ProductCommentStore $request, Product $product): JsonResponse
|
||||
{
|
||||
$product->comments()->create([
|
||||
'comment' => $request->comment,
|
||||
'user_id' => auth()->id(),
|
||||
]);
|
||||
|
||||
return response()->rest(message: 'Comment added successfully', code: 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the comment from product
|
||||
*/
|
||||
public function destroy(Product $product, Comment $comment): JsonResponse
|
||||
{
|
||||
$product->comments()->where('comments.id', $comment->id)->delete();
|
||||
|
||||
return response()->rest(message: 'Comment removed successfully from product', code: 204);
|
||||
}
|
||||
}
|
||||
52
app/Http/Controllers/Api/V1/Product/ProductController.php
Normal file
52
app/Http/Controllers/Api/V1/Product/ProductController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductIndexResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductShowResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Product\ProductIndexRequest;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Products (index)
|
||||
*/
|
||||
public function index(ProductIndexRequest $request): JsonResponse
|
||||
{
|
||||
return response()->rest_paginate(
|
||||
ProductIndexResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->applyBasicQueries()
|
||||
->applyFilters()
|
||||
->applySorting()
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Products (show)
|
||||
*/
|
||||
public function show(Product $product): JsonResponse
|
||||
{
|
||||
$product->load([
|
||||
'channels:id,name',
|
||||
'properties',
|
||||
'media' => function ($query) {
|
||||
$query->orderBy('order_column', 'asc');
|
||||
},
|
||||
'variations' => [
|
||||
'media',
|
||||
'properties',
|
||||
],
|
||||
'reviews',
|
||||
'categories:id,name',
|
||||
]);
|
||||
|
||||
return response()->rest(new ProductShowResource($product));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ProductRelatedController extends Controller
|
||||
{
|
||||
/**
|
||||
* Related products (index)
|
||||
*/
|
||||
public function index(Product $product): JsonResponse
|
||||
{
|
||||
$products = DB::table('product_has_relations')
|
||||
->select('product_has_relations.product_id')
|
||||
->whereIn('product_has_relations.productable_id', (function ($query) use ($product) {
|
||||
$query->from('product_has_relations')
|
||||
->select('productable_id')
|
||||
->distinct('productable_id')
|
||||
->where('productable_type', '=', 'category')
|
||||
->where('product_id', '=', $product->id);
|
||||
}))
|
||||
->limit(12)
|
||||
->orderByRaw('RANDOM()')
|
||||
->pluck('product_has_relations.product_id')
|
||||
->unique();
|
||||
|
||||
return response()->rest(
|
||||
ProductResource::collection(
|
||||
ProductRepository::make(request())
|
||||
->applyBasicQueries()
|
||||
->whereIntegerInRaw('id', $products->toArray())
|
||||
->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Review\ProductReviewResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\Product\Review\ProductReviewStore;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class ProductReviewController extends Controller
|
||||
{
|
||||
/**
|
||||
* Product reviews (index)
|
||||
*/
|
||||
public function index(Product $product): JsonResponse
|
||||
{
|
||||
return response()->rest(
|
||||
ProductReviewResource::collection(
|
||||
$product->reviews()
|
||||
->where('is_visible', true)
|
||||
->orderBy('is_recommended')
|
||||
->get()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* (*) Product reviews (store)
|
||||
*/
|
||||
public function store(ProductReviewStore $request, Product $product): JsonResponse
|
||||
{
|
||||
$product->reviews()->create([
|
||||
'user_id' => auth()->id(),
|
||||
...$request->validated(),
|
||||
]);
|
||||
|
||||
return response()->rest(message: 'Review added successfully', code: 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources\Attribute;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductAttributeProductShowResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'attribute_id' => $this->attribute->id,
|
||||
'name' => $this->attribute->name,
|
||||
'value' => $this->values->map(fn ($val) => $val->real_value)->first(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources\Attribute;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductAttributeResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'attribute_id' => $this->attribute->id,
|
||||
'name' => $this->attribute->name,
|
||||
'value' => $this->values->map(fn ($val) => $val->real_value)->first(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources;
|
||||
|
||||
use App\Http\Resources\MediaResource;
|
||||
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductIndexResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
$reviews = $this->whenLoaded('reviews');
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'parent_id' => $this->parent_id,
|
||||
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => '',
|
||||
|
||||
'sku' => $this->sku,
|
||||
'barcode' => $this->barcode,
|
||||
'stock' => $this->stock,
|
||||
|
||||
'price_amount' => $this->price_amount,
|
||||
'old_price_amount' => $this->old_price_amount,
|
||||
|
||||
'backorder' => $this->backorder,
|
||||
'weight_value' => $this->weight_value,
|
||||
'weight_unit' => $this->weight_unit,
|
||||
'height_value' => $this->height_value,
|
||||
'height_unit' => $this->height_unit,
|
||||
|
||||
'media' => MediaResource::collection($this->whenLoaded('media')),
|
||||
|
||||
'created_at' => $this->created_at,
|
||||
|
||||
'seo_title' => $this->seo_title,
|
||||
'seo_description' => $this->seo_description,
|
||||
|
||||
'colour' => PropertyRepository::getRealValue('colour', $this->colour),
|
||||
'size' => PropertyRepository::getRealValue('size', $this->size),
|
||||
|
||||
'brand' => [
|
||||
'id' => $this->brand_id,
|
||||
'name' => $this->brand?->name,
|
||||
],
|
||||
|
||||
'reviews' => [
|
||||
'count' => $reviews ? $reviews->count() : 0,
|
||||
'rating' => $reviews ? number_format($reviews->avg('rating'), 2) : 0,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductMediaResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'thumbnail' => $this->getUrl('thumb288x431'),
|
||||
'images_400x400' => $this->getUrl('thumb400x400'),
|
||||
'images_720x720' => $this->getUrl('thumb720x720'),
|
||||
'images_800x800' => $this->getUrl('thumb800x800'),
|
||||
'images_1200x1200' => $this->getUrl('thumb1200x1200'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Attribute\ProductAttributeProductShowResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Variant\ProductVariantResource;
|
||||
use App\Http\Resources\Api\V1\Channel\ChannelResource;
|
||||
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
$media = $this->whenLoaded('media');
|
||||
$reviews = $this->whenLoaded('reviews');
|
||||
|
||||
$reviewsLoaded = get_class($reviews) !== 'Illuminate\\Http\\Resources\\MissingValue';
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'parent_id' => $this->parent_id,
|
||||
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
|
||||
'sku' => $this->sku,
|
||||
'barcode' => $this->barcode,
|
||||
'stock' => $this->stock,
|
||||
|
||||
'price_amount' => $this->price_amount,
|
||||
'old_price_amount' => $this->old_price_amount,
|
||||
|
||||
'backorder' => $this->backorder,
|
||||
'weight_value' => $this->weight_value,
|
||||
'weight_unit' => $this->weight_unit,
|
||||
'height_value' => $this->height_value,
|
||||
'height_unit' => $this->height_unit,
|
||||
|
||||
'media' => ProductMediaResource::collection($media),
|
||||
|
||||
'created_at' => $this->created_at,
|
||||
|
||||
'seo_title' => $this->seo_title,
|
||||
'seo_description' => $this->seo_description,
|
||||
|
||||
'is_visible' => $this->is_visible,
|
||||
|
||||
'colour' => PropertyRepository::getRealValue('colour', $this->colour),
|
||||
'size' => PropertyRepository::getRealValue('size', $this->size),
|
||||
|
||||
'brand' => [
|
||||
'id' => $this->brand_id,
|
||||
'name' => $this->brand?->name,
|
||||
],
|
||||
|
||||
'channel' => ChannelResource::collection($this->whenLoaded('channels')),
|
||||
|
||||
'properties' => ProductAttributeProductShowResource::collection($this->whenLoaded('properties')),
|
||||
'variations' => ProductVariantResource::collection($this->whenLoaded('variations')),
|
||||
|
||||
'reviews' => [
|
||||
'count' => $reviewsLoaded ? $reviews->count() : 0,
|
||||
'rating' => $reviewsLoaded ? number_format($reviews->avg('rating'), 2) : '',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Attribute\ProductAttributeProductShowResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Review\ProductReviewResource;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Variant\ProductVariantResource;
|
||||
use App\Http\Resources\Api\V1\Channel\ChannelResource;
|
||||
use App\Http\Resources\MediaResource;
|
||||
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductShowResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
$reviews = $this->whenLoaded('reviews');
|
||||
$categories = $this->whenLoaded('categories');
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'parent_id' => $this->parent_id,
|
||||
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
|
||||
'sku' => $this->sku,
|
||||
'barcode' => $this->barcode,
|
||||
'stock' => $this->stock,
|
||||
|
||||
'price_amount' => $this->price_amount,
|
||||
'old_price_amount' => $this->old_price_amount,
|
||||
|
||||
'backorder' => $this->backorder ? 'true' : 'false',
|
||||
'weight_value' => $this->weight_value,
|
||||
'weight_unit' => $this->weight_unit,
|
||||
'height_value' => $this->height_value,
|
||||
'height_unit' => $this->height_unit,
|
||||
|
||||
'media' => MediaResource::collection($this->whenLoaded('media')),
|
||||
|
||||
'created_at' => $this->created_at,
|
||||
|
||||
'seo_title' => $this->seo_title,
|
||||
'seo_description' => $this->seo_description,
|
||||
|
||||
'colour' => PropertyRepository::getRealValue('colour', $this->colour),
|
||||
'size' => PropertyRepository::getRealValue('size', $this->size),
|
||||
|
||||
'available_colors' => PropertyRepository::getAvailableColors($this),
|
||||
'available_sizes' => PropertyRepository::getAvailableSizes($this),
|
||||
|
||||
'brand' => [
|
||||
'id' => $this->brand_id,
|
||||
'name' => $this->brand?->name,
|
||||
],
|
||||
|
||||
'channel' => ChannelResource::collection($this->whenLoaded('channels')),
|
||||
|
||||
'properties' => ProductAttributeProductShowResource::collection($this->whenLoaded('properties')),
|
||||
'variations' => ProductVariantResource::collection($this->whenLoaded('variations')),
|
||||
'reviews' => [
|
||||
'count' => $reviews ? $reviews->count() : 0,
|
||||
'rating' => $reviews ? number_format($reviews->avg('rating'), 2) : 0,
|
||||
],
|
||||
|
||||
'reviews_resources' => ProductReviewResource::collection($this->whenLoaded('reviews')),
|
||||
|
||||
$this->mergeWhen($categories, [
|
||||
'categories' => $categories->map(fn ($category) => [
|
||||
'id' => $category->id,
|
||||
'name' => $category->name,
|
||||
]),
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources\Review;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductReviewResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'user_id' => $this->user_id,
|
||||
'product_id' => $this->product_id,
|
||||
'rating' => $this->rating,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'is_recommended' => $this->is_recommended,
|
||||
'source' => $this->source,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Resources\Variant;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Attribute\ProductAttributeResource;
|
||||
use App\Repositories\Ecommerce\Product\Property\PropertyRepository;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ProductVariantResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'parent_id' => $this->parent_id,
|
||||
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
|
||||
'sku' => $this->sku,
|
||||
'barcode' => $this->barcode,
|
||||
'stock' => $this->stock,
|
||||
|
||||
'cost_amount' => $this->cost_amount,
|
||||
'price_amount' => $this->price_amount,
|
||||
'old_price_amount' => $this->old_price_amount,
|
||||
|
||||
'backorder' => $this->backorder,
|
||||
'weight_value' => $this->weight_value,
|
||||
'weight_unit' => $this->weight_unit,
|
||||
'height_value' => $this->height_value,
|
||||
'height_unit' => $this->height_unit,
|
||||
|
||||
'colour' => PropertyRepository::getRealValue('colour', $this->colour),
|
||||
'size' => PropertyRepository::getRealValue('size', $this->size),
|
||||
|
||||
'thumbnail' => $this->thumbnail(),
|
||||
'images_400x400' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb400x400')),
|
||||
'images_800x800' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb800x800')),
|
||||
'images_1200x1200' => $this->getMedia('uploads')->map(fn ($media) => $media->getUrl('thumb1200x1200')),
|
||||
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
|
||||
'properties' => ProductAttributeResource::collection($this->whenLoaded('properties')),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Product\Search;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\ProductMediaResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProductSearchController extends Controller
|
||||
{
|
||||
/**
|
||||
* Search product via barcode
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'q' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$searchQuery = $request->input('q');
|
||||
|
||||
$products = Product::with(['brand', 'media'])
|
||||
->where(function ($query) use ($searchQuery) {
|
||||
if (is_numeric($searchQuery)) {
|
||||
$query->where('products.id', intval($searchQuery));
|
||||
}
|
||||
|
||||
$query
|
||||
->orWhere('products.name', '~*', $searchQuery)
|
||||
->orWhere('products.sku', '~*', $searchQuery)
|
||||
->orWhere('products.barcode', '~*', $searchQuery);
|
||||
})
|
||||
->when($request->isNotFilled('internal'), function ($query) {
|
||||
$query->where('products.is_visible', true)
|
||||
->where('products.parent_id', null)
|
||||
->where('products.stock', '>', 0);
|
||||
})
|
||||
->limit(40)
|
||||
->get();
|
||||
|
||||
return response()->rest(
|
||||
$products->map(fn ($product) => [
|
||||
'id' => $product->id,
|
||||
'name' => $product->fullName,
|
||||
'stock' => $product->stock,
|
||||
'cost_amount' => $product->cost_amount,
|
||||
'price_amount' => $product->price_amount,
|
||||
'brand' => [
|
||||
'id' => $product->brand?->id,
|
||||
'name' => $product->brand?->name,
|
||||
],
|
||||
'thumbnail' => $product->thumbnail(),
|
||||
'media' => ProductMediaResource::collection($product->media),
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user