feat: Implement new admin API endpoints for product, customer, vCenter, and authentication, and add vCenter resource management UI.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\AdminUserResource;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'username' => 'required|string',
|
||||
'password' => 'required|string',
|
||||
]);
|
||||
|
||||
$token = Auth::guard('admins-api')->attempt($request->only('username', 'password'));
|
||||
|
||||
if (!$token) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Thông tin đăng nhập không chính xác.',
|
||||
], 401);
|
||||
}
|
||||
|
||||
$user = Auth::guard('admins-api')->user();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'token' => $token,
|
||||
'token_type' => 'bearer',
|
||||
'expires_in' => config('jwt.ttl') * 60,
|
||||
'user' => new AdminUserResource($user),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function me(): JsonResponse
|
||||
{
|
||||
$user = Auth::guard('admins-api')->user();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => ['user' => new AdminUserResource($user)],
|
||||
]);
|
||||
}
|
||||
|
||||
public function refresh(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$newToken = JWTAuth::parseToken()->refresh();
|
||||
$user = JWTAuth::setToken($newToken)->toUser();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'token' => $newToken,
|
||||
'token_type' => 'bearer',
|
||||
'expires_in' => config('jwt.ttl') * 60,
|
||||
'user' => new AdminUserResource($user),
|
||||
],
|
||||
]);
|
||||
} catch (JWTException) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
|
||||
], 401);
|
||||
}
|
||||
}
|
||||
|
||||
public function logout(): JsonResponse
|
||||
{
|
||||
Auth::guard('admins-api')->logout();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã đăng xuất thành công.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Customer;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\CustomerResource;
|
||||
use App\Repositories\Admin\Customer\AdminCustomerRepositoryInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminCustomerRepositoryInterface $customers,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 100);
|
||||
$paginator = $this->customers->paginate($request->only('filter', 'status'), $perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => CustomerResource::collection($paginator->items()),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Invoice;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\InvoiceResource;
|
||||
use App\Repositories\Admin\Invoice\AdminInvoiceRepositoryInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminInvoiceRepositoryInterface $invoices,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 100);
|
||||
$paginator = $this->invoices->paginate($request->only('filter', 'status'), $perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => InvoiceResource::collection($paginator->items()),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Order;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\OrderResource;
|
||||
use App\Repositories\Admin\Order\AdminOrderRepositoryInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class OrderController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminOrderRepositoryInterface $orders,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 100);
|
||||
$paginator = $this->orders->paginate($request->only('filter', 'status'), $perPage);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => OrderResource::collection($paginator->items()),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Product;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Admin\ProductResource;
|
||||
use App\Repositories\Admin\Product\AdminProductRepositoryInterface;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminProductRepositoryInterface $products,
|
||||
) {}
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 100);
|
||||
$paginator = $this->products->paginate(
|
||||
$request->only('filter', 'type', 'is_active'),
|
||||
$perPage
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => [
|
||||
'items' => ProductResource::collection($paginator->items()),
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(int $id): JsonResponse
|
||||
{
|
||||
$product = $this->products->findWithPricings($id);
|
||||
|
||||
if (!$product) {
|
||||
return response()->json(['error' => true, 'message' => 'Không tìm thấy sản phẩm.'], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'data' => new ProductResource($product),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\VCenter;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\ModuleFolder;
|
||||
use App\Models\ModuleHost;
|
||||
use App\Models\vCenter;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VCenterController extends Controller
|
||||
{
|
||||
public function tree(Request $request): JsonResponse
|
||||
{
|
||||
$vcenters = vCenter::with(['datacenters'])->get();
|
||||
$allHosts = ModuleHost::with(['datastore', 'network'])->get()->groupBy('module_datacenter_id');
|
||||
$allFolders = ModuleFolder::all()->groupBy('module_datacenter_id');
|
||||
|
||||
$tree = $vcenters->map(function ($vc) use ($allHosts, $allFolders) {
|
||||
$dcGroups = ($vc->datacenters ?? collect())->groupBy('datacenter_id');
|
||||
$datacenters = $dcGroups->map(function ($dcGroup) use ($allHosts, $allFolders) {
|
||||
$dc = $dcGroup->first();
|
||||
$dcIds = $dcGroup->pluck('id');
|
||||
$uniqueHosts = $dcIds->flatMap(fn($id) => $allHosts->get($id) ?? collect())
|
||||
->unique('host_id')->values();
|
||||
|
||||
$dcNetworks = $uniqueHosts
|
||||
->flatMap(fn($host) => $host->network ?? collect())
|
||||
->unique('network')
|
||||
->map(fn($net) => [
|
||||
'id' => 'network-' . $net->id,
|
||||
'name' => $net->name,
|
||||
'type' => 'network',
|
||||
'meta' => ['db_id' => $net->id, 'network_id' => $net->network, 'net_type' => $net->type],
|
||||
]);
|
||||
|
||||
$dcHosts = $uniqueHosts->map(function ($host) {
|
||||
$datastores = ($host->datastore ?? collect())
|
||||
->unique('datastore')
|
||||
->map(fn($ds) => [
|
||||
'id' => 'datastore-' . $ds->id,
|
||||
'name' => $ds->name,
|
||||
'type' => 'datastore',
|
||||
'meta' => [
|
||||
'db_id' => $ds->id,
|
||||
'datastore_id' => $ds->datastore,
|
||||
'store_type' => $ds->type,
|
||||
'space_gb' => $ds->space ? round($ds->space / 1024, 1) : null,
|
||||
'status' => $ds->status,
|
||||
],
|
||||
]);
|
||||
|
||||
return [
|
||||
'id' => 'host-' . $host->id,
|
||||
'name' => $host->name,
|
||||
'type' => 'host',
|
||||
'meta' => ['db_id' => $host->id, 'host_id' => $host->host_id, 'console' => $host->console],
|
||||
'children' => array_values($datastores->toArray()),
|
||||
];
|
||||
});
|
||||
|
||||
$dcFolders = $dcIds
|
||||
->flatMap(fn($id) => $allFolders->get($id) ?? collect())
|
||||
->unique('folder')
|
||||
->map(fn($f) => [
|
||||
'id' => 'folder-' . $f->id,
|
||||
'name' => $f->name,
|
||||
'type' => 'folder',
|
||||
'meta' => ['db_id' => $f->id, 'folder_id' => $f->folder, 'folder_type' => $f->type],
|
||||
]);
|
||||
|
||||
return [
|
||||
'id' => 'datacenter-' . $dc->id,
|
||||
'name' => $dc->name,
|
||||
'type' => 'datacenter',
|
||||
'meta' => ['db_id' => $dc->id, 'datacenter_id' => $dc->datacenter_id],
|
||||
'children' => array_values($dcFolders->concat($dcNetworks)->concat($dcHosts)->toArray()),
|
||||
];
|
||||
})->values();
|
||||
|
||||
return [
|
||||
'id' => 'vcenter-' . $vc->id,
|
||||
'name' => $vc->host,
|
||||
'type' => 'vcenter',
|
||||
'meta' => ['db_id' => $vc->id, 'host' => $vc->host, 'status' => $vc->status],
|
||||
'children' => array_values($datacenters->toArray()),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['error' => false, 'data' => $tree->values()]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user