commit 227b0e9740114ec2e81b255389f4388025aac06f Author: Hadi Khazaee Asl Date: Sun Mar 22 14:06:31 2026 +0330 Initial ... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d4a6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +obj \ No newline at end of file diff --git a/Base/IXBaseService.cs b/Base/IXBaseService.cs new file mode 100644 index 0000000..5f0ac40 --- /dev/null +++ b/Base/IXBaseService.cs @@ -0,0 +1,5 @@ +namespace xServices.Base +{ + public interface IXBaseService + { } +} \ No newline at end of file diff --git a/Base/XBaseService.cs b/Base/XBaseService.cs new file mode 100644 index 0000000..4206b49 --- /dev/null +++ b/Base/XBaseService.cs @@ -0,0 +1,5 @@ +namespace xServices.Base +{ + public class XBaseService : IXBaseService + { } +} \ No newline at end of file diff --git a/Configurations/.gitkeep b/Configurations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Configurations/XServicesConfiguration.cs b/Configurations/XServicesConfiguration.cs new file mode 100644 index 0000000..36301d4 --- /dev/null +++ b/Configurations/XServicesConfiguration.cs @@ -0,0 +1,5 @@ +namespace xServices.Configurations +{ + public partial class XServicesConfiguration + { } +} \ No newline at end of file diff --git a/Constants/ConfigurationNodeNames.cs b/Constants/ConfigurationNodeNames.cs new file mode 100644 index 0000000..0adf7a7 --- /dev/null +++ b/Constants/ConfigurationNodeNames.cs @@ -0,0 +1,7 @@ +namespace xServices.Constants +{ + public struct ConfigurationNodeNames + { + public const string SERVICES_NODE = "ServicesConfiguration"; + } +} \ No newline at end of file diff --git a/Constants/xServicesConstancts.cs b/Constants/xServicesConstancts.cs new file mode 100644 index 0000000..2f09f2f --- /dev/null +++ b/Constants/xServicesConstancts.cs @@ -0,0 +1,23 @@ +using xExceptions.Attributes; + +namespace xServices.Constants +{ + public class xServicesConstancts + { + } + + public enum XQueryServiceEndPoints + { + /// + /// Specified Endpoint for Users Fetching as Query ... + /// + [StringValue("Account/Users/Query")] + QueryUsers, + + /// + /// Refresh Tokens ... + /// + [StringValue("Account/RefreshTokens")] + RefreshTokens, + } +} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs new file mode 100644 index 0000000..3295b78 --- /dev/null +++ b/DI/XDIHelperExtension.cs @@ -0,0 +1,182 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using xCommons.Extensions; +using xFileService.DI; +using xMessageService.DI; +using xServices.Configurations; +using xServices.Constants; +using xServices.TermsConditions.DI; +using xStorageService.DI; +using xStringService.DI; +using xTagService.DI; + +namespace xServices.DI +{ + public static class XDIHelperExtension + { + /// + /// Retrive Services Configurations ... + /// + /// + /// + public static XServicesConfiguration GetXServicesConfiguration( + this IConfiguration source + ) + { + // + var xConfig = source + .GetSection(ConfigurationNodeNames.SERVICES_NODE); + var result = xConfig.Get(); + + // + return result; + } + + /// + /// Register Service Configuration ... + /// + /// + /// + public static void AddXServicesConfiguration( + this IServiceCollection services, + IConfiguration configuration + ) + { + // + var config = configuration + .GetXServicesConfiguration(); + if (config.IsNull()) + { + config = new XServicesConfiguration(); + } + + // + services.AddSingleton(config); + } + + /// + /// Register Service Configuration ... + /// + /// + /// + public static void AddXServicesConfiguration( + this IServiceCollection services, + XServicesConfiguration configuration + ) + { + // + if (configuration.IsNull()) + { + configuration = new XServicesConfiguration(); + } + + // + services.AddSingleton(configuration); + } + + /// + /// Register Services ... + /// + /// + /// + public static void AddXServices( + this IServiceCollection services, + IConfiguration configuration, + ServiceLifetime lifeTime = ServiceLifetime.Transient + ) + { + // + var config = configuration.GetXServicesConfiguration(); + + // + AddServices( + config: config, + services: services, + lifeTime: lifeTime, + configuration: configuration + ); + } + + /// + /// Register Services ... + /// + /// + /// + /// + public static void AddXServices( + this IServiceCollection services, + IConfiguration configuration, + XServicesConfiguration config, + ServiceLifetime lifeTime = ServiceLifetime.Transient + ) + { + // + AddServices( + config: config, + services: services, + lifeTime: lifeTime, + configuration: configuration + ); + } + + /// + /// Use All Required Middlewares ... + /// + /// + public static void UseXServices(this IApplicationBuilder app) + { + // + // Use Storage Service ... + app.UseXStorageService(); + } + + // + #region Private ... + /// + /// Register Required Services ... + /// + /// + /// + /// + private static void AddServices( + this IServiceCollection services, + IConfiguration configuration, + XServicesConfiguration config = null, + ServiceLifetime lifeTime = ServiceLifetime.Transient + ) + { + // + // Check config ... + if (config.IsNull()) + { + config = new XServicesConfiguration(); + } + + // + // Tag Service ... + services.AddXTagService(lifeTime: lifeTime); + + // + // Storage Service ... + services.AddXStorageService(configuration); + + // + // File Service ... + services.AddXFileService(lifeTime: lifeTime); + + // + // Message Service ... + services.AddXMessageService(config: configuration); + + // + // String Servicce ... + services.AddXStringService(lifeTime: lifeTime); + + // + // Terms and Conditions Service ... + services.AddXTermsConditionsService(lifeTime: lifeTime); + } + #endregion + } +} \ No newline at end of file diff --git a/Extensions/xServicesExtensions.cs b/Extensions/xServicesExtensions.cs new file mode 100644 index 0000000..d0ea550 --- /dev/null +++ b/Extensions/xServicesExtensions.cs @@ -0,0 +1,5 @@ +namespace xServices.Extensions +{ + public static class xServicesExtensions + { } +} \ No newline at end of file diff --git a/Helpers/.gitkeep b/Helpers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Interfaces/.gitkeep b/Interfaces/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Models/.gitkeep b/Models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Models/XGroupedData.cs b/Models/XGroupedData.cs new file mode 100644 index 0000000..fda9f6c --- /dev/null +++ b/Models/XGroupedData.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using xModels.Base; + +namespace xServices.Models +{ + public class XGroupedDataEntity + where T : XBaseEntity + { + public TGroupedKey GroupedBy { set; get; } + public IEnumerable Members { set; get; } + public DateTime TimeStamp { set; get; } + } + + public class XGroupedDataDto + where T : XBaseDto + { + public TGroupedKey GroupedBy { set; get; } + public IEnumerable Members { set; get; } + public DateTime TimeStamp { set; get; } + } +} \ No newline at end of file diff --git a/Providers/.gitkeep b/Providers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a06494 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# xSaherelmAI.xServices + +it is a Part of xSaherElm AI project which integrated and implements all Required Services ... + +## Maintainer + +Hadi Khazaee asl + +[https://www.saherelm.ir](https://www.saherelm.ir) + +[hadi_khazaee_asl@yahoo.com](mailto:hadi_khazaee_asl@yahoo.com) diff --git a/TermsConditions/Controllers/XTermsConditionsControllerActions.cs b/TermsConditions/Controllers/XTermsConditionsControllerActions.cs new file mode 100644 index 0000000..c5d0327 --- /dev/null +++ b/TermsConditions/Controllers/XTermsConditionsControllerActions.cs @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; +using xServices.TermsConditions.Interfaces; + +namespace xServices.TermsConditions.Controllers +{ + /// + /// a Controller base Class which implement based Actions to Provides Terms and Conditions Provider Access ... + /// + public abstract class XTermsConditionsControllerActions : XIBaseProviderController, IXTermsConditionsControllerActions + { + // + #region Props ... + public IXTermsComditionsProvider TermsProvider { get; } + #endregion + + // + #region Constructor ... + public XTermsConditionsControllerActions( + ILogger logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXTermsComditionsProvider termsProvider + ) : base( + logger, + appConfiguration, + identityProvider, + validationProvider + ) + { + // + TermsProvider = termsProvider; + } + #endregion + + // + #region Actions ... + /// + /// Retrieved all Exists Languages Terms and Conditions ... + /// + /// + [HttpGet("Terms/Languages")] + public async Task>> TermsLanguages() + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.TermsLanguages(); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Check Has Terms and Conditions based on Specified Language ... + /// + /// if null, used Default Language ... + /// + [HttpGet("Terms/{language}/Has")] + public async Task> HasTerms( + [FromRoute] string language = null + ) + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.HasTerms( + language: language + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Retrieve Terms and Conditions for Specified Language ... + /// + /// if null, used Default Language ... + /// + [HttpGet("Terms/{language}")] + public async Task> GetTerms( + [FromRoute] string language = null + ) + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.GetTerms( + language: language + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Add Terms and Conditions for Specified Language ... + /// + /// + /// + /// + [HttpPost("Terms/{language}")] + public async Task> AddTerms( + [FromRoute] string language, + [FromQuery] string terms + ) + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.AddTerms( + language: language, + terms: terms, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Remove Terms and Conditions of Specified Language ... + /// + /// + /// + [HttpDelete("Terms/{language}")] + public async Task> RemoveTerms( + [FromRoute] string language + ) + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.RemoveTerms( + language: language, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Update Terms and Conditions of Specified Language ... + /// + /// + /// + /// + [HttpPut("Terms/{language}")] + public async Task> UpdateTerms( + [FromRoute] string language, + [FromQuery] string terms + ) + { + // + // Do ... + try + { + // + // Retrieve User Info ... + var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); + + // + var result = await TermsProvider.UpdateTerms( + language: language, + terms: terms, + userInfo: userInfo, + connectionId: connectionId + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region NonActions ... + #endregion + } +} \ No newline at end of file diff --git a/TermsConditions/DI/TermsConditionsExtensions.cs b/TermsConditions/DI/TermsConditionsExtensions.cs new file mode 100644 index 0000000..3a463bc --- /dev/null +++ b/TermsConditions/DI/TermsConditionsExtensions.cs @@ -0,0 +1,100 @@ +using System; +using Microsoft.Extensions.DependencyInjection; +using xCommons.Configurations; +using xCommons.Extensions; +using xExceptions.Constants; +using xServices.TermsConditions.Interfaces; +using xServices.TermsConditions.Provides; +using xStringService.Interfaces.Entities; + +namespace xServices.TermsConditions.DI +{ + public static class TermsConditionsExtensions + { + /// + /// Register TermsConditions Provider ... + /// + /// + /// + public static void AddXTermsConditionsService( + this IServiceCollection services, + ServiceLifetime lifeTime = ServiceLifetime.Transient + ) + { + // + AddTermsConditionsService( + services: services, + lifeTime: lifeTime + ); + } + + // + #region Private ... + /// + /// a LogTag for Service ... + /// + private static string XLogTag = "XTermsConditionsService"; + + /// + /// print a log in Console ... + /// + /// + private static void Log(string message) + { + Console.WriteLine($"{XLogTag} => {message}"); + } + + /// + /// Add Terms and Conditions Provider Service ... + /// + /// + /// + private static void AddTermsConditionsService( + IServiceCollection services, + ServiceLifetime lifeTime = ServiceLifetime.Transient + ) + { + // + var exception = XException.InvalidArgs.ToException(); ; + + // + // Services ... + var isServicesExists = !services.IsNull(); + if (!isServicesExists) + { + // + Log("AddTermsConditions failed, services not provided ..."); + throw exception; + } + + // + // Lifetime ... + var isLifetimeExists = !lifeTime.IsNull(); + if (!isLifetimeExists) + { + // + Log("AddTermsConditions failed, lifetime not provided ..."); + throw exception; + } + + // + // Check Dependencies Exists ... + var isDependenciesPassed = !services + .GetRegisteredService() + .IsNull() && + !services.GetRegisteredService() + .IsNull(); + if (!isDependenciesPassed) + { + // + Log("AddTermsConditions failed due Dependencies Issues, IXStringRepository or XAppConfiguration not provided ..."); + throw exception; + } + + // + // Register Resource Providers ... + services.Add(new ServiceDescriptor(typeof(IXTermsComditionsProvider), typeof(XTermsComditionsProvider), lifeTime)); + } + #endregion + } +} \ No newline at end of file diff --git a/TermsConditions/Interfaces/IXTermsComditionsProvider.cs b/TermsConditions/Interfaces/IXTermsComditionsProvider.cs new file mode 100644 index 0000000..f2d6735 --- /dev/null +++ b/TermsConditions/Interfaces/IXTermsComditionsProvider.cs @@ -0,0 +1,76 @@ +using System.Collections; +using System.Collections.Generic; +using System.Threading.Tasks; +using xIdentityModels.Models; + +namespace xServices.TermsConditions.Interfaces +{ + public interface IXTermsComditionsProvider + { + // + #region Actions ... + /// + /// Retrieved all Exists Languages Terms and Conditions ... + /// + /// + Task> TermsLanguages(); + + /// + /// Check Has Terms and Conditions based on Specified Language ... + /// + /// if null, used Default Language ... + /// + Task HasTerms(string language = null); + + /// + /// Retrieve Terms and Conditions for Specified Language ... + /// + /// if null, used Default Language ... + /// + Task GetTerms(string language = null); + + /// + /// Add Terms and Conditions for Specified Language ... + /// + /// + /// + /// + /// + /// + Task AddTerms( + string language, + string terms, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ); + + /// + /// Remove Terms and Conditions of Specified Language ... + /// + /// + /// + /// + /// + Task RemoveTerms( + string language, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ); + + /// + /// Update Terms and Conditions of Specified Language ... + /// + /// + /// + /// + /// + /// + Task UpdateTerms( + string language, + string terms, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ); + #endregion + } +} \ No newline at end of file diff --git a/TermsConditions/Interfaces/IXTermsConditionsControllerActions.cs b/TermsConditions/Interfaces/IXTermsConditionsControllerActions.cs new file mode 100644 index 0000000..d2ec9c7 --- /dev/null +++ b/TermsConditions/Interfaces/IXTermsConditionsControllerActions.cs @@ -0,0 +1,69 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace xServices.TermsConditions.Interfaces +{ + public interface IXTermsConditionsControllerActions + { + /// + /// Terms and Conditions Provider ... + /// + IXTermsComditionsProvider TermsProvider { get; } + + /// + /// Retrieved all Exists Languages Terms and Conditions ... + /// + /// + Task>> TermsLanguages(); + + /// + /// Check Has Terms and Conditions based on Specified Language ... + /// + /// if null, used Default Language ... + /// + Task> HasTerms( + [FromRoute] string language = null + ); + + /// + /// Retrieve Terms and Conditions for Specified Language ... + /// + /// if null, used Default Language ... + /// + Task> GetTerms( + [FromRoute] string language = null + ); + + /// + /// Add Terms and Conditions for Specified Language ... + /// + /// + /// + /// + Task> AddTerms( + [FromRoute] string language, + [FromQuery] string terms + ); + + /// + /// Remove Terms and Conditions of Specified Language ... + /// + /// + /// + Task> RemoveTerms( + [FromRoute] string language + ); + + /// + /// Update Terms and Conditions of Specified Language ... + /// + /// + /// + /// + Task> UpdateTerms( + [FromRoute] string language, + [FromQuery] string terms + ); + } +} \ No newline at end of file diff --git a/TermsConditions/Provides/XTermsComditionsProvider.cs b/TermsConditions/Provides/XTermsComditionsProvider.cs new file mode 100644 index 0000000..cf6f937 --- /dev/null +++ b/TermsConditions/Provides/XTermsComditionsProvider.cs @@ -0,0 +1,411 @@ +using xStringService.Interfaces.Entities; +using xServices.TermsConditions.Interfaces; +using System.Threading.Tasks; +using xCommons.Extensions; +using xCommons.Configurations; +using xStringService.Models.Entities; +using xExceptions.Constants; +using xIdentityModels.Models; +using System.Linq; +using Microsoft.AspNetCore.SignalR; +using xServices.TermsConditions.Push; +using System.Collections.Generic; +using xPushService.Constants; + +namespace xServices.TermsConditions.Provides +{ + public class XTermsComditionsProvider : IXTermsComditionsProvider + { + // + #region Props ... + private readonly string termsId; + private readonly IHubContext hub; + private readonly XAppConfiguration appConfiguration; + private readonly IXStringRepository stringRepository; + #endregion + + // + #region Constructor ... + public XTermsComditionsProvider( + XAppConfiguration appConfiguration, + IXStringRepository stringRepository, + IHubContext hub = null, + string termsId = "terms" + ) + { + // + this.termsId = termsId; + this.stringRepository = stringRepository; + this.hub = hub; + this.appConfiguration = appConfiguration; + } + #endregion + + // + #region Actions ... + /// + /// Retrieved all Exists Languages Terms and Conditions ... + /// + /// + public async Task> TermsLanguages() + { + // + var result = (await stringRepository + .FindManyAsync(x => x.ResourceTitle == termsId)) + .Select(x => x.Language); + return result; + } + + /// + /// Check Has Terms and Conditions based on Specified Language ... + /// + /// if null, used Default Language ... + /// + public async Task HasTerms(string language = null) + { + // + // Normalize ... + if (language.IsNullOrEmpty()) + { + language = appConfiguration.DefaultLanguage; + } + + // + var result = !(await GetString(language: language)) + .IsNullOrDefault(); + + // + return result; + } + + /// + /// Retrieve Terms and Conditions for Specified Language ... + /// + /// if null, used Default Language ... + /// + public async Task GetTerms(string language = null) + { + // + var result = string.Empty; + var entity = await GetString(language: language); + + // + if (!entity.IsNullOrDefault()) + { + result = entity.TranslatedValue; + } + + // + return result; + } + + /// + /// Add Terms and Conditions for Specified Language ... + /// + /// + /// + /// + /// + /// + public async Task AddTerms( + string language, + string terms, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ) + { + // + // Validate ... + var isValid = + !language.IsNullOrEmpty() && + !terms.IsNullOrEmpty() && + !userInfo.IsNullOrDefault() && + language != appConfiguration.DefaultLanguage; + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Check Exists ... + isValid = await HasTerms(language: language); + if (isValid) + { + XException.Duplicate.Throw(); + } + + // + // Check Permission ... + isValid = + !userInfo.IsNullOrDefault() && + userInfo.Roles.Any(r => r.ToNormalString().Contains("admin")); + if (!isValid) + { + XException.NotAllowed.Throw(); + } + + // + // Add Terms ... + XString entity = null; + try + { + // + entity = new XString + { + Language = language, + ResourceTitle = termsId, + TranslatedValue = terms, + }; + entity = await stringRepository.AddAsync(entity); + } + catch { } + + // + var result = string.Empty; + if (!entity.IsNullOrDefault()) + { + result = entity.TranslatedValue; + } + + // + if (!result.IsNullOrEmpty()) + { + // + await SendPush( + action: XBaseEntityHubAction.Add.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + } + + // + return result; + } + + /// + /// Remove Terms and Conditions of Specified Language ... + /// + /// + /// + /// + /// + /// + public async Task RemoveTerms( + string language, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ) + { + // + var isValid = + !language.IsNullOrEmpty() && + !userInfo.IsNullOrDefault() && + language != appConfiguration.DefaultLanguage; + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Check Exists ... + XString entity = await GetString(language); + isValid = !entity.IsNullOrDefault(); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + // Check Permission ... + isValid = + !userInfo.IsNullOrDefault() && + userInfo.Roles.Any(r => r.ToNormalString().Contains("admin")); + if (!isValid) + { + XException.NotAllowed.Throw(); + } + + // + try + { + entity = await stringRepository.RemoveAsync(entity.Id); + } + catch + { + entity = null; + } + + // + var result = !entity.IsNullOrDefault(); + if (result) + { + // + await SendPush( + action: XBaseEntityHubAction.Delete.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + } + + // + return result; + } + + /// + /// Update Terms and Conditions of Specified Language ... + /// + /// + /// + /// + /// + /// + public async Task UpdateTerms( + string language, + string terms, + XUserClaimsInfoDto userInfo = null, + string connectionId = null + ) + { + // + var isValid = + !terms.IsNullOrEmpty() && + !language.IsNullOrEmpty() && + !userInfo.IsNullOrDefault(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Check Exists ... + XString entity = await GetString(language); + isValid = !entity.IsNullOrDefault(); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + // Check Permission ... + isValid = + !userInfo.IsNullOrDefault() && + userInfo.Roles.Any(r => r.ToNormalString().Contains("admin")); + if (!isValid) + { + XException.NotAllowed.Throw(); + } + + // + try + { + // + entity.TranslatedValue = terms; + entity = await stringRepository.UpdateAsync(entity.Id, entity); + } + catch + { + entity = null; + } + + // + var result = !entity.IsNullOrDefault(); + if (result) + { + // + await SendPush( + action: XBaseEntityHubAction.Update.GetStringValue(), + payLoad: entity.ToJSON(camelCase: true), + connectionId: connectionId + ); + } + + // + return result; + } + #endregion + + // + #region Hub Actions ... + /// + /// Send Custom Push Message ... + /// + /// + /// + /// + /// + public async Task SendPush( + string action, + string payLoad, + string connectionId = null + ) + { + // + var actions = new List + { + XBaseEntityHubAction.Add.GetStringValue(), + XBaseEntityHubAction.Update.GetStringValue(), + XBaseEntityHubAction.Delete.GetStringValue(), + }; + + // + // Validate ... + var isValid = + !hub.IsNull() && + !action.IsNullOrEmpty() && + actions.Contains(action); + if (!isValid) + { + return; + } + + // + var clients = hub.Clients.All; + if (!connectionId.IsNullOrEmpty()) + { + clients = hub.Clients.AllExcept(connectionId); + } + try + { + await clients.SendAsync(action, payLoad, connectionId); + } + catch { } + } + #endregion + + // + #region Private ... + /// + /// Retrieve Terms and Conditions XString Entity ... + /// + /// + /// + private async Task GetString(string language = null) + { + // + // Normalize ... + if (language.IsNullOrEmpty()) + { + language = appConfiguration.DefaultLanguage; + } + + // + // Retrieve ... + XString result = null; + try + { + // + result = await stringRepository.FindOneAsync(x => + x.Language == language && + x.ResourceTitle == this.termsId + ); + } + catch + { + } + + // + return result; + } + #endregion + } +} \ No newline at end of file diff --git a/TermsConditions/Push/XTermsHub.cs b/TermsConditions/Push/XTermsHub.cs new file mode 100644 index 0000000..41e5ccc --- /dev/null +++ b/TermsConditions/Push/XTermsHub.cs @@ -0,0 +1,86 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; +using xCommons.Extensions; +using xPushService.Base; +using xPushService.Constants; +using xStringService.Models.Entities; + +namespace xServices.TermsConditions.Push +{ + public class XTermsHub : XBaseHub + { + public XTermsHub(ILogger logger) : base(logger) + { + } + + // + #region Hub Actions ... + /// + /// Add Specified Terms and Conditions ... + /// + /// + /// + public async Task Add(XString entity) + { + // + var connectionId = Context.ConnectionId; + if (!connectionId.IsNullOrEmpty()) + { + // + await Clients + .AllExcept(connectionId) + .SendAsync( + XBaseEntityHubAction.Add.GetStringValue(), + entity.ToJSON(camelCase: true), + connectionId + ); + } + } + + /// + /// Deleted Specified Terms and Conditions ... + /// + /// + /// + public async Task Delete(XString entity) + { + // + var connectionId = Context.ConnectionId; + if (!connectionId.IsNullOrEmpty()) + { + // + await Clients + .AllExcept(connectionId) + .SendAsync( + XBaseEntityHubAction.Delete.GetStringValue(), + entity.ToJSON(camelCase: true), + connectionId + ); + } + } + + /// + /// Notify an Entity Updated ... + /// + /// + /// + public async Task Update(XString entity) + { + // + var connectionId = Context.ConnectionId; + if (!connectionId.IsNullOrEmpty()) + { + // + await Clients + .AllExcept(connectionId) + .SendAsync( + XBaseEntityHubAction.Update.GetStringValue(), + entity.ToJSON(camelCase: true), + connectionId + ); + } + } + #endregion + } +} \ No newline at end of file diff --git a/nuget.config b/nuget.config new file mode 100644 index 0000000..632defe --- /dev/null +++ b/nuget.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/xServices.csproj b/xServices.csproj new file mode 100644 index 0000000..ed97684 --- /dev/null +++ b/xServices.csproj @@ -0,0 +1,35 @@ + + + + + 1.0.0 + 8.0 + xSaherelmAI.xServices + Hadi Khazaee Asl + SaherElm IT Center + netstandard2.0 + + it is a Part of xSaherElm AI project which integrated and implements all Required Services ... + + + + icon.png + + + + + + + + + + + + + + + + + + \ No newline at end of file