From 9c217c4064183672ee4fe1b10a237746ae8a3435 Mon Sep 17 00:00:00 2001 From: merdan Date: Tue, 3 May 2022 16:01:43 +0500 Subject: [PATCH] category indexes --- .../Sarga/API/Http/Controllers/Products.php | 51 ---------- .../API/Http/Controllers/SearchController.php | 94 +++++++++++++++++++ packages/Sarga/API/Http/routes.php | 2 +- packages/Sarga/Shop/src/Models/Category.php | 20 ++++ .../src/Repositories/CategoryRepository.php | 1 - 5 files changed, 115 insertions(+), 53 deletions(-) create mode 100644 packages/Sarga/API/Http/Controllers/SearchController.php diff --git a/packages/Sarga/API/Http/Controllers/Products.php b/packages/Sarga/API/Http/Controllers/Products.php index 3f30403d2..5b256230b 100644 --- a/packages/Sarga/API/Http/Controllers/Products.php +++ b/packages/Sarga/API/Http/Controllers/Products.php @@ -117,57 +117,6 @@ class Products extends ProductController } - - public function suggestions(BrandRepository $brandRepository){ - - $key = request('search'); - - if(!strlen($key)>= 3){ - return response()->json(['message' => '3 karakterden kuchuk','status'=>false]); - } - - $queries = explode(' ', $key); - - $brands = $brandRepository->getModel()::search(implode(' OR ', $queries)) -// ->where('status',1) -// ->orderBy('name','asc') - ->take(10) - ->query(fn ($query) => $query->select('id','name')->orderBy('name')) - ->get(); - - if($brands->count()){ - $brands->flatMap(fn ($val) => $val['suggestion_type']='brand'); - } - -// $products = $this->productRepository->searchProductByAttribute($key); - - $channel = core()->getRequestedChannelCode(); - - $locale = core()->getRequestedLocaleCode(); - $products = app(ProductFlatRepository::class)->getModel()::search(implode(' OR ', $queries)) - ->where('status', 1) - ->where('visible_individually', 1) - ->where('channel', $channel) - ->where('locale', $locale) - ->take(10) - ->query(fn ($query) => $query->select('id','name','product_id') -// ->addSelect(DB::raw("\'product\' as type" )) - ->orderBy('name')) - ->get(); - - if($products->count()){ - $products->map(function ($item,$key) { - $item['suggestion_type'] = 'product'; - return $item; - }); - - } - - return Suggestion::collection($products->merge($brands)->sortBy('name')); - - } - - public function searchProducts(){ // return $this->productRepository->searchProductByAttribute(request('key')); return ProductResource::collection($this->productRepository->searchProductByAttribute(request('key'))); diff --git a/packages/Sarga/API/Http/Controllers/SearchController.php b/packages/Sarga/API/Http/Controllers/SearchController.php new file mode 100644 index 000000000..cf84cc9d2 --- /dev/null +++ b/packages/Sarga/API/Http/Controllers/SearchController.php @@ -0,0 +1,94 @@ += 3){ + return response()->json(['message' => '3 karakterden kuchuk','status'=>false]); + } + + $queries = explode(' ', $key); + + $brands =$this->searchBrands($queries); + + $products = $this->searchProducts($queries); + + $categories = $this->searchCategories($queries); + + return Suggestion::collection($products->merge($brands)->merge($categories)->sortBy('name')); + + } + + private function searchBrands($key){ + $brands = $this->brandRepository->getModel()::search(implode(' OR ', $key)) +// ->where('status',1) +// ->orderBy('name','asc') + ->take(10) + ->query(fn ($query) => $query->select('id','name')->orderBy('name')) + ->get(); + + if($brands->count()){ + $brands->flatMap(fn ($val) => $val['suggestion_type']='brand'); + } + + return $brands; + } + + private function searchCategories($key){ + $categories = $this->CategoryRepository->getModel()::search(implode(' OR ', $key)) + ->take(10) + ->query(fn ($query) => $query->select('id','name')->orderBy('name')) + ->get(); + + if($categories->count()){ + $categories->flatMap(fn ($val) => $val['suggestion_type']='category'); + } + + return $categories; + } + + private function searchProducts($key){ + + $channel = core()->getRequestedChannelCode(); + + $locale = core()->getRequestedLocaleCode(); + $products = $this->productRepository->getModel()::search(implode(' OR ', $key)) + ->where('status', 1) + ->where('visible_individually', 1) + ->where('channel', $channel) + ->where('locale', $locale) + ->take(10) + ->query(fn ($query) => $query->select('id','name','product_id') +// ->addSelect(DB::raw("\'product\' as type" )) + ->orderBy('name')) + ->get(); + + if($products->count()){ + $products->map(function ($item,$key) { + $item['suggestion_type'] = 'product'; + return $item; + }); + + } + + return $products; + } +} \ No newline at end of file diff --git a/packages/Sarga/API/Http/routes.php b/packages/Sarga/API/Http/routes.php index 8d3a03fc6..55ec1ca27 100644 --- a/packages/Sarga/API/Http/routes.php +++ b/packages/Sarga/API/Http/routes.php @@ -55,7 +55,7 @@ Route::group(['prefix' => 'api'], function () { Route::get('products-discounted', [Products::class, 'discountedProducts']); Route::get('products-popular', [Products::class, 'popularProducts']); Route::get('products-search', [Products::class, 'searchProducts']); - Route::get('suggestions', [Products::class, 'suggestions']); + Route::get('suggestions', [\Sarga\API\Http\Controllers\SearchController::class, 'index']); Route::get('products/{id}', [Products::class, 'get']); Route::get('products/{id}/variants', [Products::class, 'variants']); diff --git a/packages/Sarga/Shop/src/Models/Category.php b/packages/Sarga/Shop/src/Models/Category.php index c913ee61d..b05439b01 100644 --- a/packages/Sarga/Shop/src/Models/Category.php +++ b/packages/Sarga/Shop/src/Models/Category.php @@ -4,11 +4,13 @@ namespace Sarga\Shop\Models; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Laravel\Scout\Searchable; use Sarga\Brand\Models\BrandProxy; use Webkul\Category\Database\Factories\CategoryFactory; use Webkul\Category\Models\Category as WCategory; class Category extends WCategory { + use Searchable; /** * Fillables. * @@ -39,6 +41,24 @@ class Category extends WCategory * @var array */ protected $appends = ['image_url', 'category_icon_url']; + public function searchableAs() + { + return 'category_index'; + } + + public function toSearchableArray() + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'type' => 'category' + ]; + } + + public function shouldBeSearchable() + { + return $this->status; + } /** * Create a new factory instance for the model. diff --git a/packages/Webkul/Category/src/Repositories/CategoryRepository.php b/packages/Webkul/Category/src/Repositories/CategoryRepository.php index 42e12a863..f0b4e0fd6 100755 --- a/packages/Webkul/Category/src/Repositories/CategoryRepository.php +++ b/packages/Webkul/Category/src/Repositories/CategoryRepository.php @@ -72,7 +72,6 @@ class CategoryRepository extends Repository Event::dispatch('catalog.category.update.before', $id); - Log::info($data); $data = $this->setSameAttributeValueToAllLocale($data, 'slug'); $category->update($data);