Перевод защищённой части приложения на Vue.js + Inertia
Дашборд, зоны, устройства, правила автоматизации и профиль теперь Vue 3 + Inertia вместо Blade+Alpine — гостевые страницы Breeze (логин/регистрация) сознательно оставлены на Blade, чтобы не трогать уже протестированную аутентификацию. Контроллеры возвращают Inertia::render() вместо view(), переиспользуют существующие API Resources и FormRequest. Ключевой нюанс: Resource/ResourceCollection, переданные напрямую в проп Inertia, заворачиваются в ключ "data" (Inertia вызывает toResponse() у Responsable-объектов) — везде используется ->resolve() и явное резолвление вложенных ресурсов (zone/device_type внутри Device и т.п.), иначе вложенные поля тоже задваивались бы обёрткой. Alpine.js и все Blade-вьюхи защищённой части удалены как мёртвый код. Тесты (ZoneControllerTest/DeviceControllerTest) переведены на assertInertia(). UserFactory теперь явно задаёт role=owner — без этого Eloquent не подтягивает DB-default обратно в модель после create(), что уронило бы общий auth.user проп на любой странице.
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\DeviceRequest;
|
||||
use App\Http\Resources\DeviceResource;
|
||||
use App\Http\Resources\DeviceTypeResource;
|
||||
use App\Http\Resources\ZoneResource;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceType;
|
||||
use App\Models\Zone;
|
||||
@@ -10,6 +13,7 @@ use App\Services\ClickHouseClient;
|
||||
use App\Services\DeviceControlClient;
|
||||
use App\Services\DeviceShadow;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class DeviceController extends Controller
|
||||
{
|
||||
@@ -19,8 +23,11 @@ class DeviceController extends Controller
|
||||
|
||||
$devices = Device::with(['zone', 'deviceType'])->orderBy('name')->get();
|
||||
$statuses = $shadow->statuses($devices->pluck('external_id')->all());
|
||||
$devices->each(fn (Device $d) => $d->live_status = $statuses[$d->external_id] ?? 'unknown');
|
||||
|
||||
return view('devices.index', compact('devices', 'statuses'));
|
||||
return Inertia::render('Devices/Index', [
|
||||
'devices' => DeviceResource::collection($devices)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Device $device, DeviceShadow $shadow, ClickHouseClient $clickHouse)
|
||||
@@ -28,6 +35,8 @@ class DeviceController extends Controller
|
||||
$this->authorize('view', $device);
|
||||
|
||||
$device->load(['zone', 'deviceType']);
|
||||
$snapshot = $shadow->snapshot($device->external_id);
|
||||
$device->live_status = $snapshot['status'];
|
||||
|
||||
$telemetry = [];
|
||||
if ($device->deviceType->category->value === 'sensor') {
|
||||
@@ -39,9 +48,13 @@ class DeviceController extends Controller
|
||||
);
|
||||
}
|
||||
|
||||
return view('devices.show', [
|
||||
'device' => $device,
|
||||
'shadow' => $shadow->snapshot($device->external_id),
|
||||
return Inertia::render('Devices/Show', [
|
||||
'device' => (new DeviceResource($device))->resolve(),
|
||||
'shadow' => [
|
||||
'last_seen' => $snapshot['last_seen'],
|
||||
'desired_state' => $snapshot['desired_state'],
|
||||
'reported_state' => $snapshot['reported_state'],
|
||||
],
|
||||
'telemetry' => $telemetry,
|
||||
]);
|
||||
}
|
||||
@@ -50,7 +63,7 @@ class DeviceController extends Controller
|
||||
{
|
||||
$this->authorize('create', Device::class);
|
||||
|
||||
return view('devices.create', $this->formOptions());
|
||||
return Inertia::render('Devices/Create', $this->formOptions());
|
||||
}
|
||||
|
||||
public function store(DeviceRequest $request)
|
||||
@@ -66,7 +79,10 @@ class DeviceController extends Controller
|
||||
{
|
||||
$this->authorize('update', $device);
|
||||
|
||||
return view('devices.edit', ['device' => $device, ...$this->formOptions()]);
|
||||
return Inertia::render('Devices/Edit', [
|
||||
'device' => $device->only(['id', 'name', 'zone_id', 'device_type_id', 'external_id', 'protocol']),
|
||||
...$this->formOptions(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(DeviceRequest $request, Device $device)
|
||||
@@ -137,8 +153,8 @@ class DeviceController extends Controller
|
||||
private function formOptions(): array
|
||||
{
|
||||
return [
|
||||
'zones' => Zone::orderBy('name')->get(),
|
||||
'deviceTypes' => DeviceType::orderBy('code')->get(),
|
||||
'zones' => ZoneResource::collection(Zone::orderBy('name')->get())->resolve(),
|
||||
'deviceTypes' => DeviceTypeResource::collection(DeviceType::orderBy('code')->get())->resolve(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user