From ed1cf5e0730a1b4aa9d9d11e07db1d244ff25219 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 11 Nov 2026 18:06:36 +0330 Subject: [PATCH] add Push Notification Related to XBotUser entitiy, and some missing Providers and also Registration in DI Extensions ... --- Configuration/XBotServiceConfiguration.cs | 9 + DI/XDIHelperExtension.cs | 225 ++++++++++++++++-- Interfaces/IXBotProvider.cs | 11 + .../Entities/XBotUserRepositoryProvider.cs | 25 ++ Providers/XBotProvider.cs | 9 + 5 files changed, 258 insertions(+), 21 deletions(-) create mode 100644 Configuration/XBotServiceConfiguration.cs create mode 100644 Interfaces/IXBotProvider.cs create mode 100644 Providers/Entities/XBotUserRepositoryProvider.cs create mode 100644 Providers/XBotProvider.cs diff --git a/Configuration/XBotServiceConfiguration.cs b/Configuration/XBotServiceConfiguration.cs new file mode 100644 index 0000000..f7ad1e5 --- /dev/null +++ b/Configuration/XBotServiceConfiguration.cs @@ -0,0 +1,9 @@ +namespace xBotService.Configuration +{ + public class XBotServiceConfiguration + { + public string Token { get; set; } + public string BaseRoute { get; set; } + public string SendAlertRoute { get; set; } + } +} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 174acd5..21a5a97 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -1,70 +1,229 @@ using System; using xDataService.Db; using xDataService.DI; +using xPushService.DI; using xCommons.Helpers; using xCommons.Extensions; using xDataService.Models; +using xPushService.Helpers; using xExceptions.Constants; using xBotService.Constants; +using xBotService.Providers; using xDataService.Constants; +using xBotService.Interfaces; using XBotUserService.Helpers; using xHttpService.Interfaces; +using xBotService.Configuration; using xBotService.Providers.Dtos; +using xBotService.Interfaces.Dtos; using Microsoft.AspNetCore.Builder; +using xBotService.Providers.Entities; +using xBotService.Interfaces.Entities; +using Microsoft.Extensions.Configuration; using xBotService.Interfaces.Entities.Dtos; using Microsoft.Extensions.DependencyInjection; +using ConfigurationNodeNames = xBotService.Constants.ConfigurationNodeNames; namespace xBotService.DI { public static class XDIHelperExtension { /// - /// Register XBotService + /// Retrieve Bot Service Configuration ... /// + /// + /// + public static XBotServiceConfiguration GetXBotServiceConfiguration( + this IConfiguration source + ) + { + // + var section = source + .GetSection(ConfigurationNodeNames.BOT_SERVICE_NODE); + + // + var result = section.Get(); + if (result.IsNull()) + { + result = new(); + } + + // + return result; + } + + /// + /// Extract Service Configurations and Register it ... + /// + /// + /// + public static void AddXBotServiceConfiguration( + this IServiceCollection source, + IConfiguration configuration + ) + { + // + var config = configuration.GetXBotServiceConfiguration(); + source.AddXBotServiceConfiguration(config); + } + + /// + /// Register Service Configuration ... + /// + /// + /// + public static void AddXBotServiceConfiguration( + this IServiceCollection source, + XBotServiceConfiguration configuration + ) + { + // + if (configuration.IsNullOrDefault()) + { + // + Log("Service Registration faild, Invalid Configuration ..."); + XException.InvalidConfiguration.Throw(); + } + + // + source.AddSingleton(configuration); + Log("Service Configuration, Registered ..."); + } + + /// + /// Register Service ... + /// + /// + /// + /// + /// public static void AddXBotService( this IServiceCollection services, + IConfiguration configuration, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) { + // + var config = configuration.GetXBotServiceConfiguration(); + + // AddBotService( contextType: null, services: services, lifeTime: lifeTime, + configuration: config, repositoryType: repositoryType ); } /// - /// Register XBotService with Context + /// Register Service with Context ... /// + /// + /// + /// + /// + /// public static void AddXBotService( this IServiceCollection services, + IConfiguration configuration, XRepositoryType repositoryType, ServiceLifetime lifeTime = ServiceLifetime.Scoped ) where TContext : XDbContext { + // + var config = configuration.GetXBotServiceConfiguration(); + + // AddBotService( services: services, lifeTime: lifeTime, + configuration: config, contextType: typeof(TContext), repositoryType: repositoryType ); } /// - /// Use XBotService Middleware + /// Register Service ... /// + /// + /// + /// + /// + public static void AddXBotService( + this IServiceCollection services, + XBotServiceConfiguration configuration, + XRepositoryType repositoryType, + ServiceLifetime lifeTime = ServiceLifetime.Scoped + ) + { + // + AddBotService( + contextType: null, + services: services, + lifeTime: lifeTime, + configuration: configuration, + repositoryType: repositoryType + ); + } + + /// + /// Register Service with Context ... + /// + /// + /// + /// + /// + /// + public static void AddXBotService( + this IServiceCollection services, + XBotServiceConfiguration configuration, + XRepositoryType repositoryType, + ServiceLifetime lifeTime = ServiceLifetime.Scoped + ) where TContext : XDbContext + { + // + AddBotService( + services: services, + lifeTime: lifeTime, + configuration: configuration, + contextType: typeof(TContext), + repositoryType: repositoryType + ); + } + + /// + /// Use XBotService Middleware ... + /// + /// + /// public static void UseXBotService( this IApplicationBuilder app, bool isDevelopmentEnvironment = false ) { - // Using XBot Repository Descriptor - if (!descriptor.IsNull()) + // + #region Using XTag Hubs ... + // + var pushHelper = new XPushServiceHelper(); + + // + pushHelper.AddHub(XBotServiceConstants.XBotUserDtoHub); + pushHelper.AddHub(XBotServiceConstants.XBotUserEntityHub); + + // + app.UseXPushService(pushHelper); + #endregion + + // + // Using XBotUser Repository Descriptor ... + if (!botUserDescriptor.IsNull()) { + // app.UseXRepository( - descriptor: descriptor, + descriptor: botUserDescriptor, isDevelopmentEnvironment: isDevelopmentEnvironment ); } @@ -72,7 +231,7 @@ namespace xBotService.DI // #region Private ... - private static XRepositoryDescriptor descriptor = null; + private static XRepositoryDescriptor botUserDescriptor = null; private static string XLogTag = XBotServiceConstants.XBotServiceDILogTag; private static void Log(string message) @@ -82,6 +241,7 @@ namespace xBotService.DI private static void AddBotService( IServiceCollection services, + XBotServiceConfiguration configuration, XRepositoryType repositoryType, Type contextType = null, ServiceLifetime lifeTime = ServiceLifetime.Scoped @@ -109,6 +269,10 @@ namespace xBotService.DI throw exception; } + // + // Validate Configuration ... + services.AddXBotServiceConfiguration(configuration); + // // Validate Repository Type var isRepositoryTypeValid = @@ -116,43 +280,62 @@ namespace xBotService.DI ((repositoryType == XRepositoryType.Mongo || repositoryType == XRepositoryType.InMemory) && contextType.IsNull()); if (!isRepositoryTypeValid) { + // Log("Service Registration failed, Invalid Repository Type and Context Type ..."); throw exception; } // - // Make Instance of Repository Descriptor + // Make Instance of Repository Descriptor ... switch (repositoryType) { + // case XRepositoryType.EF: - descriptor = typeof(XBotServiceHelper) + botUserDescriptor = typeof(XBotServiceHelper) .InvokeGenericMethod( args: null, runtimeType: contextType, methodName: XBotServiceConstants.GetXBotUserEFRepositoryDescriptor ); break; + + // case XRepositoryType.Mongo: - // descriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor(); - Log("MongoDB support not implemented yet for XBotService."); + botUserDescriptor = XBotServiceHelper.GetXBotUserMongoRepositoryDescriptor(); break; + + // case XRepositoryType.InMemory: - // descriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor(); - Log("InMemory support not implemented yet for XBotService."); + botUserDescriptor = XBotServiceHelper.GetXBotUserInMemoryRepositoryDescriptor(); break; } // - // Register XBotUser Repository Descriptor - if (!descriptor.IsNull()) - { - services.AddXRepository(lifeTime: lifeTime, descriptor: descriptor); - } + // Register XBotUser Repository Descriptor ... + services.AddXRepository( + lifeTime: lifeTime, + descriptor: botUserDescriptor + ); // - // Register Dto Services - services.Add(new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime)); - Log("XBotService Registration Succeed ..."); + // Register Dto Services ... + services.Add( + new ServiceDescriptor(typeof(IXBotUserServiceProvider), typeof(XBotUserServiceProvider), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXBotUserRepositoryService), typeof(XBotUserRepositoryService), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXBotUserRepositoryProvider), typeof(XBotUserRepositoryProvider), lifeTime) + ); + + // + services.Add( + new ServiceDescriptor(typeof(IXBotProvider), typeof(XBotProvider), lifeTime) + ); + + // + Log("Service Registration Succeed ..."); } #endregion } diff --git a/Interfaces/IXBotProvider.cs b/Interfaces/IXBotProvider.cs new file mode 100644 index 0000000..94e908c --- /dev/null +++ b/Interfaces/IXBotProvider.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace xBotService.Interfaces +{ + public interface IXBotProvider + { + + } +} \ No newline at end of file diff --git a/Providers/Entities/XBotUserRepositoryProvider.cs b/Providers/Entities/XBotUserRepositoryProvider.cs new file mode 100644 index 0000000..2437e90 --- /dev/null +++ b/Providers/Entities/XBotUserRepositoryProvider.cs @@ -0,0 +1,25 @@ +using xPushService.Base; +using xDataService.Configuration; +using xBotService.Models.Entities; +using xIdentityService.Interfaces; +using Microsoft.AspNetCore.SignalR; +using xBotService.Interfaces.Entities; + +namespace xBotService.Providers.Entities +{ + public class XBotUserRepositoryProvider : XBaseEntityProvider, IXBotUserRepositoryProvider + { + public XBotUserRepositoryProvider( + IHubContext hub, + IXBotUserRepository repository, + XDataServiceConfiguration dataConfiguration, + IXIdentityProvider identityProvider = null + ) : base( + hub, + repository, + dataConfiguration, + identityProvider + ) + { } + } +} \ No newline at end of file diff --git a/Providers/XBotProvider.cs b/Providers/XBotProvider.cs new file mode 100644 index 0000000..585b2ba --- /dev/null +++ b/Providers/XBotProvider.cs @@ -0,0 +1,9 @@ +using xBotService.Interfaces; + +namespace xBotService.Providers +{ + public class XBotProvider : IXBotProvider + { + + } +} \ No newline at end of file