Additional Fixes

This commit is contained in:
JonatanRek 2024-07-31 18:45:14 +02:00
parent 5d8b29f828
commit bb450e3a80
10 changed files with 136 additions and 18 deletions

View File

@ -2,6 +2,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\MaintenanceHistory;
class MaintenanceController extends BaseController class MaintenanceController extends BaseController
{ {
public function index() public function index()
@ -13,4 +15,11 @@ public function planned()
{ {
return view('maintenance.planned'); return view('maintenance.planned');
} }
public function plannedDetail(MaintenanceHistory $maintenance_history)
{
return view('maintenance.planned-detail', [
'maintenance_history' => $maintenance_history,
]);
}
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace App\Livewire\Maintenance; namespace App\Livewire\Maintenance;
use App\Models\Host;
use Livewire\Component; use Livewire\Component;
use App\Models\Maintenance; use App\Models\Maintenance;
@ -8,29 +10,36 @@ class Form extends Component
{ {
public $model; public $model;
public string $name = ""; public string $name = "";
public string $description = ""; public string $description = "";
public string $schedule = ""; public string $schedule = "";
public $action = 'store'; public $action = 'store';
public $hosts = [];
public $hosts_available = [];
protected function rules() protected function rules()
{ {
return [ return [
'name' => 'required', 'name' => 'required',
'description' => 'required', 'description' => 'required',
'schedule' => '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)) { if (!empty($model)) {
$maintenance = Maintenance::find($model); $maintenance = Maintenance::find($model);
$this->model = $model; $this->model = $model;
$this->name = $maintenance->name; $this->name = $maintenance->name;
$this->description = $maintenance->description; $this->description = $maintenance->description;
$this->schedule = $maintenance->schedule; $this->schedule = $maintenance->schedule;
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
$this->action = 'update'; $this->action = 'update';
} }
@ -39,7 +48,11 @@ public function mount ($model = null){
public function store() public function store()
{ {
$validatedData = $this->validate(); $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'); $this->dispatch('closeModal');
} }
@ -47,9 +60,16 @@ public function update()
{ {
$validatedData = $this->validate(); $validatedData = $this->validate();
$maintenance = Maintenance::find($this->model); $maintenance = Maintenance::find($this->model);
$maintenance->hosts()->whereNotIn('host_id', $this->hosts)->delete();
if (!empty($maintenance)) { if (!empty($maintenance)) {
$maintenance->update($validatedData); $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'); $this->dispatch('closeModal');
} }
@ -57,4 +77,14 @@ public function render()
{ {
return view('livewire.maintenance.form'); return view('livewire.maintenance.form');
} }
public function addHost()
{
$this->hosts[] = [];
}
public function removeHost($id)
{
unset($this->hosts[$id]);
}
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace App\Livewire\MaintenanceHistory; namespace App\Livewire\MaintenanceHistory;
use App\Models\MaintenanceHistory; use App\Models\MaintenanceHistory;
@ -21,12 +22,19 @@ public function headers(): array
{ {
return [ return [
'maintenance.name' => 'maintenance.name', 'maintenance.name' => 'maintenance.name',
'start_at' => 'start_at', 'start_at' => 'start_at',
'finished_at' => 'finished_at', 'finished_at' => 'finished_at',
]; ];
} }
public function remove($maintenancehistory_id){ public function remove($maintenancehistory_id)
{
MaintenanceHistory::find($maintenancehistory_id)->delete(); MaintenanceHistory::find($maintenancehistory_id)->delete();
} }
public function renderColumnMaintenanceName($val, $row)
{
$ret = '<a href="' . route('maintenance.planned.detail', $row['id']) . '">' . e($val) . '</a>';
return $ret;
}
} }

View File

@ -19,4 +19,9 @@ public function history()
{ {
return $this->hasMany(MaintenanceHistory::class); return $this->hasMany(MaintenanceHistory::class);
} }
public function hosts()
{
return $this->belongsToMany(Host::class, "maintenance_host", "maintenance_id", "host_id" );
}
} }

View File

@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class MaintenanceHost extends Pivot
{
//
}

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Models\Maintenance;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
@ -13,7 +14,8 @@ public function up(): void
{ {
Schema::create('maintenance_tasks', function (Blueprint $table) { Schema::create('maintenance_tasks', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('hostname'); $table->foreignIdFor(Maintenance::class);
$table->string('name');
$table->text('description'); $table->text('description');
$table->timestamps(); $table->timestamps();
}); });

View File

@ -0,0 +1,31 @@
<?php
use App\Models\Host;
use App\Models\Maintenance;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('maintenance_host', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Maintenance::class);
$table->foreignIdFor(Host::class);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('maintenance_host');
}
};

View File

@ -1,8 +1,10 @@
<div> <div>
<x-form::form wire:submit.prevent="{{$action}}"> <x-form::form wire:submit.prevent="{{ $action }}">
<x-form::input group-class="mb-3" type="text" wire:model="name" id="name" label="name"/> <x-form::input group-class="mb-3" type="text" wire:model="name" id="name" label="name" />
<x-form::quill group-class="mb-3" type="text" wire:model="description" id="description" label="description"/> <x-form::quill group-class="mb-3" type="text" wire:model="description" id="description" label="description" />
<x-form::input group-class="mb-3" type="text" wire:model="schedule" id="schedule" label="schedule"/> <x-form::input group-class="mb-3" type="text" wire:model="schedule" id="schedule" label="schedule" />
<x-form::button class="btn-primary" type="submit">Create</x-form::button> <x-form::select group-class="mb-3" wire:model="hosts" label="Livewire Select" :options="$hosts_available" placeholder="Select value..." multiple />
</x-form::form> <x-form::button class="btn-primary" type="submit">Create</x-form::button>
@dump($hosts)
</x-form::form>
</div> </div>

View File

@ -0,0 +1,20 @@
<x-layout-app>
<div class="container-xl">
<div class="page-header">
<h1>{{ __('Planned Maintenance') }}</h1>
</div>
<p>{!! $maintenance_history->maintenance->description !!}</p>
@foreach ($maintenance_history->maintenance->hosts as $host)
<div class="mb-3">
<div class="btn-toggle btn py-3 w-100 bg-primary text-start" style="--bs-bg-opacity: .2;" data-bs-toggle="collapse" data-bs-target="#collapse{{ $host->id }}">
<b>{{ $host->hostname }}</b>
</div>
<div class="collapse" id="collapse{{ $host->id }}">
<div class="card card-body mt-2">
<h6>Description</h6>
</div>
</div>
</div>
@endforeach
</div>
</x-layout-app>

View File

@ -9,6 +9,7 @@
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); 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', [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'); Route::get('/host', [App\Http\Controllers\HostController::class, 'index'])->name('host');