sarga/packages/Sarga/API/Http/Controllers/SearchController.php

96 lines
2.8 KiB
PHP
Raw Normal View History

2022-05-03 11:01:43 +00:00
<?php
namespace Sarga\API\Http\Controllers;
use Sarga\API\Http\Resources\Catalog\Suggestion;
use Sarga\Brand\Repositories\BrandRepository;
use Sarga\Shop\Repositories\CategoryRepository;
2022-05-03 11:37:31 +00:00
use Webkul\Category\Models\CategoryTranslationProxy;
2022-05-03 11:01:43 +00:00
use Webkul\Product\Repositories\ProductFlatRepository;
2022-05-04 10:08:36 +00:00
use Webkul\RestApi\Http\Controllers\V1\V1Controller;
2022-05-03 11:01:43 +00:00
2022-05-04 10:08:36 +00:00
class SearchController extends V1Controller
2022-05-03 11:01:43 +00:00
{
public function __construct(protected BrandRepository $brandRepository,
protected ProductFlatRepository $productFlatRepository,
protected CategoryRepository $categoryRepository)
{
}
public function index(){
$key = request('search');
if(!strlen($key)>= 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);
2022-07-06 10:19:07 +00:00
return Suggestion::collection($categories->merge($brands)->merge($products));
2022-05-03 11:01:43 +00:00
}
private function searchBrands($key){
2022-09-23 11:00:12 +00:00
$brands = $this->brandRepository->search($key);
2022-05-03 11:01:43 +00:00
if($brands->count()){
$brands->flatMap(fn ($val) => $val['suggestion_type']='brand');
}
return $brands;
}
2022-05-03 11:37:31 +00:00
private function searchCategories(){
$key = request('search');
2022-09-30 07:56:55 +00:00
$categories = CategoryTranslationProxy::modelClass()::select('category_id as id','meta_title as name')
2022-05-03 12:02:48 +00:00
->where('locale', core()->getRequestedLocaleCode())
->where('name', 'like', '%'.$key.'%')
2022-09-27 14:49:11 +00:00
->orWhere('meta_title', 'like', '%'.$key.'%')
2022-05-03 11:37:31 +00:00
->orderBy('name')
2022-09-27 14:49:11 +00:00
->take(10)
2022-05-03 11:01:43 +00:00
->get();
if($categories->count()){
$categories->flatMap(fn ($val) => $val['suggestion_type']='category');
}
return $categories;
}
private function searchProducts($key){
2022-09-29 14:13:48 +00:00
// $channel = core()->getRequestedChannelCode();
2022-05-03 11:01:43 +00:00
2022-09-29 14:13:48 +00:00
// $locale = core()->getRequestedLocaleCode();
2022-05-03 11:52:06 +00:00
$products = $this->productFlatRepository->getModel()::search(implode(' OR ', $key))
2022-09-29 14:13:48 +00:00
// ->where('channel', $channel)
// ->where('locale', $locale)
->where('status', 1)
->where('visible_individually', 1)
2022-05-03 11:01:43 +00:00
// ->addSelect(DB::raw("\'product\' as type" ))
2022-09-29 14:13:48 +00:00
->orderBy('name')
2022-09-29 14:37:49 +00:00
->take(10)
2022-09-29 14:13:48 +00:00
->query(fn ($query) => $query->select('id','name','product_id','description'))
2022-09-27 14:54:31 +00:00
// ->take(10)
2022-05-03 11:01:43 +00:00
->get();
if($products->count()){
$products->map(function ($item,$key) {
$item['suggestion_type'] = 'product';
return $item;
});
}
return $products;
}
}