Files

271 lines
11 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Service;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\Service\ServiceListResource;
use App\Http\Resources\Api\Service\ServiceResource;
use App\Http\Services\Invoice\InvoiceService;
use App\Jobs\VirtualMachine\ProcessOrderPaymentJob;
use App\Models\Addon;
use App\Models\Cart;
use App\Models\CartAddon;
use App\Models\Discount;
use App\Models\Order;
use App\Models\OrderAddon;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\ProductPricings;
use App\Models\Service;
use App\Repositories\Api\Service\ServiceRepository;
use App\Services\Api\Service\ServiceService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
/**
* @OA\Tag(
* name="Services",
* description="API Endpoints for Services/Products"
* )
*/
class ServiceController extends Controller
{
protected $serviceService;
protected $serviceRepository;
public function __construct(
ServiceService $serviceService,
ServiceRepository $serviceRepository
)
{
$this->serviceService = $serviceService;
$this->serviceRepository = $serviceRepository;
}
/**
* @OA\Get(
* path="/services/get-list",
* operationId="getServiceList",
* tags={"Services"},
* summary="Get list of services",
* description="Returns a paginated list of active services/products",
* @OA\Parameter(
* name="limit",
* in="query",
* description="Number of items per page",
* required=false,
* @OA\Schema(type="integer", default=10)
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(property="error", type="boolean", example=false),
* @OA\Property(property="message", type="string", example="Lấy dữ liệu thành công"),
* @OA\Property(property="page_type", type="string", example="service"),
* @OA\Property(property="data", type="array", @OA\Items(
* @OA\Property(property="uuid", type="string", example="550e8400-e29b-41d4-a716-446655440000"),
* @OA\Property(property="name", type="string", example="Cloud VPS Basic"),
* @OA\Property(property="slug", type="string", example="cloud-vps-basic"),
* @OA\Property(property="short_description", type="string", example="Basic VPS plan"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="thumbnail", type="string", example="https://example.com/images/vps.jpg"),
* @OA\Property(property="product_type", type="string", example="vps"),
* @OA\Property(property="specs", type="object",
* @OA\Property(property="cpu", type="integer", example=2),
* @OA\Property(property="ram", type="integer", example=4),
* @OA\Property(property="disk", type="integer", example=50),
* @OA\Property(property="bandwidth", type="integer", example=1000),
* @OA\Property(property="gpu", type="string", nullable=true)
* ),
* @OA\Property(property="stock", type="integer", example=100),
* @OA\Property(property="price", type="number", example=299000),
* @OA\Property(property="price_sale", type="number", example=199000),
* @OA\Property(property="pricing", type="object"),
* @OA\Property(property="pricing_sale", type="object"),
* @OA\Property(property="group", type="string", example="VPS"),
* @OA\Property(property="os", type="array", @OA\Items(
* @OA\Property(property="group", type="string", example="Ubuntu")
* ))
* ))
* )
* )
* )
*/
public function getService(Request $request)
{
$limit = $request->get('limit', 10);
$services = $this->serviceService->getActiveServices($limit);
return ServiceListResource::collection($services)->additional([
'error' => false,
'message' => 'Lấy dữ liệu thành công',
'page_type' => 'service',
]);
}
/**
* @OA\Get(
* path="/services/{slug}",
* operationId="getServiceDetail",
* tags={"Services"},
* summary="Get service details",
* description="Returns detailed information about a specific service/product",
* @OA\Parameter(
* name="slug",
* in="path",
* description="Service slug",
* required=true,
* @OA\Schema(type="string")
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(property="error", type="boolean", example=false),
* @OA\Property(property="message", type="string", example="Lấy dữ liệu thành công"),
* @OA\Property(property="page_type", type="string", example="service"),
* @OA\Property(property="data", type="object",
* @OA\Property(property="uuid", type="string", example="550e8400-e29b-41d4-a716-446655440000"),
* @OA\Property(property="name", type="string", example="Cloud VPS Basic"),
* @OA\Property(property="slug", type="string", example="cloud-vps-basic"),
* @OA\Property(property="short_description", type="string", example="Basic VPS plan"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="thumbnail", type="string", example="https://example.com/images/vps.jpg"),
* @OA\Property(property="product_type", type="string", example="vps"),
* @OA\Property(property="specs", type="object",
* @OA\Property(property="cpu", type="integer", example=2),
* @OA\Property(property="ram", type="integer", example=4),
* @OA\Property(property="disk", type="integer", example=50),
* @OA\Property(property="bandwidth", type="integer", example=1000),
* @OA\Property(property="gpu", type="string", nullable=true)
* ),
* @OA\Property(property="stock", type="integer", example=100),
* @OA\Property(property="price", type="number", example=299000),
* @OA\Property(property="price_sale", type="number", example=199000),
* @OA\Property(property="group", type="string", example="VPS"),
* @OA\Property(property="os", type="array", @OA\Items(
* @OA\Property(property="name", type="string", example="Ubuntu"),
* @OA\Property(property="thumbnail", type="string"),
* @OA\Property(property="versions", type="array", @OA\Items(
* @OA\Property(property="version", type="string", example="22.04"),
* @OA\Property(property="name", type="string", example="Ubuntu 22.04 LTS")
* ))
* ))
* )
* )
* ),
* @OA\Response(
* response=404,
* description="Service not found"
* )
* )
*/
public function getDetailService(Product $product)
{
$product->load([
'osVersions.osGroup',
'category',
'pricings' => fn($q) => $q->where('is_active', 1),
'addons'
]);
return response()->json([
'error' => false,
'message' => 'Lấy dữ liệu thành công',
'page_type' => $this->resolvePageType($product->product_type),
'data' => new ServiceResource($product),
]);
}
private function resolvePageType(?string $productType): string
{
return match ($productType) {
'vps', 'cloud' => 'service_vps',
'hosting', 'email', 'ssl' => 'service_hosting',
'domain' => 'service_domain',
'blog' => 'blog',
default => 'service_vps',
};
}
public function getDetailServiceCustomer(Request $request, $uuid)
{
$service = Service::where('uuid', $uuid)->first();
if (!$service) {
return response()->json([
'error' => true,
'message' => 'Dịch vụ không tồn tại'
], 404);
}
$service->load([
'product.category',
'orderItem.os.osGroup',
'orderItem.pricing',
'invoice',
]);
return response()->json([
'error' => false,
'message' => 'Lấy dữ liệu thành công',
'data' => new ServiceResource($service),
]);
}
/**
* Get addons for a specific product
* GET /v1/products/{productId}/addons
*/
public function getProductAddons(Request $request, string $productId)
{
$product = Product::where('uuid', $productId)->first();
if (!$product) {
return response()->json([
'error' => true,
'message' => 'Sản phẩm không tồn tại'
], 404);
}
$addons = $product->addons()
->where('status', true)
->get();
$groupedAddons = [
'cpu' => [],
'ram' => [],
'disk' => [],
'other' => []
];
foreach ($addons as $addon) {
// Group by code field (cpu, ram, disk, gpu, bandwidth, etc.)
$key = strtolower(trim($addon->code));
if (!in_array($key, ['cpu', 'ram', 'disk'])) {
$key = 'other';
}
$groupedAddons[$key][] = [
'id' => $addon->id,
'name' => $addon->name,
'type' => $addon->type,
'code' => $addon->code,
'price' => $addon->price,
'description' => $addon->description,
'min' => $addon->min,
'max' => $addon->max,
'step' => $addon->step,
'prefix' => $addon->prefix,
];
}
return response()->json([
'error' => false,
'message' => 'Lấy dữ liệu thành công',
'data' => $groupedAddons
]);
}
}