private async Task <int> OnExecute(IConsole console, DatabaseContext database, ConfirmationMailService mailingService) { if (!MailUtilities.IsValidAddress(AccountName)) { console.Error.WriteLine("Please use a valid email address!"); return(1); } console.Out.WriteLine("WARNING: Argon2 hash is currently not supported!"); (var account, var confirmation, bool success) = await database.AddAccount(AccountName, Array.Empty <byte>()).ConfigureAwait(false); if (success) { console.Out.WriteLine($"Created account with ID {account.AccountId}"); console.Out.WriteLine($"Visit {mailingService.GetConfirmationUrl(confirmation.Token)} to confirm the mail address"); if (SendMail) { console.Out.WriteLine("Sending confirmation mail..."); await mailingService.SendMailAsync(AccountName, confirmation.Token).ConfigureAwait(false); } return(0); } else { console.Error.WriteLine($"The AccountName {AccountName} already exists!"); return(1); } }
private async Task OnExecute(IConsole console, IServiceProvider provider) { long accountId, channelId; Stopwatch stopwatch = new Stopwatch(); using (IServiceScope scope = provider.CreateScope()) { DatabaseContext database = scope.ServiceProvider.GetRequiredService <DatabaseContext>(); (var account, var confirmation, bool success) = await database .AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false); accountId = account.AccountId; console.Out.WriteLine($"Created account {confirmation.MailAddress} with ID {accountId}"); console.Out.WriteLine($"Created mail confirmation for {confirmation.MailAddress} with token {confirmation.Token}"); } using (IServiceScope scope = provider.CreateScope()) { DatabaseContext database = scope.ServiceProvider.GetRequiredService <DatabaseContext>(); Channel channel = await database.AddChannel(new Channel() { OwnerId = accountId }).ConfigureAwait(false); channelId = channel.ChannelId; console.Out.WriteLine($"Created channel {channelId} with owner {accountId}"); } if (AccountCount > 0) { console.Out.WriteLine($"Inserting {AccountCount} accounts..."); stopwatch.Start(); await AsyncParallel.ForAsync(0, AccountCount, async i => { using IServiceScope scope = provider.CreateScope(); DatabaseContext ctx = scope.ServiceProvider.GetRequiredService <DatabaseContext>(); await ctx.AddAccount($"{SkynetRandom.String(10)}@example.com", Array.Empty <byte>()).ConfigureAwait(false); }).ConfigureAwait(false); stopwatch.Stop(); console.Out.WriteLine($"Finished saving {AccountCount} accounts after {stopwatch.ElapsedMilliseconds}ms"); stopwatch.Reset(); } if (MessageCount > 0) { console.Out.WriteLine($"Inserting {MessageCount} messages to channel {channelId}..."); stopwatch.Start(); await AsyncParallel.ForAsync(0, MessageCount, async i => { using IServiceScope scope = provider.CreateScope(); DatabaseContext database = scope.ServiceProvider.GetRequiredService <DatabaseContext>(); Message entity = new Message { ChannelId = channelId, SenderId = accountId }; database.Messages.Add(entity); await database.SaveChangesAsync().ConfigureAwait(false); }).ConfigureAwait(false); stopwatch.Stop(); console.Out.WriteLine($"Finished saving {MessageCount} messages after {stopwatch.ElapsedMilliseconds}ms"); stopwatch.Reset(); } }