fix: do not allow to delete env if a resource is defined

This commit is contained in:
Andras Bacsai 2023-10-24 10:11:21 +02:00
parent 554222abc7
commit 8bfc1a7c06
4 changed files with 12 additions and 12 deletions

View File

@ -21,10 +21,10 @@ public function delete()
'environment_id' => 'required|int',
]);
$environment = Environment::findOrFail($this->environment_id);
if ($environment->applications->count() > 0) {
return $this->emit('error', 'Environment has resources defined, please delete them first.');
}
if ($environment->isEmpty()) {
$environment->delete();
return redirect()->route('project.show', ['project_uuid' => $this->parameters['project_uuid']]);
}
return $this->emit('error', 'Environment has defined resources, please delete them first.');
}
}

View File

@ -12,7 +12,7 @@ class Environment extends Model
'project_id',
];
public function can_delete_environment()
public function isEmpty()
{
return $this->applications()->count() == 0 &&
$this->redis()->count() == 0 &&

View File

@ -18,7 +18,7 @@ protected static function booted()
'project_id' => $project->id,
]);
Environment::create([
'name' => 'Production',
'name' => 'production',
'project_id' => $project->id,
]);
});

View File

@ -2,17 +2,17 @@
<div class="flex flex-col">
<div class="flex items-center gap-2">
<h1>Resources</h1>
@if ($environment->can_delete_environment())
<a class="font-normal text-white normal-case border-none rounded hover:no-underline btn btn-primary btn-sm no-animation"
href="{{ route('project.clone', ['project_uuid' => data_get($project, 'uuid'), 'environment_name' => request()->route('environment_name')]) }}">
Clone
</a>
@if ($environment->isEmpty())
<livewire:project.delete-environment :environment_id="$environment->id" />
@else
<a href="{{ route('project.resources.new', ['project_uuid' => request()->route('project_uuid'), 'environment_name' => request()->route('environment_name')]) }} "
class="font-normal text-white normal-case border-none rounded hover:no-underline btn btn-primary btn-sm no-animation">+
New</a>
@endif
<a class="font-normal text-white normal-case border-none rounded hover:no-underline btn btn-primary btn-sm no-animation"
href="{{ route('project.clone', ['project_uuid' => data_get($project, 'uuid'), 'environment_name' => request()->route('environment_name')]) }}">
Clone
</a>
</div>
<nav class="flex pt-2 pb-10">
<ol class="flex items-center">
@ -36,7 +36,7 @@ class="font-normal text-white normal-case border-none rounded hover:no-underline
</ol>
</nav>
</div>
@if ($environment->can_delete_environment())
@if ($environment->isEmpty())
<a href="{{ route('project.resources.new', ['project_uuid' => request()->route('project_uuid'), 'environment_name' => request()->route('environment_name')]) }} "
class="items-center justify-center box">+ Add New Resource</a>
@endif