prevent dup wallets
This commit is contained in:
@@ -4,36 +4,45 @@ using Toolkit.Foundation;
|
||||
|
||||
namespace Wallet;
|
||||
|
||||
public class CreateWalletHandler(IWalletHostFactory componentFactory,
|
||||
public class CreateWalletHandler(IHostEnvironment environment,
|
||||
IWalletHostFactory componentFactory,
|
||||
IWalletHostCollection wallets,
|
||||
IPublisher publisher) :
|
||||
IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, bool>
|
||||
IHandler<CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>>, Result>
|
||||
{
|
||||
public async Task<bool> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args,
|
||||
public async Task<Result> Handle(CreateEventArgs<Wallet<(string, string, IImageDescriptor?)>> args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (args.Sender is Wallet<(string, string, IImageDescriptor?)> wallet)
|
||||
if (args.Sender is not Wallet<(string, string, IImageDescriptor?)> wallet)
|
||||
{
|
||||
(string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value;
|
||||
return Result.Failure(Error.Failure);
|
||||
}
|
||||
|
||||
if (name is { Length: > 0 } &&
|
||||
password is { Length: > 0 })
|
||||
(string name, string password, IImageDescriptor? imageDescriptor) = wallet.Value;
|
||||
name = name.Trim();
|
||||
|
||||
if (Directory.Exists(Path.Combine(environment.ContentRootPath, "Wallet", name)))
|
||||
{
|
||||
return Result.Failure(Error.Duplicated);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
|
||||
{
|
||||
return Result.Failure(Error.Null);
|
||||
}
|
||||
|
||||
if (componentFactory.Create(name) is IComponentHost host)
|
||||
{
|
||||
IWalletFactory walletFactory = host.Services.GetRequiredService<IWalletFactory>();
|
||||
if (await walletFactory.Create(name, password, imageDescriptor))
|
||||
{
|
||||
if (componentFactory.Create(name) is IComponentHost host)
|
||||
{
|
||||
IWalletFactory walletFactory = host.Services.GetRequiredService<IWalletFactory>();
|
||||
if (await walletFactory.Create(name, password, imageDescriptor))
|
||||
{
|
||||
wallets.Add(host);
|
||||
host.Start();
|
||||
wallets.Add(host);
|
||||
host.Start();
|
||||
|
||||
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
publisher.Publish(Activated.As(new Wallet<IComponentHost>(host)));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return Result.Success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user