50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Datacenter;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.admin')]
|
|
class Datacenters extends Component
|
|
{
|
|
#[Validate('required|string|max:8|alpha_dash|unique:datacenters,code')]
|
|
public string $code = '';
|
|
|
|
#[Validate('required|string|max:255')]
|
|
public string $name = '';
|
|
|
|
#[Validate('nullable|string|max:255')]
|
|
public string $location = '';
|
|
|
|
public function save(): void
|
|
{
|
|
$data = $this->validate();
|
|
|
|
Datacenter::create([
|
|
'code' => strtolower($data['code']),
|
|
'name' => $data['name'],
|
|
'location' => $data['location'] ?: null,
|
|
'active' => true,
|
|
]);
|
|
|
|
$this->reset('code', 'name', 'location');
|
|
$this->dispatch('notify', message: __('datacenters.created'));
|
|
}
|
|
|
|
public function toggle(string $uuid): void
|
|
{
|
|
$datacenter = Datacenter::query()->where('uuid', $uuid)->first();
|
|
$datacenter?->update(['active' => ! $datacenter->active]);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.datacenters', [
|
|
'datacenters' => Datacenter::query()->withCount('hosts')->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
}
|