51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Service;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Service;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ServiceController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) ($request->per_page ?? 15), 100);
|
|
|
|
$query = Service::with(['customer:id,uuid,fullname,email', 'product:id,uuid,name', 'invoice:id,invoice_number', 'vmware:id,name,module_ip_address'])
|
|
->when($request->filter, fn($q, $v) => $q->where('server_name', 'like', "%{$v}%"))
|
|
->when($request->status, fn($q, $v) => $q->where('status', $v))
|
|
->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
|
|
|
|
$paginator = $query->latest()->paginate($perPage);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'items' => $paginator->items(),
|
|
'total' => $paginator->total(),
|
|
'per_page' => $paginator->perPage(),
|
|
'current_page' => $paginator->currentPage(),
|
|
'last_page' => $paginator->lastPage(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$service = Service::with(['customer', 'product', 'orderItem', 'invoice', 'vmware', 'addons'])
|
|
->findOrFail($id);
|
|
|
|
return response()->json(['error' => false, 'data' => $service]);
|
|
}
|
|
|
|
public function detachCustomer(int $id): JsonResponse
|
|
{
|
|
$service = Service::findOrFail($id);
|
|
$service->update(['customer_id' => null]);
|
|
|
|
return response()->json(['error' => false, 'message' => 'Đã gỡ khách hàng khỏi dịch vụ.']);
|
|
}
|
|
}
|