162 lines
4.8 KiB
PHP
162 lines
4.8 KiB
PHP
<?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\Models\ProductCategories;
|
|
use App\Repositories\Addon\AddonRepositoryInterface;
|
|
use App\Repositories\Domain\DomainRepositoryInterface;
|
|
use App\Repositories\Group\GroupRepositoryInterface;
|
|
use App\Repositories\Hosting\HostingRepositoryInterface;
|
|
use App\Repositories\VPS\VPSRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ShopController extends Controller
|
|
{
|
|
public function __construct(
|
|
public GroupRepositoryInterface $groupRepository,
|
|
public VPSRepositoryInterface $vpsRepository,
|
|
public HostingRepositoryInterface $hostingRepository,
|
|
public DomainRepositoryInterface $domainRepository,
|
|
public AddonRepositoryInterface $addonRepository,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Display the shop index page
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$title = "Dịch vụ của chúng tôi";
|
|
|
|
// Get types with groups
|
|
$type = 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
|
|
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 category slug (ProductCategories)
|
|
*/
|
|
public function show(Request $request, string $slug)
|
|
{
|
|
// Find category by slug
|
|
$category = ProductCategories::with(['parent', 'children'])
|
|
->where('slug', $slug)
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
if (!$category) {
|
|
abort(404, 'Danh mục sản phẩm không tồn tại');
|
|
}
|
|
|
|
// Get products with pagination and pricing
|
|
$products = $category->products()
|
|
->with([
|
|
'pricings' => function ($query) {
|
|
$query->where('is_active', 1)->orderBy('price');
|
|
},
|
|
'osVersions.osGroup',
|
|
'controlPanels'
|
|
])
|
|
->where('is_active', 1)
|
|
->orderBy('created_at', 'desc')
|
|
->paginate(12);
|
|
|
|
return view('frontend.service.index', [
|
|
'category' => $category,
|
|
'products' => $products,
|
|
'breadcrumbs' => $this->crums->add(__('Dashboard'))->add('Sản phẩm thuộc ' . $category->name),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get products by type and group
|
|
*/
|
|
private function getProductsByTypeAndGroup(int $typeId, int $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
|
|
*/
|
|
private function getGroupsByType(int $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
|
|
};
|
|
}
|
|
}
|