lasthourcloud/app/Http/Controllers/MagicController.php

75 lines
2.0 KiB
PHP
Raw Normal View History

2023-06-07 13:08:35 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\Environment;
use App\Models\Project;
use App\Models\Server;
2023-06-16 13:01:58 +00:00
use App\Models\Team;
2023-06-07 13:08:35 +00:00
class MagicController extends Controller
{
public function servers()
{
return response()->json([
2023-07-14 11:38:24 +00:00
'servers' => Server::isUsable()->get()
2023-06-07 13:08:35 +00:00
]);
}
2023-06-07 13:08:35 +00:00
public function destinations()
{
return response()->json([
'destinations' => Server::destinationsByServer(request()->query('server_id'))->sortBy('name')
]);
}
2023-06-07 13:08:35 +00:00
public function projects()
{
return response()->json([
'projects' => Project::ownedByCurrentTeam()->get()
]);
}
2023-06-07 13:08:35 +00:00
public function environments()
{
return response()->json([
'environments' => Project::ownedByCurrentTeam()->whereUuid(request()->query('project_uuid'))->first()->environments
]);
}
2023-06-15 07:15:41 +00:00
public function newProject()
2023-06-07 13:08:35 +00:00
{
$project = Project::firstOrCreate(
['name' => request()->query('name') ?? generate_random_name()],
2023-08-22 15:44:49 +00:00
['team_id' => currentTeam()->id]
2023-06-07 13:08:35 +00:00
);
return response()->json([
'project_uuid' => $project->uuid
]);
}
2023-06-15 07:15:41 +00:00
public function newEnvironment()
2023-06-07 13:08:35 +00:00
{
$environment = Environment::firstOrCreate(
['name' => request()->query('name') ?? generate_random_name()],
['project_id' => Project::ownedByCurrentTeam()->whereUuid(request()->query('project_uuid'))->firstOrFail()->id]
);
return response()->json([
'environment_name' => $environment->name,
]);
}
2023-06-16 13:01:58 +00:00
public function newTeam()
{
$team = Team::create(
[
'name' => request()->query('name') ?? generate_random_name(),
'personal_team' => false,
],
);
auth()->user()->teams()->attach($team, ['role' => 'admin']);
2023-08-22 15:44:49 +00:00
refreshSession();
2023-06-16 13:01:58 +00:00
return redirect(request()->header('Referer'));
}
2023-06-07 13:08:35 +00:00
}