-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: manage vault in api (monicahq/chandler#313)
- Loading branch information
Showing
20 changed files
with
479 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
app/Domains/Vault/ManageVault/Api/Controllers/VaultController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?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; | ||
use Knuckles\Scribe\Attributes\QueryParam; | ||
use Knuckles\Scribe\Attributes\Response; | ||
use Knuckles\Scribe\Attributes\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, int $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, int $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, int $vaultId) | ||
{ | ||
$data = [ | ||
'account_id' => $request->user()->account_id, | ||
'author_id' => $request->user()->id, | ||
'vault_id' => $vaultId, | ||
]; | ||
|
||
(new DestroyVault())->execute($data); | ||
|
||
return $this->respondObjectDeleted($vaultId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use App\Helpers\DateHelper; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
/** | ||
* @mixin \App\Models\Vault | ||
*/ | ||
class VaultResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'name' => $this->name, | ||
'description' => $this->description, | ||
'created_at' => DateHelper::getTimestamp($this->created_at), | ||
'updated_at' => DateHelper::getTimestamp($this->updated_at), | ||
'links' => [ | ||
'self' => route('api.vaults.show', $this), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.