This commit is contained in:
Kerim 2023-04-08 01:26:36 +05:00
parent db70320812
commit 69e2af5f18
4 changed files with 146 additions and 36 deletions

View File

@ -4,6 +4,7 @@ namespace Sarga\API\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Sarga\API\Http\Resources\Catalog\HomeResource;
use Sarga\API\Http\Resources\Catalog\ProductVariant;
use Sarga\API\Http\Resources\Catalog\Suggestion;
use Sarga\API\Http\Resources\Catalog\SuperAttribute;
@ -13,15 +14,19 @@ use Webkul\API\Http\Controllers\Shop\ProductController;
use Sarga\API\Http\Resources\Catalog\Product as ProductResource;
use Sarga\Shop\Repositories\AttributeOptionRepository;
use Webkul\Product\Repositories\ProductFlatRepository;
use Sarga\API\Http\Controllers\Banners as Slider;
use Webkul\Core\Repositories\SliderRepository;
use Carbon\Carbon;
class Products extends ProductController
{
protected $attributeOptionRepository;
public function __construct(ProductRepository $productRepository,
AttributeOptionRepository $attributeOptionRepository)
{
public function __construct(
ProductRepository $productRepository,
SliderRepository $sliderRepository,
AttributeOptionRepository $attributeOptionRepository
) {
$this->attributeOptionRepository = $attributeOptionRepository;
parent::__construct($productRepository);
@ -37,6 +42,66 @@ class Products extends ProductController
return ProductResource::collection($this->productRepository->getAll(request()->input('category')));
}
public function home(SliderRepository $sliderRepository,)
{
// $data = [
// "sliders" => $sliderRepository->getActiveSliders(),
// "product_sections" => 'asd'
// ];
$sliders = $sliderRepository->select('title', 'path', 'content', 'expired_at', 'sort_order', 'slider_path')
->where(function ($query) {
$query->where('expired_at', '>=', Carbon::now()->format('Y-m-d'))
->orWhereNull('expired_at');
})
->orderBy('sort_order', 'ASC')
->limit(3)
->get();
$banner = $sliderRepository->select('title', 'path', 'content', 'expired_at', 'sort_order', 'slider_path')
->where(function ($query) {
$query->where('expired_at', '>=', Carbon::now()->format('Y-m-d'))
->orWhereNull('expired_at');
})
->orderBy('sort_order', 'DESC')
->limit(1)
->get();
$newProducts = ProductResource::collection($this->productRepository->getNewProducts());
$featuredProducts = ProductResource::collection($this->productRepository->getFeaturedProducts());
// $slider_map = $sliders->map(function ($item, $key){
// $item->review_average = $this->reviewRepository->getAverageRating($item);
// return $item;
// });
$data = [
'data' => [
'sliders' => $sliders,
'product_sections' => [
[
'title' => 'New Products',
'type' => 'product',
'items' => $newProducts
],
[
'title' => 'Adv Banner',
'type' => 'banner',
'items' => $banner
],
[
'title' => 'Featured Products',
'type' => 'product',
'items' => $featuredProducts
],
],
]
];
return $data;
// return HomeResource::collection($sliders);
// dump(HomeResource::collection($sliders));
// dump($sliders);
}
/**
* Returns a individual resource.
@ -46,17 +111,15 @@ class Products extends ProductController
*/
public function get($id)
{
return ($product = $this->productRepository->find($id))?
return ($product = $this->productRepository->find($id)) ?
new ProductResource($product) :
response()->json(['error' => 'not found'],404);
response()->json(['error' => 'not found'], 404);
}
public function variants($id)
{
$product = $this->productRepository->with(['super_attributes:id,code','variants'=>function($query)
{
$query->with(['images','product_flats' => function($qf)
{
$product = $this->productRepository->with(['super_attributes:id,code', 'variants' => function ($query) {
$query->with(['images', 'product_flats' => function ($qf) {
$channel = core()->getRequestedChannelCode();
$locale = core()->getRequestedLocaleCode();
@ -64,15 +127,16 @@ class Products extends ProductController
$qf->where('product_flat.channel', $channel)
->where('product_flat.locale', $locale)
->whereNotNull('product_flat.url_key')
->where('status',1);
->where('status', 1);
}]);
}])->find($id);
if(!empty($product) && $product->super_attributes->isNotEmpty() && $product->variants->isNotEmpty())
{
$variants = $product->variants->makeHidden(['type','created_at','updated_at','parent_id','attribute_family_id',
'additional','new','featured','visible_individually','status','guest_checkout','meta_title','meta_keywords',
'product_flats','attribute_family','short_description','sku','brand']);
if (!empty($product) && $product->super_attributes->isNotEmpty() && $product->variants->isNotEmpty()) {
$variants = $product->variants->makeHidden([
'type', 'created_at', 'updated_at', 'parent_id', 'attribute_family_id',
'additional', 'new', 'featured', 'visible_individually', 'status', 'guest_checkout', 'meta_title', 'meta_keywords',
'product_flats', 'attribute_family', 'short_description', 'sku', 'brand'
]);
$attribute = $product->super_attributes->first();
@ -80,13 +144,12 @@ class Products extends ProductController
$gr_data = array(
'attribute' => SuperAttribute::make($attribute),
'options' =>[],
'options' => [],
'level' => $product->super_attributes->count()
);
foreach($distinctVariants as $variant)
{
$option = $attribute->options->firstWhere('id',$variant->{$attribute->code});
foreach ($distinctVariants as $variant) {
$option = $attribute->options->firstWhere('id', $variant->{$attribute->code});
$item = [
'option' => $option->admin_name,
@ -94,40 +157,39 @@ class Products extends ProductController
];
$attributes = $product->super_attributes;
if($attributes->count()>1 && $option)
{
$products = $variants->where($attribute->code,$variant->{$attribute->code})
->map(function ($item,$key) use ($attributes){
return ProductVariant::make($item,$attributes);
});
if ($attributes->count() > 1 && $option) {
$products = $variants->where($attribute->code, $variant->{$attribute->code})
->map(function ($item, $key) use ($attributes) {
return ProductVariant::make($item, $attributes);
});
$item['variants']['attribute'] = SuperAttribute::make($product->super_attributes->last());
$item['variants']['products'] = $products->values();
}
else
{
$item['product'] = ProductVariant::make($variant,$attributes);
} else {
$item['product'] = ProductVariant::make($variant, $attributes);
}
$gr_data['options'][] = $item;
}
return response()->json($gr_data);
}
return response()->json(['message' => 'not found'],404);
return response()->json(['message' => 'not found'], 404);
}
public function searchProducts(){
// return $this->productRepository->searchProductByAttribute(request('key'));
public function searchProducts()
{
// return $this->productRepository->searchProductByAttribute(request('key'));
return ProductResource::collection($this->productRepository->searchProductByAttribute(request('key')));
}
public function discountedProducts(){
public function discountedProducts()
{
return ProductResource::collection($this->productRepository->getDiscountedProducts(request()->input('category')));
}
public function popularProducts(){
public function popularProducts()
{
return ProductResource::collection($this->productRepository->getPopularProducts(request()->input('category')));
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
use Illuminate\Http\Resources\Json\JsonResource;
use Sarga\API\Http\Resources\Catalog\Product;
class HomeProductSection extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'title' => $this->title,
'products' => Product::collection($this->products),
];
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Sarga\API\Http\Resources\Catalog;
use Illuminate\Http\Resources\Json\JsonResource;
use Sarga\API\Http\Resources\Core\Slider;
class HomeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'sliders' => Slider::collection($this->sliders),
//'product_sections' => HomeProductSection::collection($this->product_section),
];
}
}

View File

@ -56,6 +56,8 @@ Route::group(['prefix' => 'api'], function () {
'resource' => AttributeOption::class,
]);
Route::get('home', [Products::class, 'home']);
//Product routes
Route::get('products', [Products::class, 'index']);
Route::get('products-discounted', [Products::class, 'discountedProducts']);