|
| 1 | +# Asynchronous wallet creation |
| 2 | + |
| 3 | +The idea is based on the division into teams for creating wallets, transactions, etc. The creation of a wallet can be accelerated if the client "generates a wallet himself". |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## User Model |
| 8 | + |
| 9 | +Add the `HasWallet`, `HasWallets` trait's and `Wallet` interface to model. |
| 10 | + |
| 11 | +```php |
| 12 | +use Bavix\Wallet\Traits\HasWallet; |
| 13 | +use Bavix\Wallet\Traits\HasWallets; |
| 14 | +use Bavix\Wallet\Interfaces\Wallet; |
| 15 | + |
| 16 | +class User extends Model implements Wallet |
| 17 | +{ |
| 18 | + use HasWallet, HasWallets; |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Action Handler |
| 23 | + |
| 24 | +```php |
| 25 | +use Illuminate\Http\Request; |
| 26 | +use Illuminate\Http\Response; |
| 27 | +use Illuminate\Support\Facades\Response as ResponseFactory; |
| 28 | +... |
| 29 | + |
| 30 | +public function __invoke(User $user, Request $request): Response |
| 31 | +{ |
| 32 | + $name = $request->get('wallet_name'); |
| 33 | + $uuid = $request->get('wallet_uuid'); |
| 34 | + |
| 35 | + $message = new CreateWalletCommandMessage($user, $name, $uuid); |
| 36 | + dispatch($message); |
| 37 | + |
| 38 | + return ResponseFactory::json([], 202); |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Command Handler |
| 43 | + |
| 44 | +```php |
| 45 | +public function __invoke(CreateWalletCommandMessage $message): void |
| 46 | +{ |
| 47 | + $user = $message->getUser(); |
| 48 | + $user->createWallet([ |
| 49 | + 'uuid' => $message->getWalletUuid(), |
| 50 | + 'name' => $message->getWalletName(), |
| 51 | + ]); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +You receive requests to create a wallet on the backend, and you create them asynchronously. UUID4 is generated on the client side and the client already knows it. You will not be able to create two wallets with one uuid, because the column in the database is unique. |
| 56 | + |
| 57 | +The user no longer needs to wait for the creation of a wallet, it is enough to know the uuid. You get the most stable application. |
| 58 | + |
| 59 | +It worked! |
0 commit comments