get('per_page', 15), 500); $filter = $request->get('filter'); $type = $request->get('type'); $status = $request->get('status'); $query = InfraManagement::query()->select([ 'id', 'name', 'host', 'username', 'token', 'type', 'status', 'description', 'meta_data', 'created_at', 'updated_at', ]); if ($filter) { $query->where(function ($q) use ($filter) { $q->where('name', 'like', "%{$filter}%") ->orWhere('host', 'like', "%{$filter}%"); }); } if ($type !== null && $type !== '') { if (is_array($type)) { $query->whereIn('type', $type); } else { $query->where('type', $type); } } if ($status !== null && $status !== '') { $query->where('status', filter_var($status, FILTER_VALIDATE_BOOLEAN)); } $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 { $infra = InfraManagement::select([ 'id', 'name', 'host', 'username', 'token', 'type', 'status', 'description', 'meta_data', 'created_at', 'updated_at', ])->find($id); if (!$infra) { return response()->json(['error' => true, 'message' => "Không tìm thấy cấu hình #$id"], 404); } return response()->json(['error' => false, 'data' => $infra]); } public function store(Request $request): JsonResponse { $request->validate([ 'name' => 'required|string|max:255', 'host' => 'required|string|max:255', 'username' => 'required|string|max:255', 'password' => 'required|string', 'token' => 'nullable|string', 'type' => 'required|string|in:vcenter,esxi,jumphost', 'status' => 'nullable|boolean', 'description' => 'nullable|string', 'meta_data' => 'nullable|array', ]); DB::beginTransaction(); try { $data = $request->except(['password']); $data['password'] = SignRSA::EncryptFunc($request->password); $data['status'] = $request->boolean('status', true); $infra = InfraManagement::create($data); // Return without password $infra->makeHidden(['password']); DB::commit(); return response()->json(['error' => false, 'data' => $infra], 201); } catch (\Exception $e) { DB::rollBack(); return response()->json(['error' => true, 'message' => $e->getMessage()], 500); } } public function update(Request $request, int $id): JsonResponse { $infra = InfraManagement::find($id); if (!$infra) { return response()->json(['error' => true, 'message' => "Không tìm thấy cấu hình #$id"], 404); } $request->validate([ 'name' => 'required|string|max:255', 'host' => 'required|string|max:255', 'username' => 'required|string|max:255', 'password' => 'nullable|string', 'token' => 'nullable|string', 'type' => 'required|string|in:vcenter,esxi,jumphost', 'status' => 'nullable|boolean', 'description' => 'nullable|string', 'meta_data' => 'nullable|array', ]); DB::beginTransaction(); try { $data = $request->except(['password']); $data['status'] = $request->boolean('status', $infra->status); if (!empty($request->password)) { $data['password'] = SignRSA::EncryptFunc($request->password); } $infra->update($data); $infra->refresh(); $infra->makeHidden(['password']); DB::commit(); return response()->json(['error' => false, 'data' => $infra]); } catch (\Exception $e) { DB::rollBack(); return response()->json(['error' => true, 'message' => $e->getMessage()], 500); } } public function destroy(int $id): JsonResponse { $infra = InfraManagement::find($id); if (!$infra) { return response()->json(['error' => true, 'message' => "Không tìm thấy cấu hình #$id"], 404); } $infra->delete(); return response()->json(['error' => false, 'message' => 'Đã xoá cấu hình thành công']); } }