-
-
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: display vaults (monicahq/chandler#2)
- Loading branch information
Showing
45 changed files
with
1,923 additions
and
236,146 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
21 changes: 21 additions & 0 deletions
21
app/Features/Vault/ManageVault/ViewHelpers/VaultCreateViewHelper.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,21 @@ | ||
<?php | ||
|
||
namespace App\Features\Vault\ManageVault\ViewHelpers; | ||
|
||
class VaultCreateViewHelper | ||
{ | ||
/** | ||
* Get all the data needed for the page. | ||
* | ||
* @return array | ||
*/ | ||
public static function data(): array | ||
{ | ||
return [ | ||
'url' => [ | ||
'store' => route('vault.store'), | ||
'back' => route('vault.index'), | ||
], | ||
]; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
app/Features/Vault/ManageVault/ViewHelpers/VaultIndexViewHelper.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,69 @@ | ||
<?php | ||
|
||
namespace App\Features\Vault\ManageVault\ViewHelpers; | ||
|
||
use App\Models\Vault; | ||
use App\Models\Account; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class VaultIndexViewHelper | ||
{ | ||
/** | ||
* Get all the data needed for the general layout page. | ||
* | ||
* @param Vault $vault | ||
* @return array | ||
*/ | ||
public static function layoutData(Vault $vault = null): array | ||
{ | ||
return [ | ||
'user' => [ | ||
'name' => Auth::user()->name, | ||
], | ||
'vault' => $vault ? [ | ||
'id' => $vault->id, | ||
'name' => $vault->name, | ||
] : null, | ||
'url' => [ | ||
'vaults' => route('vault.index'), | ||
'logout' => route('logout'), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Get all the data needed for the general layout page. | ||
* | ||
* @param Account $account | ||
* @return array | ||
*/ | ||
public static function data(Account $account): array | ||
{ | ||
$vaults = Vault::where('account_id', $account->id) | ||
->orderBy('name', 'asc') | ||
->get(); | ||
|
||
$vaultCollection = collect(); | ||
foreach ($vaults as $vault) { | ||
$vaultCollection->push([ | ||
'id' => $vault->id, | ||
'name' => $vault->name, | ||
'description' => $vault->description, | ||
'url' => [ | ||
'show' => route('vault.show', [ | ||
'vault' => $vault, | ||
]), | ||
], | ||
]); | ||
} | ||
|
||
return [ | ||
'vaults' => $vaultCollection, | ||
'url' => [ | ||
'vault' => [ | ||
'new' => route('vault.new'), | ||
], | ||
], | ||
]; | ||
} | ||
} |
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,82 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Vault; | ||
|
||
use Inertia\Inertia; | ||
use App\Models\Vault; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use App\Http\Controllers\Controller; | ||
use Illuminate\Support\Facades\Auth; | ||
use App\Features\Vault\ManageVault\Services\CreateVault; | ||
use App\Features\Vault\ManageVault\ViewHelpers\VaultIndexViewHelper; | ||
use App\Features\Vault\ManageVault\ViewHelpers\VaultCreateViewHelper; | ||
|
||
class VaultController extends Controller | ||
{ | ||
/** | ||
* Show all the vaults of the user. | ||
* | ||
* @return Response | ||
*/ | ||
public function index() | ||
{ | ||
return Inertia::render('Vault/Index', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData(), | ||
'data' => VaultIndexViewHelper::data(Auth::user()->account), | ||
]); | ||
} | ||
|
||
/** | ||
* Display the create vault page. | ||
* | ||
* @return Response | ||
*/ | ||
public function new() | ||
{ | ||
return Inertia::render('Vault/Create', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData(), | ||
'data' => VaultCreateViewHelper::data(), | ||
]); | ||
} | ||
|
||
/** | ||
* Store the vault. | ||
* | ||
* @param Request $request | ||
* @return Response | ||
*/ | ||
public function store(Request $request) | ||
{ | ||
$data = [ | ||
'account_id' => Auth::user()->account_id, | ||
'author_id' => Auth::user()->id, | ||
'type' => Vault::TYPE_PERSONAL, | ||
'name' => $request->input('name'), | ||
'description' => $request->input('description'), | ||
]; | ||
|
||
(new CreateVault)->execute($data); | ||
|
||
return response()->json([ | ||
'data' => route('vault.index'), | ||
], 201); | ||
} | ||
|
||
/** | ||
* Display the vault. | ||
* | ||
* @param Request $request | ||
* @param int $vaultId | ||
* @return Response | ||
*/ | ||
public function show(Request $request, int $vaultId) | ||
{ | ||
$vault = Vault::find($vaultId); | ||
|
||
return Inertia::render('Vault/Show', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => VaultCreateViewHelper::data(), | ||
]); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class CheckVaultAccess | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle(Request $request, Closure $next) | ||
{ | ||
$requestedVaultId = $request->route()->parameter('vault'); | ||
|
||
$exists = DB::table('user_vault')->where('user_id', Auth::user()->id) | ||
->where('vault_id', $requestedVaultId) | ||
->count() > 0; | ||
|
||
if ($exists) { | ||
return $next($request); | ||
} else { | ||
abort(401); | ||
} | ||
} | ||
} |
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.