-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
VaultController.php
126 lines (109 loc) · 3.88 KB
/
VaultController.php
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
118
119
120
121
122
123
124
125
126
<?php
namespace App\Domains\Vault\ManageVault\Api\Controllers;
use App\Domains\Vault\ManageVault\Services\CreateVault;
use App\Domains\Vault\ManageVault\Services\DestroyVault;
use App\Domains\Vault\ManageVault\Services\UpdateVault;
use App\Http\Controllers\ApiController;
use App\Http\Resources\VaultResource;
use App\Models\Vault;
use Illuminate\Http\Request;
use Knuckles\Scribe\Attributes\{BodyParam,QueryParam,Response,ResponseFromApiResource};
/**
* @group Vault management
*
* @subgroup Vaults
*/
class VaultController extends ApiController
{
public function __construct()
{
$this->middleware('abilities:read')->only(['index', 'show']);
$this->middleware('abilities:write')->only(['store', 'update', 'delete']);
parent::__construct();
}
/**
* List all vaults.
*
* Get all the vaults in the account.
*/
#[QueryParam('limit', 'int', description: 'A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 10.', required: false, example: 10)]
#[ResponseFromApiResource(VaultResource::class, Vault::class, collection: true)]
public function index(Request $request)
{
$vaults = $request->user()->account->vaults()
->paginate($this->getLimitPerPage());
return VaultResource::collection($vaults);
}
/**
* Create a vault.
*
* Creates a vault object.
*/
#[BodyParam('name', description: 'The name of the vault. Max 255 characters.')]
#[BodyParam('description', description: 'The description of the vault. Max 65535 characters.', required: false)]
#[ResponseFromApiResource(VaultResource::class, Vault::class, status: 201)]
public function store(Request $request)
{
$data = [
'account_id' => $request->user()->account_id,
'author_id' => $request->user()->id,
'type' => Vault::TYPE_PERSONAL,
'name' => $request->input('name'),
'description' => $request->input('description'),
];
$vault = (new CreateVault())->execute($data);
return new VaultResource($vault);
}
/**
* Retrieve a vault.
*
* Get a specific vault object.
*/
#[ResponseFromApiResource(VaultResource::class, Vault::class)]
public function show(Request $request, string $vaultId)
{
$vault = $request->user()->account->vaults()
->findOrFail($vaultId);
return new VaultResource($vault);
}
/**
* Update a vault.
*
* Updates a vault object.
*
* If the call succeeds, the response is the same as the one for the
* Retrieve a vault endpoint.
*/
#[BodyParam('name', description: 'The name of the vault. Max 255 characters.')]
#[BodyParam('description', description: 'The description of the vault. Max 65535 characters.', required: false)]
#[ResponseFromApiResource(VaultResource::class, Vault::class)]
public function update(Request $request, string $vaultId)
{
$data = [
'account_id' => $request->user()->account_id,
'author_id' => $request->user()->id,
'vault_id' => $vaultId,
'name' => $request->input('name'),
'description' => $request->input('description'),
];
$vault = (new UpdateVault())->execute($data);
return new VaultResource($vault);
}
/**
* Delete a vault.
*
* Destroys a vault object.
* Warning: everything in the vault will be immediately deleted.
*/
#[Response(['deleted' => true, 'id' => 1])]
public function destroy(Request $request, string $vaultId)
{
$data = [
'account_id' => $request->user()->account_id,
'author_id' => $request->user()->id,
'vault_id' => $vaultId,
];
(new DestroyVault())->execute($data);
return $this->respondObjectDeleted($vaultId);
}
}