diff --git a/app/Http/Controllers/MaintenanceController.php b/app/Http/Controllers/MaintenanceController.php index 5ed5d20..b70a708 100644 --- a/app/Http/Controllers/MaintenanceController.php +++ b/app/Http/Controllers/MaintenanceController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Models\MaintenanceHistory; + class MaintenanceController extends BaseController { public function index() @@ -13,4 +15,11 @@ class MaintenanceController extends BaseController { return view('maintenance.planned'); } + + public function plannedDetail(MaintenanceHistory $maintenance_history) + { + return view('maintenance.planned-detail', [ + 'maintenance_history' => $maintenance_history, + ]); + } } diff --git a/app/Livewire/Maintenance/Form.php b/app/Livewire/Maintenance/Form.php index 543c6e9..d951719 100644 --- a/app/Livewire/Maintenance/Form.php +++ b/app/Livewire/Maintenance/Form.php @@ -1,6 +1,8 @@ 'required', - 'description' => 'required', - 'schedule' => 'required', + 'description' => 'required', + 'schedule' => 'required', ]; } - public function mount ($model = null){ + public function mount($model = null) + { + $this->hosts_available = Host::all()->pluck('hostname', 'id')->toArray(); if (!empty($model)) { $maintenance = Maintenance::find($model); $this->model = $model; $this->name = $maintenance->name; - $this->description = $maintenance->description; - $this->schedule = $maintenance->schedule; + $this->description = $maintenance->description; + $this->schedule = $maintenance->schedule; + $this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray(); $this->action = 'update'; } @@ -39,7 +48,11 @@ class Form extends Component public function store() { $validatedData = $this->validate(); - Maintenance::create($validatedData); + $maintenance = Maintenance::create($validatedData); + $hosts = Host::whereIn('id', $this->hosts)->get(); + foreach ($hosts as $key => $host) { + $maintenance->hosts()->attach($host); + } $this->dispatch('closeModal'); } @@ -47,9 +60,16 @@ class Form extends Component { $validatedData = $this->validate(); $maintenance = Maintenance::find($this->model); + $maintenance->hosts()->whereNotIn('host_id', $this->hosts)->delete(); + if (!empty($maintenance)) { $maintenance->update($validatedData); + $hosts = Host::whereIn('id', $this->hosts)->whereNotIn('id', $maintenance->hosts()->pluck('hosts.id')->toArray())->get(); + foreach ($hosts as $key => $host) { + $maintenance->hosts()->attach($host); + } } + $this->dispatch('closeModal'); } @@ -57,4 +77,14 @@ class Form extends Component { return view('livewire.maintenance.form'); } + + public function addHost() + { + $this->hosts[] = []; + } + + public function removeHost($id) + { + unset($this->hosts[$id]); + } } diff --git a/app/Livewire/MaintenanceHistory/DataTable.php b/app/Livewire/MaintenanceHistory/DataTable.php index cd1b0a0..e10ca5d 100644 --- a/app/Livewire/MaintenanceHistory/DataTable.php +++ b/app/Livewire/MaintenanceHistory/DataTable.php @@ -1,4 +1,5 @@ 'maintenance.name', - 'start_at' => 'start_at', - 'finished_at' => 'finished_at', + 'start_at' => 'start_at', + 'finished_at' => 'finished_at', ]; } - public function remove($maintenancehistory_id){ + public function remove($maintenancehistory_id) + { MaintenanceHistory::find($maintenancehistory_id)->delete(); } + + public function renderColumnMaintenanceName($val, $row) + { + $ret = '' . e($val) . ''; + return $ret; + } } diff --git a/app/Models/Maintenance.php b/app/Models/Maintenance.php index 0bde4b8..decb1a0 100644 --- a/app/Models/Maintenance.php +++ b/app/Models/Maintenance.php @@ -19,4 +19,9 @@ class Maintenance extends Model { return $this->hasMany(MaintenanceHistory::class); } + + public function hosts() + { + return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" ); + } } diff --git a/app/Models/MaintenanceHost.php b/app/Models/MaintenanceHost.php new file mode 100644 index 0000000..c1dc913 --- /dev/null +++ b/app/Models/MaintenanceHost.php @@ -0,0 +1,10 @@ +id(); - $table->string('hostname'); + $table->foreignIdFor(Maintenance::class); + $table->string('name'); $table->text('description'); $table->timestamps(); }); diff --git a/database/migrations/2024_07_31_155038_create_maintenance_host_table.php b/database/migrations/2024_07_31_155038_create_maintenance_host_table.php new file mode 100644 index 0000000..fc471ee --- /dev/null +++ b/database/migrations/2024_07_31_155038_create_maintenance_host_table.php @@ -0,0 +1,31 @@ +id(); + $table->foreignIdFor(Maintenance::class); + $table->foreignIdFor(Host::class); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('maintenance_host'); + } +}; diff --git a/resources/views/livewire/maintenance/form.blade.php b/resources/views/livewire/maintenance/form.blade.php index 36df09a..f6abada 100644 --- a/resources/views/livewire/maintenance/form.blade.php +++ b/resources/views/livewire/maintenance/form.blade.php @@ -1,8 +1,10 @@
- - - - - Create - + + + + + + Create + @dump($hosts) +
diff --git a/resources/views/maintenance/planned-detail.blade.php b/resources/views/maintenance/planned-detail.blade.php new file mode 100644 index 0000000..e85fb34 --- /dev/null +++ b/resources/views/maintenance/planned-detail.blade.php @@ -0,0 +1,20 @@ + +
+ +

{!! $maintenance_history->maintenance->description !!}

+ @foreach ($maintenance_history->maintenance->hosts as $host) +
+
+ {{ $host->hostname }} +
+
+
+
Description
+
+
+
+ @endforeach +
+
diff --git a/routes/web.php b/routes/web.php index fe1717e..0eddda3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -9,6 +9,7 @@ Route::get('/', function () { Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); Route::get('/maintenance/planned', [App\Http\Controllers\MaintenanceController::class, 'planned'])->name('maintenance.planned'); +Route::get('/maintenance/planned/{maintenance_history}', [App\Http\Controllers\MaintenanceController::class, 'plannedDetail'])->name('maintenance.planned.detail'); Route::get('/host', [App\Http\Controllers\HostController::class, 'index'])->name('host');