brand product vendors api
This commit is contained in:
parent
d4281d67cf
commit
36ee06c789
|
|
@ -2,10 +2,13 @@
|
|||
|
||||
namespace Sarga\API\Http\Controllers;
|
||||
|
||||
use Sarga\API\Http\Resources\Catalog\Brand;
|
||||
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
|
||||
use Sarga\API\Http\Resources\Core\Vendor;
|
||||
use Sarga\API\Repositories\ProductRepository;
|
||||
use Sarga\Brand\Repositories\BrandRepository;
|
||||
use Sarga\Shop\Repositories\CategoryRepository;
|
||||
use Sarga\Shop\Repositories\VendorRepository;
|
||||
use Webkul\API\Http\Controllers\Shop\Controller;
|
||||
use Webkul\Marketplace\Repositories\SellerRepository;
|
||||
use Webkul\Product\Repositories\ProductFlatRepository;
|
||||
|
|
@ -15,15 +18,12 @@ class Vendors extends Controller
|
|||
|
||||
protected $vendorRepository;
|
||||
protected $categoryRepository;
|
||||
protected $productRepository;
|
||||
|
||||
public function __construct(SellerRepository $sellerRepository,
|
||||
ProductRepository $productRepository,
|
||||
public function __construct(VendorRepository $sellerRepository,
|
||||
CategoryRepository $categoryRepository)
|
||||
{
|
||||
$this->vendorRepository = $sellerRepository;
|
||||
$this->categoryRepository = $categoryRepository;
|
||||
$this->productRepository = $productRepository;
|
||||
}
|
||||
|
||||
public function index()
|
||||
|
|
@ -58,7 +58,14 @@ class Vendors extends Controller
|
|||
return Vendor::collection($vendors);
|
||||
}
|
||||
|
||||
public function vendor_products($vendor_id){
|
||||
return ProductResource::collection($this->productRepository->findAllBySeller($vendor_id,request()->input('category_id')));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\API\Http\Resources\Catalog;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class Brand extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'code' => $this->code,
|
||||
'name' => $this->name,
|
||||
'image_url' => $this->image_url,
|
||||
'postion' => $this->position,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,8 @@ Route::group(['prefix' => 'api'], function ($router) {
|
|||
|
||||
//Vendors
|
||||
Route::get('vendors',[Vendors::class,'index'])->name('api.vendors');
|
||||
Route::get('vendor/products/{vendor_id}',[Vendors::class,'vendor_products'])->name('api.vendor.products');
|
||||
Route::get('vendor/products/{vendor_id}',[Vendors::class,'products'])->name('api.vendor.products');
|
||||
Route::get('vendor/brands/{vendor_id}',[Vendors::class,'brands'])->name('api.vendor.brands');
|
||||
|
||||
|
||||
//category routes
|
||||
|
|
|
|||
|
|
@ -77,4 +77,22 @@ class BrandRepository extends Repository
|
|||
public function actives(){
|
||||
return $this->findByField('status',1);
|
||||
}
|
||||
|
||||
public function findAllBySeller($seller_id){
|
||||
$query = $this->leftJoin('seller_brands as sb','sb.brand_id','=','brands.id')
|
||||
->where('sb.seller_id',$seller_id);
|
||||
|
||||
if(request()->has('category_id')){
|
||||
$query->leftJoin('category_brands as cb','cb.brand_id','=','brands.id')
|
||||
->where('cb.category_id',request()->get('category_id'));
|
||||
}
|
||||
|
||||
$limit = request()->get('limit') ?? 10;
|
||||
$page = request()->get('page') ?? 1;
|
||||
|
||||
return $query->orderBy('position', 'ASC')
|
||||
->skip(($page-1) * $limit)
|
||||
->take($limit)
|
||||
->get();
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Sarga\Shop\Models;
|
||||
|
||||
use Sarga\Brand\Models\BrandProxy;
|
||||
use Webkul\Category\Models\CategoryProxy;
|
||||
use Webkul\Marketplace\Models\Seller;
|
||||
use Webkul\Marketplace\Models\SellerCategory;
|
||||
|
|
@ -9,7 +10,12 @@ use Webkul\Marketplace\Models\SellerCategoryProxy;
|
|||
|
||||
class Vendor extends Seller
|
||||
{
|
||||
public function categories(){
|
||||
public function categories()
|
||||
{
|
||||
return $this->hasOne(SellerCategoryProxy::modelClass(),'seller_id',);
|
||||
}
|
||||
|
||||
public function brands(){
|
||||
return $this->belongsToMany(BrandProxy::modelClass(),'seller_brands','seller_id');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Sarga\Shop\Repositories;
|
||||
|
||||
use Sarga\Shop\Models\Vendor;
|
||||
use Webkul\Marketplace\Repositories\SellerRepository;
|
||||
|
||||
class VendorRepository extends SellerRepository
|
||||
{
|
||||
/**
|
||||
* Specify Model class name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
function model()
|
||||
{
|
||||
return Vendor::class;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue