Files
portal-api/app/Http/Controllers/frontend/ShopController.php
T
pbao9 057353a2e1 feat: Enhance product support in Cart and OrderItem models
- Added new fields 'billing_cycle' and 'product_pricing_id' to Cart and OrderItem models.
- Updated product retrieval logic to support both new and legacy systems.
- Introduced relationships for Product and ProductPricings in Cart and OrderItem models.
- Created migration to add new fields to the carts and order_item tables.
- Updated frontend service index to handle new pricing structure and billing cycles.
- Improved UI components for better user experience in product selection and pricing display.
- Refactored action buttons in various views for consistency and improved styling.
2026-02-09 08:04:56 +07:00

164 lines
4.9 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\Type\TypeRepositoryInterface;
use App\Repositories\VPS\VPSRepositoryInterface;
use Illuminate\Http\Request;
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
*/
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
};
}
}