public ValueTask <Topic?> ExecuteAsync(Topic topic, IServiceProvider serviceProvider, CancellationToken ct) { Validate <Validator> .It(this); var newTopic = topic with { IsExplicit = true }; if (Is.Changed(Name, topic.Name)) { newTopic = newTopic with { Name = Name.Trim() }; } if (Is.Changed(Description, topic.Description)) { newTopic = newTopic with { Description = Description.Trim() }; } if (Is.Changed(ShowAutomatically, topic.ShowAutomatically)) { newTopic = newTopic with { ShowAutomatically = ShowAutomatically.Value }; } if (Channels != null) { newTopic = newTopic with { Channels = Channels }; } return(new ValueTask <Topic?>(newTopic)); } } }
public async ValueTask <ChannelTemplate <T>?> ExecuteAsync(ChannelTemplate <T> template, IServiceProvider serviceProvider, CancellationToken ct) { var newTemplate = template; if (Languages != null) { var languages = new Dictionary <string, T>(); var factory = serviceProvider.GetRequiredService <IChannelTemplateFactory <T> >(); foreach (var(key, value) in Languages) { languages[key] = await factory.ParseAsync(value, ct); } newTemplate = newTemplate with { Languages = languages.ToReadonlyDictionary() }; } if (Is.Changed(Name, template.Name)) { newTemplate = newTemplate with { Name = Name.Trim() }; } if (Is.Changed(Primary, template.Primary)) { newTemplate = newTemplate with { Primary = Primary.Value }; } return(newTemplate); } } }
public async ValueTask <App?> ExecuteAsync(App app, IServiceProvider serviceProvider, CancellationToken ct) { Validate <Validator> .It(this); var newApp = app; if (Is.Changed(Languages, app.Languages)) { newApp = newApp with { Languages = Languages.ToReadonlyList() }; } if (Is.Changed(Name, app.Name)) { newApp = newApp with { Name = Name.Trim() }; } if (Is.Changed(ConfirmUrl, app.ConfirmUrl)) { newApp = newApp with { ConfirmUrl = ConfirmUrl.Trim() }; } if (app.ApiKeys.Count == 0) { var apiKeyGenerator = serviceProvider.GetRequiredService <IApiKeyGenerator>(); newApp = newApp with { ApiKeys = new Dictionary <string, string> { [await apiKeyGenerator.GenerateAppTokenAsync(app.Id)] = NotifoRoles.AppAdmin, [await apiKeyGenerator.GenerateAppTokenAsync(app.Id)] = NotifoRoles.AppAdmin, [await apiKeyGenerator.GenerateAppTokenAsync(app.Id)] = NotifoRoles.AppWebManager, [await apiKeyGenerator.GenerateAppTokenAsync(app.Id)] = NotifoRoles.AppWebManager }.ToReadonlyDictionary() }; } if (app.Contributors.Count == 0 && !string.IsNullOrWhiteSpace(UserId)) { newApp = newApp with { Contributors = new Dictionary <string, string> { [UserId] = NotifoRoles.AppOwner }.ToReadonlyDictionary() }; } return(newApp); } } }
public async ValueTask <User?> ExecuteAsync(User user, IServiceProvider serviceProvider, CancellationToken ct) { Validate <Validator> .It(this); var newUser = user; if (Is.Changed(FullName, user.FullName)) { newUser = newUser with { FullName = FullName.Trim() }; } if (Is.Changed(EmailAddress, user.EmailAddress)) { newUser = newUser with { EmailAddress = EmailAddress.Trim().ToLowerInvariant() }; } if (Is.Changed(PhoneNumber, user.PhoneNumber)) { newUser = newUser with { PhoneNumber = PhoneNumber.Trim().ToLowerInvariant() }; } if (Is.Changed(Properties, user.Properties)) { newUser = newUser with { Properties = Properties }; } if (Is.Changed(PreferredLanguage, user.PreferredLanguage)) { newUser = newUser with { PreferredLanguage = PreferredLanguage.Trim() }; } if (Is.Changed(PreferredTimezone, user.PreferredTimezone)) { newUser = newUser with { PreferredTimezone = PreferredTimezone.Trim() }; } if (Is.Changed(RequiresWhitelistedTopics, user.RequiresWhitelistedTopics)) { newUser = newUser with { RequiresWhitelistedTopics = RequiresWhitelistedTopics.Value }; } if (Settings != null) { var newSettings = Settings; if (MergeSettings) { newSettings = ChannelSettings.Merged(user.Settings, Settings); } newUser = newUser with { Settings = newSettings }; } if (string.IsNullOrWhiteSpace(user.ApiKey)) { var tokenGenerator = serviceProvider.GetRequiredService <IApiKeyGenerator>(); newUser = newUser with { ApiKey = await tokenGenerator.GenerateUserTokenAsync(user.AppId, user.Id) }; } return(newUser); } } }
public async ValueTask <App?> ExecuteAsync(App app, IServiceProvider serviceProvider, CancellationToken ct) { var integrationManager = serviceProvider.GetRequiredService <IIntegrationManager>(); ConfiguredIntegration configured; if (app.Integrations.TryGetValue(Id, out var previousIntegration)) { Validate <UpdateValidator> .It(this); configured = previousIntegration with { Properties = Properties }; } else { Validate <CreateValidator> .It(this); configured = new ConfiguredIntegration(Type, Properties); } if (Is.Changed(Test, configured.Test)) { configured = configured with { Test = Test }; } if (Is.Changed(Enabled, configured.Enabled)) { configured = configured with { Enabled = Enabled.Value }; } if (Is.Changed(Priority, configured.Priority)) { configured = configured with { Priority = Priority.Value }; } if (Is.Changed(Condition, configured.Condition)) { configured = configured with { Condition = Condition }; } await integrationManager.ValidateAsync(configured, ct); await integrationManager.HandleConfiguredAsync(Id, app, configured, previousIntegration, ct); var newIntegrations = new Dictionary <string, ConfiguredIntegration>(app.Integrations) { [Id] = configured }; var newApp = app with { Integrations = newIntegrations.ToReadonlyDictionary() }; return(newApp); } } }