using System; using System.Linq; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using xCommons.Configurations; using xCommons.Extensions; using xExceptions.Constants; using xPushService.DI; using xPushService.Helpers; using xStringService.Interfaces.Dtos; using xTermsAndConditionsService.Configurations; using xTermsAndConditionsService.Constants; using xTermsAndConditionsService.Hubs; using xTermsAndConditionsService.Interfaces; using xTermsAndConditionsService.Providers; namespace xTermsAndConditionsService.DI { public static class XDIHelperExtension { /// /// Retrieve xTermsAndConditionsService Configuration from AppSettings ... /// /// /// public static XTermsAndConditionsServiceConfiguration GetXTermsAndConditionsServiceConfiguration( this IConfiguration configuration ) { // var section = configuration.GetSection(ConfigurationNodeNames.TERMS_AND_CONDITIONS_SERVICE_NODE); var result = section.Get(); // return result; } /// /// Extract xTermsAndConditionsService Configurations from AppSettings /// and Register it in DI ... /// /// /// public static void AddXTermsAndConditionsConfiguration( this IServiceCollection services, IConfiguration configuration ) { // var serviceConfiguration = configuration.GetXTermsAndConditionsServiceConfiguration(); services.AddXTermsAndConditionsConfiguration(serviceConfiguration); } /// /// Register xTermsAndConditionsService Configurations in DI ... /// /// /// public static void AddXTermsAndConditionsConfiguration( this IServiceCollection services, XTermsAndConditionsServiceConfiguration configuration ) { // if (configuration.IsNull()) { Log($"there is no provided configurations, using default ..."); configuration = new XTermsAndConditionsServiceConfiguration(); } // services.AddSingleton(configuration); } /// /// Register TermsConditions Provider ... /// /// /// provided AppSettings Configuration /// public static void AddXTermsConditionsService( this IServiceCollection services, IConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Transient ) { // AddTermsAndConditionsService( services: services, lifeTime: lifeTime, configuration: configuration ); } /// /// Register TermsConditions Provider ... /// /// /// provided xTermsAndConditionsService Configuration /// public static void AddXTermsConditionsService( this IServiceCollection services, XTermsAndConditionsServiceConfiguration configuration, ServiceLifetime lifeTime = ServiceLifetime.Transient ) { // AddTermsAndConditionsService( services: services, lifeTime: lifeTime, serviceConfiguration: configuration ); } public static void UseXTermsAndConditionsService( this IApplicationBuilder app ) { // // Create an Scope for Services Acess ... using (var scope = app.ApplicationServices.CreateScope()) { // // Inject Requirements ... var stringProvider = scope.ServiceProvider.GetService(); var serviceConfiguration = scope.ServiceProvider.GetService(); // // Using XTerms Hub ... var pushHelper = new XPushServiceHelper(); pushHelper.AddHub("terms"); app.UseXPushService(pushHelper); // // Validate Requirements Exists ... var isValid = !stringProvider.IsNull() && !serviceConfiguration.IsNull() && !serviceConfiguration.TermsAndConditions.IsNull() && serviceConfiguration.TermsAndConditions.HasChild() && serviceConfiguration.SeedingMode != xDataService.Constants.XDbSeedingMode.None; if (isValid) { // // Prepare Default Terms ... serviceConfiguration .TermsAndConditions .ToList() .ForEach(t => t.ResourceTitle = serviceConfiguration.ResourceTitle); // // Loop through Terms and Conditions for Seeding ... foreach (var item in serviceConfiguration.TermsAndConditions) { // // Detect Exists Item ... var exists = stringProvider.FindOneAsync( predicate: x => x.Language == item.Language && x.ResourceTitle == item.ResourceTitle ) .GetAwaiter() .GetResult(); // var isExists = !exists.IsNullOrDefault(); // switch (serviceConfiguration.SeedingMode) { // case xDataService.Constants.XDbSeedingMode.AddOrUpdate: // // Update Exists by new Provided ... exists.TranslatedValue = item.TranslatedValue; exists = stringProvider.UpdateAsync( id: exists.Id, item: exists, saveChanges: true ) .GetAwaiter() .GetResult(); // Log($"Refresh Terms and Conditions of {exists.Language}"); break; // case xDataService.Constants.XDbSeedingMode.AddIfNotExists: // if (!isExists) { // exists = stringProvider.AddAsync( item: item, saveChanges: true ) .GetAwaiter() .GetResult(); // Log($"Add Terms and Conditions for {exists.Language}"); } break; } } } } } // #region Private ... /// /// a LogTag for Service ... /// private static string XLogTag = "XTermsAndConditionsService"; /// /// print a log in Console ... /// /// private static void Log(string message) { Console.WriteLine($"{XLogTag} => {message}"); } /// /// Add Terms and Conditions Provider Service ... /// /// /// private static void AddTermsAndConditionsService( IServiceCollection services, IConfiguration configuration = null, XTermsAndConditionsServiceConfiguration serviceConfiguration = null, ServiceLifetime lifeTime = ServiceLifetime.Transient ) { // var exception = XException.InvalidArgs.ToException(); ; // // Services ... var isServicesExists = !services.IsNull(); if (!isServicesExists) { // Log("AddTermsAndConditions failed, services not provided ..."); throw exception; } // // Validate Configuration ... var isConfigValid = !configuration.IsNull() || !serviceConfiguration.IsNull(); if (!isConfigValid) { // Log("AddTermsAndConditions failed, configuration not provided ..."); throw exception; } // // Lifetime ... var isLifetimeExists = !lifeTime.IsNull(); if (!isLifetimeExists) { // Log("AddTermsAndConditions failed, lifetime not provided ..."); throw exception; } // // Check Dependencies Exists ... var isDependenciesPassed = !services .GetRegisteredService() .IsNull() && !services.GetRegisteredService() .IsNull(); if (!isDependenciesPassed) { // Log("AddTermsAndConditions failed due Dependencies Issues, IXStringServiceProvider or XAppConfiguration not provided ..."); throw exception; } // // Service Configuration Handler ... if (serviceConfiguration.IsNull()) { serviceConfiguration = configuration.GetXTermsAndConditionsServiceConfiguration(); } services.AddXTermsAndConditionsConfiguration(serviceConfiguration); // // Register Resource Providers ... services.Add(new ServiceDescriptor(typeof(IXTermsAndConditionsProvider), typeof(XTermsAndConditionsProvider), lifeTime)); } #endregion } }