Update source into github
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\frontend;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Services\Cart;
|
||||
use App\Http\Requests\Shop\AddToCartRequest;
|
||||
use App\Models\Hosting;
|
||||
use App\Models\Group;
|
||||
use App\Models\Type;
|
||||
use App\Models\VPS;
|
||||
use App\Repositories\Addon\AddonRepositoryInterface;
|
||||
use App\Repositories\Domain\DomainRepositoryInterface;
|
||||
use App\Repositories\Group\GroupRepositoryInterface;
|
||||
use App\Repositories\Hosting\HostingRepositoryInterface;
|
||||
use App\Repositories\Type\TypeRepositoryInterface;
|
||||
use App\Repositories\VPS\VPSRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ShopController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
public TypeRepositoryInterface $typeRepository,
|
||||
public GroupRepositoryInterface $groupRepository,
|
||||
public VPSRepositoryInterface $vpsRepository,
|
||||
public HostingRepositoryInterface $hostingRepository,
|
||||
public DomainRepositoryInterface $domainRepository,
|
||||
public AddonRepositoryInterface $addonRepository,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the shop index page with optimized queries
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$title = "Dịch vụ của chúng tôi";
|
||||
|
||||
// Cache types with groups for better performance
|
||||
$type = Cache::remember('shop_types_with_groups', 3600, function () {
|
||||
return Type::with(['group' => function ($query) {
|
||||
$query->where('status', 1); // Only active groups
|
||||
}])
|
||||
->whereNotIn('id', [1, 2])
|
||||
->where('status', 1) // Only active types
|
||||
->latest('id')
|
||||
->get();
|
||||
});
|
||||
|
||||
// Handle product filtering with optimized queries
|
||||
if ($request->type && $request->group) {
|
||||
$product = $this->getProductsByTypeAndGroup($request->type, $request->group);
|
||||
$group = $this->getGroupsByType($request->type);
|
||||
|
||||
return view('frontend.shop', compact('title', 'type', 'group', 'product'));
|
||||
}
|
||||
|
||||
if ($request->type) {
|
||||
$group = $this->getGroupsByType($request->type);
|
||||
return view('frontend.shop', compact('title', 'type', 'group'));
|
||||
}
|
||||
|
||||
return view('frontend.shop', compact('title', 'type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product to cart with validation
|
||||
*/
|
||||
public function store(AddToCartRequest $request, Cart $cart)
|
||||
{
|
||||
$product = $this->findProduct($request->type_id, $request->product_id);
|
||||
|
||||
if (!$product) {
|
||||
return back()->with('error', 'Sản phẩm không tồn tại!');
|
||||
}
|
||||
|
||||
$cart->addToCart($request->user('users')->id, $request->product_price, $product);
|
||||
|
||||
return redirect()->back()->with('success', 'Thêm vào giỏ hàng thành công!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display products by group slug with pagination and optimization
|
||||
*/
|
||||
public function show(Request $request, string $slug)
|
||||
{
|
||||
// Cache group data for better performance
|
||||
$page = (int) $request->get('page', 1);
|
||||
$cacheKey = "group_products_{$slug}_page_{$page}";
|
||||
|
||||
$data = Cache::remember($cacheKey, 1800, function () use ($slug, $page) {
|
||||
// Find group with optimized query
|
||||
$group = Group::with(['type'])
|
||||
->where('slug', $slug)
|
||||
->where('status', 1) // Only active groups
|
||||
->first();
|
||||
|
||||
if (!$group) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get products with pagination based on group type
|
||||
$products = $this->getPaginatedProducts($group, $page);
|
||||
|
||||
return [
|
||||
'group' => $group,
|
||||
'products' => $products,
|
||||
];
|
||||
});
|
||||
|
||||
if (!$data) {
|
||||
abort(404, 'Nhóm sản phẩm không tồn tại');
|
||||
}
|
||||
|
||||
return view('frontend.service.index', [
|
||||
'group' => $data['group'],
|
||||
'products' => $data['products'],
|
||||
'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $data['group']->name),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get products by type and group with optimization
|
||||
*/
|
||||
private function getProductsByTypeAndGroup(int $typeId, int $groupId)
|
||||
{
|
||||
return Cache::remember("products_type_{$typeId}_group_{$groupId}", 1800, function () use ($typeId, $groupId) {
|
||||
return match ($typeId) {
|
||||
3 => Hosting::where('group_id', $groupId)
|
||||
->where('status', 1)
|
||||
->with(['group', 'type'])
|
||||
->get(),
|
||||
4 => VPS::where('group_id', $groupId)
|
||||
->where('status', 1)
|
||||
->with(['group', 'type', 'operatingSystems.osGroup'])
|
||||
->get(),
|
||||
default => collect()
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get groups by type with optimization
|
||||
*/
|
||||
private function getGroupsByType(int $typeId)
|
||||
{
|
||||
return Cache::remember("groups_type_{$typeId}", 3600, function () use ($typeId) {
|
||||
return Group::with('type')
|
||||
->where('type_id', $typeId)
|
||||
->where('status', 1)
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Find product by type and ID
|
||||
*/
|
||||
private function findProduct(int $typeId, int $productId)
|
||||
{
|
||||
return match ($typeId) {
|
||||
3 => Hosting::where('id', $productId)
|
||||
->where('status', 1)
|
||||
->first(),
|
||||
4 => VPS::where('id', $productId)
|
||||
->where('status', 1)
|
||||
->first(),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paginated products based on group type
|
||||
*/
|
||||
private function getPaginatedProducts(Group $group, int $page = 1, int $perPage = 12)
|
||||
{
|
||||
$query = null;
|
||||
|
||||
// Determine which model to query based on group type
|
||||
switch ($group->type_id) {
|
||||
case 3: // Hosting
|
||||
$query = Hosting::where('group_id', $group->id)
|
||||
->where('status', 1)
|
||||
->with(['group', 'type'])
|
||||
->orderBy('created_at', 'desc');
|
||||
break;
|
||||
|
||||
case 4: // VPS
|
||||
$query = VPS::where('group_id', $group->id)
|
||||
->where('status', 1)
|
||||
->with(['group', 'type'])
|
||||
->orderBy('created_at', 'desc');
|
||||
break;
|
||||
|
||||
default:
|
||||
return collect()->paginate($perPage);
|
||||
}
|
||||
|
||||
return $query->paginate($perPage, ['*'], 'page', $page);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user