diff --git a/Configuration/XTermsAndConditionsServiceConfiguration.cs b/Configuration/XTermsAndConditionsServiceConfiguration.cs
new file mode 100644
index 0000000..40fd629
--- /dev/null
+++ b/Configuration/XTermsAndConditionsServiceConfiguration.cs
@@ -0,0 +1,27 @@
+using System.Collections.Generic;
+using xDataService.Constants;
+using xStringService.Models.Dtos;
+
+namespace xTermsAndConditionsService.Configuration
+{
+ ///
+ /// Terms and Conditions Configuration ...
+ ///
+ public class XTermsAndConditionsServiceConfiguration
+ {
+ ///
+ /// Terms And Conditions Resource Title in XString Table ...
+ ///
+ public string ResourceTitle { get; set; } = "terms_and_conditions";
+
+ ///
+ /// Seeding Mode for Terms and Conditions ...
+ ///
+ public XDbSeedingMode SeedingMode { get; set; } = XDbSeedingMode.AddIfNotExists;
+
+ ///
+ /// Default Terms and Conditions for Add to Databse on App Startup ...
+ ///
+ public List TermsAndConditions { get; set; } = null;
+ }
+}
\ No newline at end of file
diff --git a/Constants/ConfigurationNodeNames.cs b/Constants/ConfigurationNodeNames.cs
new file mode 100644
index 0000000..2be21c6
--- /dev/null
+++ b/Constants/ConfigurationNodeNames.cs
@@ -0,0 +1,7 @@
+namespace xTermsAndConditionsService.Constants
+{
+ public partial struct ConfigurationNodeNames
+ {
+ public const string TERMS_AND_CONDITIONS_SERVICE_NODE = "TermsAndConditionsService";
+ }
+}
\ No newline at end of file
diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs
index 6580181..4790954 100644
--- a/DI/XDIHelperExtension.cs
+++ b/DI/XDIHelperExtension.cs
@@ -1,9 +1,14 @@
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 xStringService.Interfaces.Dtos;
+using xTermsAndConditionsService.Configuration;
+using xTermsAndConditionsService.Constants;
using xTermsAndConditionsService.Interfaces;
using xTermsAndConditionsService.Providers;
@@ -11,29 +16,196 @@ 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
)
{
//
- AddTermsConditionsService(
+ AddTermsAndConditionsService(
services: services,
- lifeTime: lifeTime
+ 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();
+
+ //
+ // 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 = "XTermsConditionsService";
+ private static string XLogTag = "XTermsAndConditionsService";
///
/// print a log in Console ...
@@ -49,8 +221,10 @@ namespace xTermsAndConditionsService.DI
///
///
///
- private static void AddTermsConditionsService(
+ private static void AddTermsAndConditionsService(
IServiceCollection services,
+ IConfiguration configuration = null,
+ XTermsAndConditionsServiceConfiguration serviceConfiguration = null,
ServiceLifetime lifeTime = ServiceLifetime.Transient
)
{
@@ -63,7 +237,17 @@ namespace xTermsAndConditionsService.DI
if (!isServicesExists)
{
//
- Log("AddTermsConditions failed, services not provided ...");
+ 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;
}
@@ -73,7 +257,7 @@ namespace xTermsAndConditionsService.DI
if (!isLifetimeExists)
{
//
- Log("AddTermsConditions failed, lifetime not provided ...");
+ Log("AddTermsAndConditions failed, lifetime not provided ...");
throw exception;
}
@@ -82,15 +266,23 @@ namespace xTermsAndConditionsService.DI
var isDependenciesPassed = !services
.GetRegisteredService()
.IsNull() &&
- !services.GetRegisteredService()
+ !services.GetRegisteredService()
.IsNull();
if (!isDependenciesPassed)
{
//
- Log("AddTermsConditions failed due Dependencies Issues, IXStringRepository or XAppConfiguration not provided ...");
+ 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(IXTermsAndComditionsProvider), typeof(XTermsAndComditionsProvider), lifeTime));
diff --git a/Providers/XTermsAndComditionsProvider.cs b/Providers/XTermsAndComditionsProvider.cs
index 31b09a9..d55e2d6 100644
--- a/Providers/XTermsAndComditionsProvider.cs
+++ b/Providers/XTermsAndComditionsProvider.cs
@@ -11,6 +11,7 @@ using xIdentityModels.Models;
using xPushService.Constants;
using xStringService.Interfaces.Dtos;
using xStringService.Models.Dtos;
+using xTermsAndConditionsService.Configuration;
using xTermsAndConditionsService.Hubs;
using xTermsAndConditionsService.Interfaces;
@@ -28,6 +29,7 @@ namespace xTermsAndConditionsService.Providers
private readonly IHubContext hub;
private readonly XAppConfiguration appConfiguration;
private readonly IXStringServiceProvider stringProvider;
+ private readonly XTermsAndConditionsServiceConfiguration serviceConfiguration;
#endregion
//
@@ -35,15 +37,18 @@ namespace xTermsAndConditionsService.Providers
public XTermsAndComditionsProvider(
XAppConfiguration appConfiguration,
IXStringServiceProvider stringProvider,
- IHubContext hub = null,
- string termsId = "terms"
+ XTermsAndConditionsServiceConfiguration serviceConfiguration,
+ IHubContext hub = null
)
{
//
this.hub = hub;
- this.termsId = termsId;
this.stringProvider = stringProvider;
this.appConfiguration = appConfiguration;
+ this.serviceConfiguration = serviceConfiguration;
+
+ //
+ termsId = serviceConfiguration.ResourceTitle;
//
// Specified roles for Handling Dangerouse Actions ...