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

102 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Sarga\API\Http\Controllers;
2022-01-28 10:01:59 +00:00
use Sarga\API\Http\Resources\Catalog\Brand;
2022-01-19 14:04:47 +00:00
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
2022-04-13 15:28:39 +00:00
use Sarga\API\Http\Resources\Core\Source;
use Sarga\API\Http\Resources\Core\Vendor;
use Sarga\Shop\Repositories\ProductRepository;
2022-01-28 10:01:59 +00:00
use Sarga\Brand\Repositories\BrandRepository;
2022-01-19 14:04:47 +00:00
use Sarga\Shop\Repositories\CategoryRepository;
2022-01-28 10:01:59 +00:00
use Sarga\Shop\Repositories\VendorRepository;
use Webkul\API\Http\Controllers\Shop\Controller;
use Webkul\Marketplace\Repositories\SellerRepository;
2022-01-24 12:20:46 +00:00
use Webkul\Product\Repositories\ProductFlatRepository;
class Vendors extends Controller
{
2022-04-06 07:33:17 +00:00
public function __construct(protected VendorRepository $vendorRepository,
protected CategoryRepository $categoryRepository)
{
}
2022-04-13 13:45:17 +00:00
public function sources(){
$vendors = $this->vendorRepository->select('marketplace_sellers.id','url','logo','banner','shop_title','brand_attribute_id')
->where('is_approved',true)
->with(['categories:seller_id,type,categories'])
// ->leftJoin('seller_categories','marketplace_sellers.id','=','seller_categories.seller_id')
->get();
2022-04-13 16:00:31 +00:00
//return $vendors;
2022-04-13 13:45:17 +00:00
2022-04-13 16:00:31 +00:00
if(! $vendors){
return response()->json(['error' => 'not found'],404);
}
// return $vendors->first()->categories()->first();
// return json_decode($cats->categories,true);
$categorizedVendors = $vendors->map(function ($item, $key){
if($item->categories && $mainCats = $item->categories()->first()){
2022-04-13 15:28:39 +00:00
$cat_ids = json_decode($mainCats->categories,true);
// $vendor->test = Category::collection($this->categoryRepository->getVisibleCategoryTree($cat_ids[0]));
2022-04-13 16:00:31 +00:00
$item->main_categories = $this->categoryRepository->whereIn('id',$cat_ids)
2022-04-13 15:28:39 +00:00
->select('id','image','position','parent_id','display_mode','category_icon_path')
->where('status',1)
->with(['children'=> function($q){
$q->orderBy('position','asc');
}])
->orderBy('position','asc')
->get();
}
2022-04-13 16:00:31 +00:00
return $item;
});
return Source::collection($categorizedVendors);
2022-04-13 13:45:17 +00:00
}
public function index()
{
2021-12-29 07:11:37 +00:00
$vendors = $this->vendorRepository->select('marketplace_sellers.id','url','logo','banner','shop_title','brand_attribute_id')
->where('is_approved',true)
2021-12-29 03:05:55 +00:00
->with(['categories:seller_id,type,categories'])
// ->leftJoin('seller_categories','marketplace_sellers.id','=','seller_categories.seller_id')
->get();
2022-01-24 12:31:10 +00:00
foreach ($vendors as $vendor){
2022-04-13 16:00:31 +00:00
if($vendor->categories && $mainCats = $vendor->categories()->first()){
2022-01-24 12:31:10 +00:00
$cat_ids = json_decode($mainCats->categories,true);
// $vendor->test = Category::collection($this->categoryRepository->getVisibleCategoryTree($cat_ids[0]));
$vendor->main_categories = $this->categoryRepository->whereIn('id',$cat_ids)
->select('id','image','position','parent_id','display_mode','category_icon_path')
->where('status',1)
->with(['children'=> function($q){
$q->orderBy('position','asc');
}])
->orderBy('position','asc')
->get();
2022-01-25 06:54:15 +00:00
// if($vendor->main_categories->count()){
// foreach($vendor->main_categories as $category){
// $category->filters = app(ProductFlatRepository::class)->getProductsRelatedFilterableAttributes($category);
// }
// }
2022-01-24 12:31:10 +00:00
}
}
2022-01-25 06:54:15 +00:00
// return $vendors;
return Vendor::collection($vendors);
}
2022-01-19 14:04:47 +00:00
2022-01-28 10:01:59 +00:00
public function products(ProductRepository $productRepository,$seller_id){
$products = $productRepository->findAllBySeller($seller_id,request()->input('category_id'));
return ProductResource::collection($products);
}
public function brands(BrandRepository $brandRepository, $seller_id){
return Brand::collection($brandRepository->findAllBySeller($seller_id));
2022-01-19 14:04:47 +00:00
}
}