2023-10-03 11:56:56 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\Application;
|
|
|
|
use App\Models\Service;
|
|
|
|
use App\Models\StandalonePostgresql;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
use function Laravel\Prompts\confirm;
|
|
|
|
use function Laravel\Prompts\select;
|
|
|
|
|
2023-10-03 11:59:30 +02:00
|
|
|
class ResourceDelete extends Command
|
2023-10-03 11:56:56 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-10-03 11:59:30 +02:00
|
|
|
protected $signature = 'resource:delete';
|
2023-10-03 11:56:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Delete a resource from the database';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$resource = select(
|
|
|
|
'What resource do you want to delete?',
|
|
|
|
['Application', 'Database', 'Service'],
|
|
|
|
);
|
|
|
|
if ($resource === 'Application') {
|
|
|
|
$this->deleteApplication();
|
|
|
|
} elseif ($resource === 'Database') {
|
|
|
|
$this->deleteDatabase();
|
|
|
|
} elseif ($resource === 'Service') {
|
|
|
|
$this->deleteService();
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 11:59:30 +02:00
|
|
|
private function deleteApplication()
|
|
|
|
{
|
2023-10-03 11:56:56 +02:00
|
|
|
$applications = Application::all();
|
|
|
|
$application = select(
|
|
|
|
'What application do you want to delete?',
|
|
|
|
$applications->pluck('name')->toArray(),
|
|
|
|
);
|
|
|
|
$application = $applications->where('name', $application)->first();
|
|
|
|
$confirmed = confirm("Are you sure you want to delete {$application->name}?");
|
|
|
|
if (!$confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$application->delete();
|
|
|
|
}
|
2023-10-03 11:59:30 +02:00
|
|
|
private function deleteDatabase()
|
|
|
|
{
|
2023-10-03 11:56:56 +02:00
|
|
|
$databases = StandalonePostgresql::all();
|
|
|
|
$database = select(
|
|
|
|
'What database do you want to delete?',
|
|
|
|
$databases->pluck('name')->toArray(),
|
|
|
|
);
|
|
|
|
$database = $databases->where('name', $database)->first();
|
|
|
|
$confirmed = confirm("Are you sure you want to delete {$database->name}?");
|
|
|
|
if (!$confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$database->delete();
|
|
|
|
}
|
2023-10-03 11:59:30 +02:00
|
|
|
private function deleteService()
|
|
|
|
{
|
2023-10-03 11:56:56 +02:00
|
|
|
$services = Service::all();
|
|
|
|
$service = select(
|
|
|
|
'What service do you want to delete?',
|
|
|
|
$services->pluck('name')->toArray(),
|
|
|
|
);
|
|
|
|
$service = $services->where('name', $service)->first();
|
|
|
|
$confirmed = confirm("Are you sure you want to delete {$service->name}?");
|
|
|
|
if (!$confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$service->delete();
|
|
|
|
}
|
|
|
|
}
|