Управление типами устройств из UI (список + создание)
Раньше device_types заводились только через сидер — теперь owner может посмотреть все типы (карточки: иконка по категории, capabilities тегами, число устройств этого типа) и добавить новый прямо из интерфейса: код, категория, возможности вводятся тегами (можно вписать вручную и добавить по Enter/кнопке). Ссылка на список — со страницы устройств. DeviceTypePolicy зеркалит остальные Policy (view — всем, create — только owner). DeviceTypeResource получил devices_count через whenCounted — опционально, не ломает существующий API-эндпоинт (там withCount не вызывается, поле просто не появляется).
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\DeviceTypeRequest;
|
||||
use App\Http\Resources\DeviceTypeResource;
|
||||
use App\Models\DeviceType;
|
||||
use Inertia\Inertia;
|
||||
|
||||
class DeviceTypeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$this->authorize('viewAny', DeviceType::class);
|
||||
|
||||
$deviceTypes = DeviceType::withCount('devices')->orderBy('code')->get();
|
||||
|
||||
return Inertia::render('DeviceTypes/Index', [
|
||||
'deviceTypes' => DeviceTypeResource::collection($deviceTypes)->resolve(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create', DeviceType::class);
|
||||
|
||||
return Inertia::render('DeviceTypes/Create');
|
||||
}
|
||||
|
||||
public function store(DeviceTypeRequest $request)
|
||||
{
|
||||
DeviceType::create($request->validated());
|
||||
|
||||
return redirect()->route('device-types.index')->with('status', 'Тип устройства создан.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Enums\DeviceCategory;
|
||||
use App\Models\DeviceType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DeviceTypeRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('create', DeviceType::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code' => ['required', 'string', 'max:255', 'alpha_dash', Rule::unique('device_types', 'code')],
|
||||
'category' => ['required', Rule::enum(DeviceCategory::class)],
|
||||
'capabilities' => ['array'],
|
||||
'capabilities.*' => ['string', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ class DeviceTypeResource extends JsonResource
|
||||
'code' => $this->code,
|
||||
'category' => $this->category->value,
|
||||
'capabilities' => $this->capabilities,
|
||||
'devices_count' => $this->whenCounted('devices'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user