-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathDatabaseController.php
More file actions
117 lines (103 loc) · 3.77 KB
/
DatabaseController.php
File metadata and controls
117 lines (103 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Http\Controllers\Api\Client\Servers;
use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
use App\Exceptions\Service\Database\TooManyDatabasesException;
use App\Facades\Activity;
use App\Http\Controllers\Api\Client\ClientApiController;
use App\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
use App\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
use App\Http\Requests\Api\Client\Servers\Databases\RotatePasswordRequest;
use App\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
use App\Models\Database;
use App\Models\Server;
use App\Services\Databases\DatabaseManagementService;
use App\Services\Databases\DeployServerDatabaseService;
use App\Transformers\Api\Client\DatabaseTransformer;
use Dedoc\Scramble\Attributes\Group;
use Illuminate\Http\Response;
use Throwable;
#[Group('Server - Database')]
class DatabaseController extends ClientApiController
{
/**
* DatabaseController constructor.
*/
public function __construct(
private DeployServerDatabaseService $deployDatabaseService,
private DatabaseManagementService $managementService,
) {
parent::__construct();
}
/**
* List databases
*
* Return all the databases that belong to the given server.
*
* @return array<string, mixed>
*/
public function index(GetDatabasesRequest $request, Server $server): array
{
return $this->fractal->collection($server->databases)
->transformWith($this->getTransformer(DatabaseTransformer::class))
->toArray();
}
/**
* Create database
*
* Create a new database for the given server and return it.
*
* @return array<string, mixed>
*
* @throws Throwable
* @throws TooManyDatabasesException
* @throws DatabaseClientFeatureNotEnabledException
*/
public function store(StoreDatabaseRequest $request, Server $server): array
{
$database = Activity::event('server:database.create')->transaction(function ($log) use ($request, $server) {
$server->databases()->lockForUpdate()->count();
$database = $this->deployDatabaseService->handle($server, $request->validated());
$log->subject($database)->property('name', $database->database);
return $database;
});
return $this->fractal->item($database)
->parseIncludes(['password'])
->transformWith($this->getTransformer(DatabaseTransformer::class))
->toArray();
}
/**
* Rotate password
*
* Rotates the password for the given server model and returns a fresh instance to
* the caller.
*
* @return array<array-key, mixed>
*
* @throws Throwable
*/
public function rotatePassword(RotatePasswordRequest $request, Server $server, Database $database): array
{
Activity::event('server:database.rotate-password')
->subject($database)
->property('name', $database->database)
->transaction(fn () => $this->managementService->rotatePassword($database));
return $this->fractal->item($database->refresh())
->parseIncludes(['password'])
->transformWith($this->getTransformer(DatabaseTransformer::class))
->toArray();
}
/**
* Delete database
*
* Removes a database from the server.
*/
public function delete(DeleteDatabaseRequest $request, Server $server, Database $database): Response
{
$this->managementService->delete($database);
Activity::event('server:database.delete')
->subject($database)
->property('name', $database->database)
->log();
return new Response('', Response::HTTP_NO_CONTENT);
}
}