update ippool
This commit is contained in:
@@ -3,15 +3,27 @@
|
||||
namespace App\Http\Controllers\Api\Admin\IpPool;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Imports\IpPoolImport;
|
||||
use App\Models\IpPool;
|
||||
use App\Models\ModuleIpAddress;
|
||||
use App\Models\ModuleNetwork;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Maatwebsite\Excel\Validators\ValidationException;
|
||||
|
||||
class IpPoolController extends Controller
|
||||
{
|
||||
private const EDITABLE_FIELDS = [
|
||||
'gateway', 'netmask', 'service_id', 'status',
|
||||
'owner_type', 'ip_role', 'parent_ip', 'customer_name', 'service_name',
|
||||
'allocated_at', 'revoked_at', 'allocation_status',
|
||||
'contact_email', 'contact_cc', 'contact_phone',
|
||||
'monthly_cost', 'selling_price', 'additional_info',
|
||||
'last_email_type', 'last_email_at', 'updated_by', 'note',
|
||||
];
|
||||
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min((int) $request->get('per_page', 15), 500);
|
||||
@@ -116,6 +128,9 @@ class IpPoolController extends Controller
|
||||
$request->validate([
|
||||
'ip_address' => 'required|string',
|
||||
'module_network_id' => 'required|integer|exists:module_network,id',
|
||||
'contact_email' => 'nullable|email',
|
||||
'monthly_cost' => 'nullable|numeric',
|
||||
'selling_price' => 'nullable|numeric',
|
||||
]);
|
||||
|
||||
$networkId = (int) $request->module_network_id;
|
||||
@@ -154,6 +169,8 @@ class IpPoolController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
$extra = $request->only(self::EDITABLE_FIELDS);
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$created = [];
|
||||
@@ -161,6 +178,7 @@ class IpPoolController extends Controller
|
||||
$created[] = IpPool::create([
|
||||
'ip_address' => $ip,
|
||||
'module_network_id' => $networkId,
|
||||
...$extra,
|
||||
]);
|
||||
}
|
||||
DB::commit();
|
||||
@@ -183,6 +201,9 @@ class IpPoolController extends Controller
|
||||
$request->validate([
|
||||
'ip_address' => 'required|string|max:45',
|
||||
'module_network_id' => 'required|integer|exists:module_network,id',
|
||||
'contact_email' => 'nullable|email',
|
||||
'monthly_cost' => 'nullable|numeric',
|
||||
'selling_price' => 'nullable|numeric',
|
||||
]);
|
||||
|
||||
if (!filter_var($request->ip_address, FILTER_VALIDATE_IP)) {
|
||||
@@ -204,7 +225,7 @@ class IpPoolController extends Controller
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
$pool->update($request->only(['ip_address', 'module_network_id']));
|
||||
$pool->update($request->only(['ip_address', 'module_network_id', ...self::EDITABLE_FIELDS]));
|
||||
DB::commit();
|
||||
|
||||
return response()->json(['error' => false, 'data' => $pool->fresh('network')]);
|
||||
@@ -276,6 +297,40 @@ class IpPoolController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function import(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|file|mimes:csv,txt|max:10240',
|
||||
]);
|
||||
|
||||
$countBefore = IpPool::withTrashed()->count();
|
||||
|
||||
try {
|
||||
Excel::import(new IpPoolImport(), $request->file('file'));
|
||||
} catch (ValidationException $e) {
|
||||
$errors = [];
|
||||
foreach ($e->failures() as $failure) {
|
||||
$errors[] = 'Dòng ' . $failure->row() . ': ' . implode(', ', $failure->errors());
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => 'Import thất bại',
|
||||
'errors' => $errors,
|
||||
], 422);
|
||||
} catch (\Exception $e) {
|
||||
return response()->json(['error' => true, 'message' => 'Import thất bại: ' . $e->getMessage()], 500);
|
||||
}
|
||||
|
||||
$countAfter = IpPool::withTrashed()->count();
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Import thành công',
|
||||
'created' => max(0, $countAfter - $countBefore),
|
||||
]);
|
||||
}
|
||||
|
||||
public function networks(): JsonResponse
|
||||
{
|
||||
$networks = ModuleNetwork::select(['id', 'name', 'type', 'network'])
|
||||
|
||||
Reference in New Issue
Block a user