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;
use App\Models\MaintenanceHistory;
class MaintenanceController extends BaseController
{
public function index()
@ -13,4 +15,11 @@ public function 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
namespace App\Livewire\Maintenance;
use App\Models\Host;
use Livewire\Component;
use App\Models\Maintenance;
@ -13,6 +15,10 @@ class Form extends Component
public $action = 'store';
public $hosts = [];
public $hosts_available = [];
protected function rules()
{
return [
@ -22,7 +28,9 @@ protected function rules()
];
}
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);
@ -31,6 +39,7 @@ public function mount ($model = null){
$this->name = $maintenance->name;
$this->description = $maintenance->description;
$this->schedule = $maintenance->schedule;
$this->hosts = $maintenance->hosts()->pluck('hosts.id')->toArray();
$this->action = 'update';
}
@ -39,7 +48,11 @@ public function mount ($model = null){
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 @@ public function update()
{
$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 @@ public function render()
{
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
namespace App\Livewire\MaintenanceHistory;
use App\Models\MaintenanceHistory;
@ -26,7 +27,14 @@ public function headers(): array
];
}
public function remove($maintenancehistory_id){
public function remove($maintenancehistory_id)
{
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);
}
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
use App\Models\Maintenance;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
@ -13,7 +14,8 @@ public function up(): void
{
Schema::create('maintenance_tasks', function (Blueprint $table) {
$table->id();
$table->string('hostname');
$table->foreignIdFor(Maintenance::class);
$table->string('name');
$table->text('description');
$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

@ -3,6 +3,8 @@
<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::input group-class="mb-3" type="text" wire:model="schedule" id="schedule" label="schedule" />
<x-form::select group-class="mb-3" wire:model="hosts" label="Livewire Select" :options="$hosts_available" placeholder="Select value..." multiple />
<x-form::button class="btn-primary" type="submit">Create</x-form::button>
@dump($hosts)
</x-form::form>
</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('/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');