¨4.0.1¨

This commit is contained in:
¨NW¨
2023-12-03 14:07:47 +00:00
parent c08b36d1b6
commit f35052522d
1112 changed files with 43019 additions and 24987 deletions

View File

@@ -2,9 +2,15 @@
namespace Modules\Product\Http\Controllers\Admin;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\View\View;
use Modules\Product\Entities\Product;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Modules\Admin\Traits\HasCrudActions;
use Modules\Product\Http\Requests\SaveProductRequest;
use Modules\Product\Transformers\ProductEditResource;
class ProductController
{
@@ -15,26 +21,124 @@ class ProductController
*
* @var string
*/
protected $model = Product::class;
protected string $model = Product::class;
/**
* Label of the resource.
*
* @var string
*/
protected $label = 'product::products.product';
protected string $label = 'product::products.product';
/**
* View path of the resource.
*
* @var string
*/
protected $viewPath = 'product::admin.products';
protected string $viewPath = 'product::admin.products';
/**
* Form requests for the resource.
*
* @var array|string
*/
protected $validation = SaveProductRequest::class;
protected string|array $validation = SaveProductRequest::class;
/**
* Store a newly created resource in storage.
*
* @return Response|JsonResponse
*/
public function store()
{
$this->disableSearchSyncing();
$entity = $this->getModel()->create(
$this->getRequest('store')->all()
);
$this->searchable($entity);
$message = trans('admin::messages.resource_created', ['resource' => $this->getLabel()]);
if (request()->query('exit_flash')) {
session()->flash('exit_flash', $message);
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => $message,
'product_id' => $entity->id,
], 200
);
}
return redirect()->route("{$this->getRoutePrefix()}.index")
->withSuccess($message);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return Factory|View|Application
*/
public function edit($id): Factory|View|Application
{
$entity = $this->getEntity($id);
$productEditResource = new ProductEditResource($entity);
return view("{$this->viewPath}.edit",
[
'product' => $entity,
'product_resource' => $productEditResource->response()->content(),
]
);
}
/**
* Update the specified resource in storage.
*
* @param int $id
*/
public function update($id)
{
$entity = $this->getEntity($id);
$this->disableSearchSyncing();
$entity->update(
$this->getRequest('update')->all()
);
$entity->withoutEvents(function () use ($entity) {
$entity->touch();
});
$productEditResource = new ProductEditResource($entity);
$this->searchable($entity);
$message = trans('admin::messages.resource_updated', ['resource' => $this->getLabel()]);
if (request()->query('exit_flash')) {
session()->flash('exit_flash', $message);
}
if (request()->wantsJson()) {
return response()->json(
[
'success' => true,
'message' => $message,
'product_resource' => $productEditResource,
], 200
);
}
}
}

View File

@@ -2,11 +2,17 @@
namespace Modules\Product\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Modules\Review\Entities\Review;
use Illuminate\Contracts\View\View;
use Modules\Product\Entities\Product;
use Illuminate\Contracts\View\Factory;
use Modules\Product\Events\ProductViewed;
use Modules\Product\Filters\ProductFilter;
use Illuminate\Contracts\Foundation\Application;
use Modules\Product\Repositories\ProductRepository;
use Modules\Product\Http\Middleware\SetProductSortOption;
class ProductController extends Controller
@@ -23,12 +29,14 @@ class ProductController extends Controller
$this->middleware(SetProductSortOption::class)->only('index');
}
/**
* Display a listing of the resource.
*
* @param \Modules\Product\Entities\Product $model
* @param \Modules\Product\Filters\ProductFilter $productFilter
* @return \Illuminate\Http\Response
* @param Product $model
* @param ProductFilter $productFilter
*
* @return JsonResponse|Application|Factory|View
*/
public function index(Product $model, ProductFilter $productFilter)
{
@@ -39,28 +47,37 @@ class ProductController extends Controller
return view('public.products.index');
}
/**
* Show the specified resource.
*
* @param string $slug
* @return \Illuminate\Http\Response
*
* @return Response
*/
public function show($slug)
{
$product = Product::findBySlug($slug);
$product = ProductRepository::findBySlug($slug);
$relatedProducts = $product->relatedProducts()->forCard()->get();
$upSellProducts = $product->upSellProducts()->forCard()->get();
$review = $this->getReviewData($product);
$product->append([
'is_in_flash_sale',
'flash_sale_end_date',
'formatted_price_range',
]);
event(new ProductViewed($product));
return view('public.products.show', compact('product', 'relatedProducts', 'upSellProducts', 'review'));
}
private function getReviewData(Product $product)
{
if (! setting('reviews_enabled')) {
return;
if (!setting('reviews_enabled')) {
return null;
}
return Review::countAndAvgRating($product);

View File

@@ -13,7 +13,8 @@ class ProductPriceController
* Show the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*
* @return string
*/
public function show($id)
{
@@ -23,12 +24,12 @@ class ProductPriceController
->findOrFail($id);
$variantPrice = $this->cartItem($product, request('options', []))
->total()
->totalPrice()
->convertToCurrentCurrency()
->format();
return product_price_formatted($product, function ($price) use ($product, $variantPrice) {
if (! $product->hasSpecialPrice()) {
if (!$product->hasSpecialPrice()) {
return $variantPrice;
}
@@ -36,6 +37,7 @@ class ProductPriceController
});
}
private function cartItem(Product $product, array $options)
{
$chosenOptions = new ChosenProductOptions($product, $options);

View File

@@ -2,6 +2,7 @@
namespace Modules\Product\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Modules\Product\Entities\Product;
use Modules\Category\Entities\Category;
@@ -14,9 +15,10 @@ trait ProductSearch
/**
* Search products for the request.
*
* @param \Modules\Product\Entities\Product $model
* @param \Modules\Product\Filters\ProductFilter $productFilter
* @return \Illuminate\Http\Response
* @param Product $model
* @param ProductFilter $productFilter
*
* @return JsonResponse
*/
public function searchProducts(Product $model, ProductFilter $productFilter)
{
@@ -43,9 +45,10 @@ trait ProductSearch
]);
}
private function getAttributes($productIds)
{
if (! request()->filled('category') || $this->filteringViaRootCategory()) {
if (!request()->filled('category') || $this->filteringViaRootCategory()) {
return collect();
}
@@ -57,6 +60,7 @@ trait ProductSearch
->get();
}
private function filteringViaRootCategory()
{
return Category::where('slug', request('category'))
@@ -64,6 +68,7 @@ trait ProductSearch
->isRoot();
}
private function getProductsCategoryIds($productIds)
{
return DB::table('product_categories')

View File

@@ -2,8 +2,10 @@
namespace Modules\Product\Http\Controllers;
use Closure;
use Modules\Product\Entities\Product;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Modules\Product\Http\Response\SuggestionsResponse;
class SuggestionController
@@ -11,9 +13,9 @@ class SuggestionController
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
* @return SuggestionsResponse
*/
public function index(Product $model)
public function index(Product $model): SuggestionsResponse
{
$products = $this->getProducts($model);
@@ -25,25 +27,13 @@ class SuggestionController
);
}
/**
* Get total results count.
*
* @param \Modules\Product\Entities\Product $model
* @return int
*/
private function getTotalResults(Product $model)
{
return $model->search(request('query'))
->query()
->when(request()->filled('category'), $this->categoryQuery())
->count();
}
/**
* Get products suggestions.
*
* @param \Modules\Product\Entities\Product $model
* @return \Illuminate\Database\Eloquent\Collection
* @param Product $model
*
* @return Collection
*/
private function getProducts(Product $model)
{
@@ -67,10 +57,11 @@ class SuggestionController
->get();
}
/**
* Returns categories condition closure.
*
* @return \Closure
* @return Closure
*/
private function categoryQuery()
{
@@ -80,4 +71,20 @@ class SuggestionController
});
};
}
/**
* Get totalPrice results count.
*
* @param Product $model
*
* @return int
*/
private function getTotalResults(Product $model): int
{
return $model->search(request('query'))
->query()
->when(request()->filled('category'), $this->categoryQuery())
->count();
}
}