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

112 lines
4.5 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;
2022-05-04 10:08:36 +00:00
use Webkul\RestApi\Http\Controllers\V1\V1Controller;
2022-05-04 10:08:36 +00:00
class Vendors extends V1Controller
{
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(){
2022-09-27 10:11:11 +00:00
$vendors = $this->vendorRepository->select('marketplace_sellers.id','url','shop_title')
->where('is_approved',true)
2022-12-05 07:46:01 +00:00
// ->with(['categories:seller_id,type,categories'])
2022-09-27 10:11:11 +00:00
// ->leftJoin('seller_categories','marketplace_sellers.id','=','seller_categories.seller_id')
->get();
//return $vendors;
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()){
$cat_ids = json_decode($mainCats->categories,true);
// $vendor->test = Category::collection($this->categoryRepository->getVisibleCategoryTree($cat_ids[0]));
$item->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();
}
return $item;
});
return Source::collection($categorizedVendors);
}
public function menus(){
2022-09-25 16:06:53 +00:00
$vendors = $this->vendorRepository->select('marketplace_sellers.id','url','shop_title')
->where('is_approved',true)
->with(['menus' => function($query){
$query->where('status',1)
->with(['categories','brands'])
2022-09-27 10:08:36 +00:00
->orderBy('position','asc');
2022-09-27 11:50:57 +00:00
}])->get();
2022-09-27 10:08:36 +00:00
return Vendor::collection($vendors);
2022-09-25 16:06:53 +00:00
2022-09-25 15:56:43 +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
}
}