From e6e9e1b93d205fa68f8a731a9e05e2a3a567f204 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 22 May 2026 20:22:34 +0330 Subject: [PATCH] last ... --- Base/.gitkeep | 0 Base/XBaseStringResourceProvider.cs | 347 ---- Controllers/XStringServiceControllerBase.cs | 878 ---------- DI/XDIHelperExtension.cs | 8 +- DataHelper/Events/XStringEvents.cs | 2 +- Interfaces/IXBaseStringResourceProvider.cs | 98 -- Interfaces/IXStringProvider.cs | 307 ---- .../IXStringServiceControllerActions.cs | 257 --- Providers/XStringProvider.cs | 1477 ----------------- nuget.config | 3 +- 10 files changed, 6 insertions(+), 3371 deletions(-) create mode 100644 Base/.gitkeep delete mode 100644 Base/XBaseStringResourceProvider.cs delete mode 100644 Controllers/XStringServiceControllerBase.cs delete mode 100644 Interfaces/IXBaseStringResourceProvider.cs delete mode 100644 Interfaces/IXStringProvider.cs delete mode 100644 Interfaces/IXStringServiceControllerActions.cs delete mode 100644 Providers/XStringProvider.cs diff --git a/Base/.gitkeep b/Base/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Base/XBaseStringResourceProvider.cs b/Base/XBaseStringResourceProvider.cs deleted file mode 100644 index ebd94e3..0000000 --- a/Base/XBaseStringResourceProvider.cs +++ /dev/null @@ -1,347 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using xCommons.Configurations; -using xCommons.Extensions; -using xExceptions.Constants; -using xModels.Dtos; -using xStringService.Extensions; -using xStringService.Interfaces; -using xStringService.Models.Dtos; - -namespace xStringService.Base -{ - /// - /// a Base Class for Handling Resources Based on Specified Types on - /// String Resources Use ... - /// such as: - /// - Terms and Conditions; - /// - Contents; - /// - News; - /// - etc ... - /// - public abstract class XBaseStringResourceProvider : IXBaseStringResourceProvider - { - // - #region Props ... - public string Prefix { get; } - public IXStringProvider Provider { get; } - public XAppConfiguration AppConfiguration { get; } - #endregion - - // - #region Constructor ... - public XBaseStringResourceProvider( - string prefix, - IXStringProvider provider, - XAppConfiguration appConfiguration - ) - { - // - Prefix = prefix; - Provider = provider; - AppConfiguration = appConfiguration; - } - #endregion - - // - #region Actions ... - /// - /// Prepare Resource Title by Given Data ... - /// - /// - /// - public string GetResourceTitle(string suffix) - { - // - string result = ""; - - // - if (suffix.IsNullOrEmpty()) - { - return result; - } - - // - result = $"{Prefix}_${suffix}"; - - // - return result; - } - - /// - /// Add Specified Locales ... - /// - /// - /// - /// - public async Task> Add( - string suffix, - IEnumerable locales - ) - { - // - // Validate ... - var has = - locales.HasChild() && - !suffix.IsNullOrEmpty(); - if (!has) - { - XException.InvalidArgs.Throw(); - } - - // - // Prepare Resource ID ... - string resourceID = GetResourceTitle(suffix); - var result = await locales.SelectAsync(async l => - { - // - // Normalize Language ... - if (l.Language.IsNullOrEmpty()) - { - l.Language = AppConfiguration.DefaultLanguage; - } - - // - // Prepare Model for Add ... - var model = new XStringDto - { - Language = l.Language, - TranslatedValue = l.Value, - ResourceTitle = resourceID, - }; - model = await Provider.AddEntity(model); - return model; - }); - - // - return result; - } - - /// - /// Add Or Update Specified Locals ... - /// - /// - /// - /// - public async Task> AddOrUpdate( - string resource, - IEnumerable locales - ) - { - // - // Validate ... - bool isValid = - locales.HasChild() && - !resource.IsNullOrEmpty() && - locales.All(l => !l.Value.IsNullOrEmpty() && !l.Language.IsNullOrEmpty()); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Resource ... - isValid = resource.Contains(Prefix); - if (!isValid) - { - XException.NotAllowed.Throw(); - } - - // - var result = await locales.SelectAsync(async l => - await Provider.AddOrUpdateByLocaleResource( - item: l, - resource: resource - ) - ); - - // - return result; - } - - /// - /// Get Specified Locale ... - /// - /// - /// - /// - public async Task GetLocale( - string suffix, - string language = null - ) - { - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - if (suffix.IsNullOrEmpty() || language.IsNullOrEmpty()) - { - XException.InvalidArgs.Throw(); - } - - // - // Prepare Resource Title ... - string resource = GetResourceTitle(suffix); - - // - // Check Resource Exists ... - var isExists = await Provider.IsResourceExists( - resource: resource, - language: language - ); - if (!isExists) - { - XException.NotFound.Throw(); - } - - // - // Retrieve and Validate Dto Model ... - var model = await Provider.Get( - resource: resource, - language: language - ); - if (model.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - // Prepare Result ... - var result = model - .ToXLocaleResourceDto(); - return result; - } - - /// - /// Add Specified Resource ... - /// - /// - /// - /// - public async Task AddResource( - string suffix, - XResourceDto item - ) - { - // - // Validate ... - if (suffix.IsNullOrEmpty() || - item.IsNull() || - !item.Locales.HasChild() || - !item.Locales.All(l => !l.Value.IsNullOrEmpty())) - { - XException.InvalidArgs.Throw(); - } - - // - // Prepare Resource ID ... - var addedLocals = await Add(suffix, item.Locales); - if (!addedLocals.HasChild()) - { - XException.ActionFailed.Throw(); - } - - // - var result = await GetResource(suffix); - return result; - } - - /// - /// Get Resources of Specified Resource ... - /// - /// - /// - public async Task GetResource(string suffix) - { - // - // Validate ... - if (suffix.IsNullOrEmpty()) - { - XException.InvalidArgs.Throw(); - } - - // - // Prepare Resource ID ... - string resourceID = GetResourceTitle(suffix); - - // - // Prepare ... - var result = await Provider.GetResource(resourceID); - return result; - } - - /// - /// Remove Specified Suffix Resources ... - /// - /// - /// - public async Task Remove(string suffix) - { - // - // Validate ... - if (suffix.IsNullOrEmpty()) - { - XException.InvalidArgs.Throw(); - } - - // - var resourceID = GetResourceTitle(suffix); - await Provider.RemoveResource(resourceID); - } - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - public async Task Remove( - string resource, - string language - ) - { - // - // Validate ... - bool isValid = - !resource.IsNullOrEmpty() && - !language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check resource ID Contains Prefix ... - isValid = resource.Contains(Prefix); - if (!isValid) - { - XException.NotAllowed.Throw(); - } - - // - // Check Exists ... - var isExists = await Provider.IsResourceExists( - resource: resource, - language: language - ); - if (!isExists) - { - XException.NotFound.Throw(); - } - - // - // Remove Resource ... - await Provider.Remove( - resource: resource, - language: language - ); - } - #endregion - } -} \ No newline at end of file diff --git a/Controllers/XStringServiceControllerBase.cs b/Controllers/XStringServiceControllerBase.cs deleted file mode 100644 index 7d88a31..0000000 --- a/Controllers/XStringServiceControllerBase.cs +++ /dev/null @@ -1,878 +0,0 @@ -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 xModels.Dtos; -using xStringService.Interfaces; -using xStringService.Models.Dtos; - -namespace xStringService.Controllers -{ - public abstract class XStringServiceControllerBase : XIBaseProviderController, IXStringServiceControllerActions - { - // - #region Props ... - public IXStringProvider StringProvider { get; } - #endregion - - // - #region Constructor ... - public XStringServiceControllerBase( - ILogger logger, - IXStringProvider stringProvider, - XAppConfiguration appConfiguration, - IXIdentityProvider identityProvider, - XValidationProvider validationProvider - ) : base( - logger, - appConfiguration, - identityProvider, - validationProvider - ) - { - // - StringProvider = stringProvider; - } - #endregion - - // - #region Retrievers ... - /// - /// Retrieve all Exists Languages ... - /// - /// - [HttpGet("Languages")] - public virtual async Task>> GetLanguages() - { - // - // Do ... - try - { - // - var result = await StringProvider - .GetLanguages(); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Check Specified Resource Exists or not ... - /// - /// - /// - /// - [HttpGet("IsResourceExists")] - public virtual async Task> IsResourceExists( - [FromQuery] string resource, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .IsResourceExists( - resource: resource, - language: language - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Retrieve Specified Resource ... - /// - /// - /// - /// - [HttpGet("")] - public virtual async Task> Get( - [FromQuery] string resource, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .Get( - resource: resource, - language: language - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Retrieve Specified Translation of Specified Resource ... - /// - /// - /// - /// - [HttpGet("ResourceValue")] - public virtual async Task> GetResourceValue( - [FromQuery] string resource, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .GetResourceValue( - resource: resource, - language: language - ); - - // - return Ok(result); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Retrieve an Specified Resource Items ... - /// - /// - /// - [HttpGet("Resources")] - public virtual async Task>> GetResources( - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .GetResources(resource); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Retrieve Specified Resource Items as XResourceDto Presentation ... - /// - /// - /// - [HttpGet("AsResource")] - public virtual async Task> GetResource( - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .GetResource(resource); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - #endregion - - // - #region Query ... - /// - /// Query Resource IDs ... - /// - /// - /// - [HttpGet("QueryResourceIds")] - public virtual ActionResult> QueryResourceIds( - [FromQuery] XQuery query - ) - { - // - // Do ... - try - { - // - var result = StringProvider - .QueryResourceIds(query); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Query Specified Language Resources ... - /// - /// - /// - /// - [HttpGet("QueryLanguageResources")] - public virtual async Task>> QueryLanguageResources( - [FromQuery] XQuery query, - [FromQuery] string language = null // - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .QueryLanguageResources( - query: query, - language: language - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Query Locale Resources ... - /// - /// - /// - /// - [HttpGet("QueryLocaleResources")] - public virtual async Task>> QueryLocaleResources( - [FromQuery] XQuery query, - [FromQuery] string language = null // - ) - { - // - // Do ... - try - { - // - var result = await StringProvider - .QueryLocaleResources( - query: query, - language: language - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - #endregion - - // - #region Add/Update Actions ... - /// - /// Add Specified Resource ... - /// - /// - /// - /// - /// - [HttpPost("Add")] - public virtual async Task> Add( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .Add( - value: value, - resource: resource, - language: language, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Update Resource ... - /// - /// - /// - /// - /// - [HttpPost("Update")] - public virtual async Task> Update( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .Update( - value: value, - resource: resource, - language: language, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Or Update Resource ... - /// - /// - /// - /// - /// - [HttpPost("AddOrUpdate")] - public virtual async Task> AddOrUpdate( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .AddOrUpdate( - value: value, - resource: resource, - language: language, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Specified Resource ... - /// - /// - /// - [HttpPost("AddModel")] - public virtual async Task> AddEntity( - [FromBody] XStringDto item - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider.AddEntity( - item: item, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Update Specified Entity ... - /// - /// - /// - [HttpPost("UpdateModel")] - public virtual async Task> UpdateEntity( - [FromBody] XStringDto item - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .UpdateEntity( - item: item, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Or Update Specified Entitiy ... - /// - /// - /// - [HttpPost("AddOrUpdateModel")] - public virtual async Task> AddOrUpdateEntity( - [FromBody] XStringDto item - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .AddOrUpdateEntity( - item: item, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Specified Locale Resource ... - /// - /// - /// - /// - [HttpPost("AddLocale")] - public virtual async Task> AddByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .AddByLocaleResource( - item: item, - resource: resource, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Update Specified Locale Resource ... - /// - /// - /// - /// - [HttpPost("UpdateLocale")] - public virtual async Task> UpdateByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .UpdateByLocaleResource( - item: item, - resource: resource, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Or Update Resource By Locale ... - /// - /// - /// - /// - [HttpPost("AddOrUpdateLocale")] - public virtual async Task> AddOrUpdateByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .AddOrUpdateByLocaleResource( - item: item, - resource: resource, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Add Or Update all XResourceDto(s) Locales ... - /// - /// - /// - [HttpPost("AddResource")] - public virtual async Task>> AddByResource( - [FromBody] XResourceDto item - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .AddByResource( - item: item, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - #endregion - - // - #region Remove Actions ... - /// - /// Remove all Specified Language's Resources ... - /// - /// - /// - [HttpDelete("RemoveLanguage")] - public virtual async Task RemoveLanguageResources( - [FromQuery] string language - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - await StringProvider - .RemoveLanguageResources( - language: language, - connectionId: connectionId - ); - - // - return Ok(); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Remove all Specified Resource Ids instances ... - /// - /// - /// - [HttpDelete("RemoveLocales")] - public virtual async Task>> RemoveResource( - [FromQuery] string resource - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .RemoveResource( - resource: resource, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - [HttpDelete("Remove")] - public virtual async Task> Remove( - [FromQuery] string resource, - [FromQuery] string language = null - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - var result = await StringProvider - .Remove( - resource: resource, - language: language, - connectionId: connectionId - ); - - // - return Ok(result. - ToDynamicObject()); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - - /// - /// Remove all Resource of Specified XResourceDto ... - /// - /// - /// - [HttpPost("RemoveResource")] - public virtual async Task RemoveByResource( - [FromBody] XResourceDto item - ) - { - // - // Do ... - try - { - // - var connectionId = GetConnectionId(); - - // - await StringProvider - .RemoveByResource( - item: item, - connectionId: connectionId - ); - - // - return Ok(); - } - catch (Exception ex) - { - // - var result = GetExceptionActionResult(ex); - return result; - } - } - #endregion - } -} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index fd9faa1..f0f5f30 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -2,8 +2,6 @@ using System; using Microsoft.Extensions.DependencyInjection; using xExceptions.Constants; using xCommons.Extensions; -using xStringService.Interfaces; -using xStringService.Providers; using xStringService.Interfaces.Entities; namespace xStringService.DI @@ -87,9 +85,9 @@ namespace xStringService.DI throw exception; } - // - // Main Provider ... - services.Add(new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime)); + // // + // // Main Provider ... + // services.Add(new ServiceDescriptor(typeof(IXStringProvider), typeof(XStringProvider), lifeTime)); // Log("AddStringService Succeed ..."); diff --git a/DataHelper/Events/XStringEvents.cs b/DataHelper/Events/XStringEvents.cs index a56db2c..06d0f8d 100644 --- a/DataHelper/Events/XStringEvents.cs +++ b/DataHelper/Events/XStringEvents.cs @@ -1,4 +1,4 @@ -using xDataService.Events; +using xDataService.Providers; using xStringService.Interfaces.Entities; using xStringService.Models.Entities; diff --git a/Interfaces/IXBaseStringResourceProvider.cs b/Interfaces/IXBaseStringResourceProvider.cs deleted file mode 100644 index 50217ad..0000000 --- a/Interfaces/IXBaseStringResourceProvider.cs +++ /dev/null @@ -1,98 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using xCommons.Configurations; -using xModels.Dtos; -using xStringService.Models.Dtos; - -namespace xStringService.Interfaces -{ - public interface IXBaseStringResourceProvider - { - // - #region Props ... - string Prefix { get; } - IXStringProvider Provider { get; } - XAppConfiguration AppConfiguration { get; } - #endregion - - // - #region Actions ... - /// - /// Prepare Resource Title by Given Data ... - /// - /// - /// - string GetResourceTitle(string suffix); - - /// - /// Add Specified Locales ... - /// - /// - /// - /// - Task> Add( - string suffix, - IEnumerable locales - ); - - /// - /// Add Or Update Specified Locals ... - /// - /// - /// - /// - Task> AddOrUpdate( - string resource, - IEnumerable locales - ); - - /// - /// Get Specified Locale ... - /// - /// - /// - /// - Task GetLocale( - string suffix, - string language = null - ); - - /// - /// Add Specified Resource ... - /// - /// - /// - /// - Task AddResource( - string suffix, - XResourceDto item - ); - - /// - /// Get Resources of Specified Resource ... - /// - /// - /// - Task GetResource(string suffix); - - /// - /// Remove Specified Suffix Resources ... - /// - /// - /// - Task Remove(string suffix); - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - Task Remove( - string resource, - string language - ); - #endregion - } -} \ No newline at end of file diff --git a/Interfaces/IXStringProvider.cs b/Interfaces/IXStringProvider.cs deleted file mode 100644 index 26b5327..0000000 --- a/Interfaces/IXStringProvider.cs +++ /dev/null @@ -1,307 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using xCommons.Configurations; -using xDataService.Configuration; -using xModels.Dtos; -using xStringService.Hubs; -using xStringService.Interfaces.Entities; -using xStringService.Models.Dtos; -using xStringService.Models.Entities; - -namespace xStringService.Interfaces -{ - public interface IXStringProvider - { - // - #region Props ... - IHubContext Hub { get; } - IXStringRepository Repository { get; } - XAppConfiguration AppConfiguration { get; } - XDataServiceConfiguration DataConfiguration { get; } - #endregion - - // - #region Retrievers ... - /// - /// Retrieve all Exists Languages ... - /// - /// - Task> GetLanguages(); - - /// - /// Check Specified Resource Exists or not ... - /// - /// - /// - /// - Task IsResourceExists( - string resource, - string language = null - ); - - /// - /// Retrieve Specified Resource ... - /// - /// - /// - /// - Task Get( - string resource, - string language = null - ); - - /// - /// Retrieve Specified Translation of Specified Resource ... - /// - /// - /// - /// - Task GetResourceValue( - string resource, - string language = null - ); - - /// - /// Retrieve an Specified Resource Items ... - /// - /// - /// - Task> GetResources(string resource); - - /// - /// Retrieve Specified Resource Items as XResourceDto Presentation ... - /// - /// - /// - Task GetResource(string resource); - #endregion - - // - #region Query ... - /// - /// Query Resource IDs ... - /// - /// - /// - XQueryResult QueryResourceIds(XQuery query); - - /// - /// Query Specified Language Resources ... - /// - /// - /// - /// - Task> QueryLanguageResources( - XQuery query, - string language = null // - ); - - /// - /// Query Locale Resources ... - /// - /// - /// - /// - Task> QueryLocaleResources( - XQuery query, - string language = null // - ); - - /// - /// Conditional Query Resources ... - /// - /// - /// - /// - /// - Task> ConditionalQueryResources( - XQuery query, - Expression> condition, - string language = null - ); - #endregion - - // - #region Add/Update Actions ... - /// - /// Add Specified Resource ... - /// - /// - /// - /// - /// - /// - Task Add( - string resource, - string value, - string language = null, - string connectionId = null - ); - - /// - /// Update Resource ... - /// - /// - /// - /// - /// - /// - Task Update( - string resource, - string value, - string language = null, - string connectionId = null - ); - - /// - /// Add Or Update Resource ... - /// - /// - /// - /// - /// - /// - Task AddOrUpdate( - string resource, - string value, - string language = null, - string connectionId = null - ); - - /// - /// Add Specified Resource ... - /// - /// - /// - /// - Task AddEntity( - XStringDto item, - string connectionId = null - ); - - /// - /// Update Specified Entity ... - /// - /// - /// - /// - Task UpdateEntity( - XStringDto item, - string connectionId = null - ); - - /// - /// Add Or Update Specified Entitiy ... - /// - /// - /// - /// - Task AddOrUpdateEntity( - XStringDto item, - string connectionId = null - ); - - /// - /// Add Specified Locale Resource ... - /// - /// - /// - /// - /// - Task AddByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ); - - /// - /// Update Specified Locale Resource ... - /// - /// - /// - /// - /// - Task UpdateByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ); - - /// - /// Add Or Update Resource By Locale ... - /// - /// - /// - /// - /// - Task AddOrUpdateByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ); - - /// - /// Add Or Update all XResourceDto(s) Locales ... - /// - /// - /// - /// - Task> AddByResource( - XResourceDto item, - string connectionId = null - ); - #endregion - - // - #region Remove Actions ... - /// - /// Remove all Specified Language's Resources ... - /// - /// - /// - /// - Task RemoveLanguageResources( - string language, - string connectionId = null - ); - - /// - /// Remove all Specified Resource Ids instances ... - /// - /// - /// - /// - Task> RemoveResource( - string resource, - string connectionId = null - ); - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - /// - Task Remove( - string resource, - string language = null, - string connectionId = null - ); - - /// - /// Remove all Resource of Specified XResourceDto ... - /// - /// - /// - /// - Task RemoveByResource( - XResourceDto item, - string connectionId = null - ); - #endregion - } -} \ No newline at end of file diff --git a/Interfaces/IXStringServiceControllerActions.cs b/Interfaces/IXStringServiceControllerActions.cs deleted file mode 100644 index d5d8d23..0000000 --- a/Interfaces/IXStringServiceControllerActions.cs +++ /dev/null @@ -1,257 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using xModels.Dtos; -using xStringService.Models.Dtos; - -namespace xStringService.Interfaces -{ - public interface IXStringServiceControllerActions - { - // - #region Retrievers ... - /// - /// Retrieve all Exists Languages ... - /// - /// - Task>> GetLanguages(); - - /// - /// Check Specified Resource Exists or not ... - /// - /// - /// - /// - Task> IsResourceExists( - [FromQuery] string resource, - [FromQuery] string language = null - ); - - /// - /// Retrieve Specified Resource ... - /// - /// - /// - /// - Task> Get( - [FromQuery] string resource, - [FromQuery] string language = null - ); - - /// - /// Retrieve Specified Translation of Specified Resource ... - /// - /// - /// - /// - Task> GetResourceValue( - [FromQuery] string resource, - [FromQuery] string language = null - ); - - /// - /// Retrieve an Specified Resource Items ... - /// - /// - /// - Task>> GetResources( - [FromQuery] string resource - ); - - /// - /// Retrieve Specified Resource Items as XResourceDto Presentation ... - /// - /// - /// - Task> GetResource( - [FromQuery] string resource - ); - #endregion - - // - #region Query ... - /// - /// Query Resource IDs ... - /// - /// - /// - ActionResult> QueryResourceIds( - [FromQuery] XQuery query - ); - - /// - /// Query Specified Language Resources ... - /// - /// - /// - /// - Task>> QueryLanguageResources( - [FromQuery] XQuery query, - [FromQuery] string language = null // - ); - - /// - /// Query Locale Resources ... - /// - /// - /// - /// - Task>> QueryLocaleResources( - [FromQuery] XQuery query, - [FromQuery] string language = null // - ); - #endregion - - // - #region Add/Update Actions ... - /// - /// Add Specified Resource ... - /// - /// - /// - /// - /// - Task> Add( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ); - - /// - /// Update Resource ... - /// - /// - /// - /// - /// - Task> Update( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ); - - /// - /// Add Or Update Resource ... - /// - /// - /// - /// - /// - Task> AddOrUpdate( - [FromQuery] string resource, - [FromQuery] string value, - [FromQuery] string language = null - ); - - /// - /// Add Specified Resource ... - /// - /// - /// - Task> AddEntity( - [FromBody] XStringDto item - ); - - /// - /// Update Specified Entity ... - /// - /// - /// - Task> UpdateEntity( - [FromBody] XStringDto item - ); - - /// - /// Add Or Update Specified Entitiy ... - /// - /// - /// - Task> AddOrUpdateEntity( - [FromBody] XStringDto item - ); - - /// - /// Add Specified Locale Resource ... - /// - /// - /// - /// - Task> AddByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ); - - /// - /// Update Specified Locale Resource ... - /// - /// - /// - /// - Task> UpdateByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ); - - /// - /// Add Or Update Resource By Locale ... - /// - /// - /// - /// - Task> AddOrUpdateByLocaleResource( - [FromBody] XLocaleResourceDto item, - [FromQuery] string resource - ); - - /// - /// Add Or Update all XResourceDto(s) Locales ... - /// - /// - /// - Task>> AddByResource( - [FromBody] XResourceDto item - ); - #endregion - - // - #region Remove Actions ... - /// - /// Remove all Specified Language's Resources ... - /// - /// - /// - Task RemoveLanguageResources( - [FromQuery] string language - ); - - /// - /// Remove all Specified Resource Ids instances ... - /// - /// - /// - Task>> RemoveResource( - [FromQuery] string resource - ); - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - Task> Remove( - [FromQuery] string resource, - [FromQuery] string language = null - ); - - /// - /// Remove all Resource of Specified XResourceDto ... - /// - /// - /// - Task RemoveByResource( - [FromBody] XResourceDto item - ); - #endregion - } -} \ No newline at end of file diff --git a/Providers/XStringProvider.cs b/Providers/XStringProvider.cs deleted file mode 100644 index 9420234..0000000 --- a/Providers/XStringProvider.cs +++ /dev/null @@ -1,1477 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using xCommons.Configurations; -using xCommons.Extensions; -using xDataService.Configuration; -using xDataService.Extensions; -using xExceptions.Constants; -using xModels.Dtos; -using xPushService.Constants; -using xStringService.Extensions; -using xStringService.Hubs; -using xStringService.Interfaces; -using xStringService.Interfaces.Entities; -using xStringService.Models.Dtos; -using xStringService.Models.Entities; - -namespace xStringService.Providers -{ - public class XStringProvider : IXStringProvider - { - // - #region Props ... - public IHubContext Hub { get; } - public IXStringRepository Repository { get; } - public XAppConfiguration AppConfiguration { get; } - public XDataServiceConfiguration DataConfiguration { get; } - #endregion - - // - #region Constructor ... - public XStringProvider( - IXStringRepository repository, - XAppConfiguration appConfiguration, - XDataServiceConfiguration dataConfiguration, - IHubContext hub = null - ) - { - // - Repository = repository; - AppConfiguration = appConfiguration; - DataConfiguration = dataConfiguration; - Hub = hub; - } - #endregion - - // - #region Retrievers ... - /// - /// Retrieve all Exists Languages ... - /// - /// - public async Task> GetLanguages() - { - // - var result = (await Repository.GetAllAsync()) - .AsQueryable() - .Select(e => e.Language) - .Distinct() - .ToList(); - - // - // Check Configuration Default Language Exists ... - var hasDefaultLanguage = !AppConfiguration.DefaultLanguage.IsNullOrEmpty(); - if (hasDefaultLanguage) - { - // - // Find Default Language on Result ... - hasDefaultLanguage = result.Any(e => e.ToNormalString() == AppConfiguration.DefaultLanguage.ToNormalString()); - if (!hasDefaultLanguage) - { - result.Add(AppConfiguration.DefaultLanguage); - } - } - - // - result = result - .Distinct() - .ToList(); - - // - return result; - } - - /// - /// Check Specified Resource Exists or not ... - /// - /// - /// - /// - public async Task IsResourceExists( - string resource, - string language = null - ) - { - // - var result = false; - - // - // Validate ... - result = !resource.IsNullOrEmpty(); - if (!result) - { - return result; - } - - // - XString entity = null; - if (language.IsNullOrEmpty()) - { - // - entity = await Repository - .FindOneAsync(e => e.ResourceTitle == resource); - } - else - { - // - entity = await Repository - .FindOneAsync(e => e.ResourceTitle == resource && - e.Language.ToNormalString() == language.ToNormalString() - ); - } - result = !entity.IsNullOrDefault(); - - // - return result; - } - - /// - /// Retrieve Specified Resource ... - /// - /// - /// - /// - public async Task Get( - string resource, - string language = null - ) - { - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - if (resource.IsNullOrEmpty() || language.IsNullOrEmpty()) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Resource Exists ... - var isExists = await IsResourceExists( - resource: resource, - language: language - ); - if (!isExists) - { - XException.NotFound.Throw(); - } - - // - var entity = await Repository - .FindOneAsync(e => - e.ResourceTitle == resource && - e.Language.ToNormalString() == language.ToNormalString() - ); - if (entity.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - return entity.ToXStringDto(); - } - - /// - /// Retrieve Specified Translation of Specified Resource ... - /// - /// - /// - /// - public async Task GetResourceValue( - string resource, - string language = null - ) - { - // - var result = ""; - - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - var isValid = !resource.IsNullOrEmpty() && - !language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Langauage Exists ... - var languages = await GetLanguages(); - isValid = languages.Any(l => l.ToNormalString() == language.ToNormalString()); - if (!isValid) - { - XException.NotFound.Throw(); - } - - // - // Check Resource Exists ... - isValid = await IsResourceExists(resource); - if (!isValid) - { - XException.NotFound.Throw(); - } - - // - var entity = await Repository.FindOneAsync(e => - e.ResourceTitle == resource && - e.Language.ToNormalString() == language.ToNormalString() - ); - isValid = !entity.IsNullOrDefault(); - if (!isValid) - { - XException.ActionFailed.Throw(); - } - - // - result = entity.TranslatedValue; - - // - return result; - } - - /// - /// Retrieve an Specified Resource Items ... - /// - /// - /// - public async Task> GetResources(string resource) - { - // - // Validate ... - var isValid = await IsResourceExists(resource); - if (!isValid) - { - XException.NotFound.Throw(); - } - - // - var result = (await Repository - .FindManyAsync(e => e.ResourceTitle == resource)) - .ToXStringDtos(); - - // - return result; - } - - /// - /// Retrieve Specified Resource Items as XResourceDto Presentation ... - /// - /// - /// - public async Task GetResource(string resource) - { - // - // Validate ... - bool isExists = await IsResourceExists(resource); - if (!isExists) - { - XException.NotFound.Throw(); - } - - // - // Retrieve String Dto (s) ... - var resources = await GetResources(resource); - if (!resource.HasChild()) - { - XException.NotFound.Throw(); - } - - // - var result = new XResourceDto - { - Resource = resource, - Locales = resources.ToXLocaleResourceDtos() - }; - - return result; - } - #endregion - - // - #region Query ... - /// - /// Query Resource IDs ... - /// - /// - /// - public XQueryResult QueryResourceIds(XQuery query) - { - // - // Normalize ... - query = query.NormalizeQuery(DataConfiguration); - - // - // Read Resource IDs ... - var items = Repository - .AsQueryable() - .Select(e => e.ResourceTitle) - .Distinct(); - - // - var totalItemsCount = items.Count(); - - // - // Apply Filter ... - if (totalItemsCount > 0 && - !query.Filter.IsNullOrEmpty()) - { - // - items = items - .Where(e => e.ToNormalString().Contains(query.Filter.ToNormalString())); - } - - // - var filteredItemsCount = items.Count(); - var totalPagesCount = query.CountPages(totalItemsCount); - var filteredPagesCount = query.CountPages(filteredItemsCount); - - // - // Apply Paging / Sorting ... - if (totalItemsCount > 0 && - filteredItemsCount > 0) - { - // - items = items.ApplyPaging( - query.Page, - query.PageSize - ); - } - - // - // Prepare result ... - var result = new XQueryResult - { - Items = items, - Page = query.Page, - PageSize = query.PageSize, - TotalPages = totalPagesCount, - TotalItems = totalItemsCount, - TotalFilteredPages = filteredPagesCount, - TotalFilteredItems = filteredItemsCount - }; - - // - return result; - } - - /// - /// Query Specified Language Resources ... - /// - /// - /// - /// - public async Task> QueryLanguageResources( - XQuery query, - string language = null // - ) - { - // - // Normalize ... - query = query.NormalizeQuery(DataConfiguration); - - // - // Validate ... - if (query.IsNull()) - { - XException.InvalidArgs.Throw(); - } - - // - XQueryResult entityResult = null; - if (language.IsNullOrEmpty()) - { - // - entityResult = await Repository.QueryAsync(query); - } - else - { - // - entityResult = await Repository.ConditionalQueryAsync( - e => e.Language.ToNormalString() == language.ToNormalString(), - query - ); - } - - // - var result = new XQueryResult - { - Page = entityResult.Page, - PageSize = entityResult.PageSize, - TotalPages = entityResult.TotalPages, - TotalItems = entityResult.TotalItems, - TotalFilteredPages = entityResult.TotalFilteredPages, - TotalFilteredItems = entityResult.TotalFilteredItems, - Items = entityResult.Items.Select(i => i.ToXStringDto()), - }; - - // - return result; - } - - /// - /// Query Locale Resources ... - /// - /// - /// - /// - public async Task> QueryLocaleResources( - XQuery query, - string language = null // - ) - { - // - // Normalize ... - query.NormalizeQuery(DataConfiguration); - - // - // Validate ... - if (query.IsNull()) - { - XException.InvalidArgs.Throw(); - } - - // - IEnumerable items = null; - if (!language.IsNullOrEmpty()) - { - items = (await Repository - .FindManyAsync(e => e.Language.ToNormalString() == language.ToNormalString())) - .ToXLocaleResourceDtos(); - } - else - { - items = (await Repository.GetAllAsync()) - .ToXLocaleResourceDtos(); - } - - // - var totalItemsCount = items.Count(); - - // - // Apply Filter ... - if (totalItemsCount > 0 && - !query.Filter.IsNullOrEmpty()) - { - // - items = items.ApplyFilter(query.Filter); - } - - // - var filteredItemsCount = items.Count(); - var totalPagesCount = query.CountPages(totalItemsCount); - var filteredPagesCount = query.CountPages(filteredItemsCount); - - // - // Apply Paging/Sorting ... - if (totalItemsCount > 0 && - filteredItemsCount > 0) - { - // - // Apply Sorting ... - items = items.ApplySorting( - query.SortBy, - query.IsAscending - ); - - // - // Apply Paging ... - items = items.ApplyPaging( - query.Page, - query.PageSize - ); - } - - // - // Prepare Result ... - var result = new XQueryResult - { - Items = items, - Page = query.Page, - PageSize = query.PageSize, - TotalPages = totalPagesCount, - TotalItems = totalItemsCount, - TotalFilteredPages = filteredPagesCount, - TotalFilteredItems = filteredItemsCount - }; - - // - return result; - } - - /// - /// Conditional Query Resources ... - /// - /// - /// - /// - /// - public async Task> ConditionalQueryResources( - XQuery query, - Expression> condition, - string language = null - ) - { - // - // Normalize ... - query = query.NormalizeQuery(DataConfiguration); - - // - if (!language.IsNullOrEmpty()) - { - // - // Renew and Update Condition by Language Checking ... - var compiledCondition = condition.Compile(); - condition = - e => compiledCondition(e) && - e.Language.ToNormalString() == language.ToNormalString(); - } - - // - var entityResult = await Repository.ConditionalQueryAsync(condition, query); - var result = new XQueryResult - { - Page = entityResult.Page, - PageSize = entityResult.PageSize, - TotalPages = entityResult.TotalPages, - TotalItems = entityResult.TotalItems, - TotalFilteredPages = entityResult.TotalFilteredPages, - TotalFilteredItems = entityResult.TotalFilteredItems, - Items = entityResult.Items.Select(i => i.ToXStringDto()), - }; - - // - return result; - } - #endregion - - // - #region Add/Update Actions ... - /// - /// Add Specified Resource ... - /// - /// - /// - /// - /// - /// - public async Task Add( - string resource, - string value, - string language = null, - string connectionId = null - ) - { - // - bool result = false; - - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - result = - !value.IsNullOrEmpty() && - !resource.IsNullOrEmpty() && - !language.IsNullOrEmpty(); - if (!result) - { - return result; - } - - // - // Check Exists ... - bool isExists = await IsResourceExists(resource, language); - result = !isExists; - if (!result) - { - XException.Duplicate.Throw(); - } - - // - // Prepare Entity to Add ... - XString entity = new XString - { - Language = language, - TranslatedValue = value, - ResourceTitle = resource, - }; - entity = await Repository.AddAsync(entity); - result = !entity.IsNullOrDefault(); - if (result) - { - // - await SendPush( - action: XBaseEntityHubAction.Add.GetStringValue(), - entityId: entity.Id, - connectionId: connectionId - ); - } - - // - return result; - } - - /// - /// Update Resource ... - /// - /// - /// - /// - /// - /// - public async Task Update( - string resource, - string value, - string language = null, - string connectionId = null - ) - { - // - bool result = false; - - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - result = - !value.IsNullOrEmpty() && - !language.IsNullOrEmpty() && - !resource.IsNullOrEmpty(); - if (!result) - { - return result; - } - - // - // Check Value Must Exists ... - result = await IsResourceExists( - resource: resource, - language: language - ); - if (!result) - { - XException.NotFound.Throw(); - } - - // - var entity = await Repository.FindOneAsync(e => - e.ResourceTitle == resource && - e.Language.ToNormalString() == language.ToNormalString() - ); - if (entity.IsNullOrDefault()) - { - XException.NotFound.Throw(); - } - - // - // Update Entity ... - entity.TranslatedValue = value; - entity = await Repository.UpdateAsync(entity.Id, entity); - result = !entity.IsNullOrDefault(); - if (result) - { - // - await SendPush( - action: XBaseEntityHubAction.Update.GetStringValue(), - entityId: entity.Id, - connectionId: connectionId - ); - } - - // - return result; - } - - /// - /// Add Or Update Resource ... - /// - /// - /// - /// - /// - /// - public async Task AddOrUpdate( - string resource, - string value, - string language = null, - string connectionId = null - ) - { - // - var result = false; - - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Check Existsance ... - bool isExists = await IsResourceExists( - resource: resource, - language: language - ); - - // - // Act Based on Existance ... - if (isExists) - { - // - result = await Update( - value: value, - resource: resource, - language: language - ); - } - else - { - // - result = await Add( - value: value, - resource: resource, - language: language - ); - } - - // - return result; - } - - /// - /// Add Specified Resource ... - /// - /// - /// - /// - public async Task AddEntity( - XStringDto item, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = !item.IsNull() && - !item.ResourceTitle.IsNullOrEmpty() && - !item.TranslatedValue.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Normalize ... - if (item.Language.IsNullOrEmpty()) - { - item.Language = AppConfiguration.DefaultLanguage; - } - isValid = !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Resource Doesn't Exists ... - isValid = await IsResourceExists( - resource: item.ResourceTitle, - language: item.Language - ); - if (isValid) - { - XException.Duplicate.Throw(); - } - - // - var result = new XString - { - Language = item.Language, - ResourceTitle = item.ResourceTitle, - TranslatedValue = item.TranslatedValue - }; - result = await Repository.AddAsync(result); - if (result.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - await SendPush( - action: XBaseEntityHubAction.Add.GetStringValue(), - entityId: result.Id, - connectionId: connectionId - ); - - // - return result.ToXStringDto(); - } - - /// - /// Update Specified Entity ... - /// - /// - /// - /// - public async Task UpdateEntity( - XStringDto item, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = !item.IsNull() && - !item.ResourceTitle.IsNullOrEmpty() && - !item.TranslatedValue.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Normalize ... - if (item.Language.IsNullOrEmpty()) - { - item.Language = AppConfiguration.DefaultLanguage; - } - isValid = !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Resource Exists ... - isValid = await IsResourceExists( - resource: item.ResourceTitle, - language: item.Language - ); - if (!isValid) - { - XException.NotFound.Throw(); - } - - // - var entity = await Repository.FindOneAsync(e => - e.ResourceTitle == item.ResourceTitle && - e.Language.ToNormalString() == item.Language.ToNormalString() - ); - if (entity.IsNullOrDefault()) - { - XException.NotFound.Throw(); - } - - // - // Update Entity ... - entity.TranslatedValue = item.TranslatedValue; - entity = await Repository.UpdateAsync(entity.Id, entity); - if (entity.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - XStringDto result = entity.ToXStringDto(); - await SendPush( - action: XBaseEntityHubAction.Update.GetStringValue(), - entityId: entity.Id, - connectionId: connectionId - ); - - // - return result; - } - - /// - /// Add Or Update Specified Entitiy ... - /// - /// - /// - /// - public async Task AddOrUpdateEntity( - XStringDto item, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = !item.IsNull() && - !item.ResourceTitle.IsNullOrEmpty() && - !item.TranslatedValue.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Normalize ... - if (item.Language.IsNullOrEmpty()) - { - item.Language = AppConfiguration.DefaultLanguage; - } - isValid = !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Resource Exists ... - var isExists = await IsResourceExists( - resource: item.ResourceTitle, - language: item.Language - ); - - // - XStringDto result = null; - if (!isExists) - { - result = await AddEntity(item, connectionId); - } - else - { - result = await UpdateEntity(item, connectionId); - } - if (result.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - return result; - } - - /// - /// Add Specified Locale Resource ... - /// - /// - /// - /// - /// - public async Task AddByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = - !item.IsNull() && - !resource.IsNullOrEmpty() && - !item.Value.IsNullOrEmpty() && - !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Specified Resource / Language Exists ... - isValid = await IsResourceExists( - resource: resource, - language: item.Language - ); - if (isValid) - { - XException.Duplicate.Throw(); - } - - // - // Prepare Item to Add ... - var result = new XStringDto - { - Language = item.Language, - ResourceTitle = resource, - TranslatedValue = item.Value - }; - result = await AddEntity(result, connectionId); - - // - return result; - } - - /// - /// Update Specified Locale Resource ... - /// - /// - /// - /// - /// - public async Task UpdateByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = - !item.IsNull() && - !resource.IsNullOrEmpty() && - !item.Value.IsNullOrEmpty() && - !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Specified Resource / Language Exists ... - isValid = await IsResourceExists( - resource: resource, - language: item.Language - ); - if (!isValid) - { - XException.NotFound.Throw(); - } - - // - var entity = await Repository.FindOneAsync(e => - e.ResourceTitle == resource && - e.Language.ToNormalString() == item.Language.ToNormalString() - ); - if (entity.IsNullOrDefault()) - { - XException.NotFound.Throw(); - } - - // - // Update ... - entity.TranslatedValue = item.Value; - entity = await Repository.UpdateAsync(entity.Id, entity); - if (entity.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - XStringDto result = entity.ToXStringDto(); - await SendPush( - action: XBaseEntityHubAction.Update.GetStringValue(), - entityId: entity.Id, - connectionId: connectionId - ); - - // - return result; - } - - /// - /// Add Or Update Resource By Locale ... - /// - /// - /// - /// - /// - public async Task AddOrUpdateByLocaleResource( - XLocaleResourceDto item, - string resource, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = - !item.IsNull() && - !resource.IsNullOrEmpty() && - !item.Value.IsNullOrEmpty() && - !item.Language.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - // Check Specified Resource / Language Exists ... - isValid = await IsResourceExists( - resource: resource, - language: item.Language - ); - - // - XStringDto result = null; - if (!isValid) - { - // - result = await AddByLocaleResource( - item: item, - resource: resource - ); - } - else - { - // - result = await UpdateByLocaleResource( - item: item, - resource: resource - ); - } - if (result.IsNullOrDefault()) - { - XException.ActionFailed.Throw(); - } - - // - return result; - } - - /// - /// Add Or Update all XResourceDto(s) Locales ... - /// - /// - /// - /// - public async Task> AddByResource( - XResourceDto item, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = - !item.IsNull() && - item.Locales.HasChild() && - !item.Resource.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - var result = await Task - .WhenAll(item.Locales - .Select(async e => await AddOrUpdateByLocaleResource(e, item.Resource))); - - // - return result; - } - #endregion - - // - #region Remove Actions ... - /// - /// Remove all Specified Language's Resources ... - /// - /// - /// - /// - public async Task RemoveLanguageResources( - string language, - string connectionId = null - ) - { - // - // Validate ... - bool isValid = !language.IsNullOrEmpty(); - if (!isValid) - { - return; - } - - // - var entities = await Repository - .FindManyAsync(e => e.Language.ToNormalString() == language.ToNormalString()); - - // - try - { - // - await Repository.RemoveRangeAsync(entities); - - // - var entitieIds = entities.Select(e => e.Id); - await SendCustomPush( - action: XBaseEntityHubAction.DeleteMany.GetStringValue(), - payLoad: entitieIds.ToJSON(camelCase: true), - connectionId: connectionId - ); - } - catch (Exception ex) - { - throw ex; - } - } - - /// - /// Remove all Specified Resource Ids instances ... - /// - /// - /// - /// - public async Task> RemoveResource( - string resource, - string connectionId = null - ) - { - // - // Validate ... - if (resource.IsNullOrEmpty()) - { - XException.InvalidArgs.Throw(); - } - - // - var result = new List(); - var entities = await Repository.FindManyAsync(e => e.ResourceTitle == resource); - foreach (var entity in entities) - { - // - var removed = await Repository.RemoveAsync(entity); - if (!removed.IsNullOrDefault()) - { - result.Add(removed.ToXStringDto()); - } - } - - // - if (result.HasChild()) - { - // - var ids = result.Select(i => i.Id); - await SendCustomPush( - action: XBaseEntityHubAction.DeleteMany.GetStringValue(), - payLoad: ids.ToJSON(camelCase: true), - connectionId: connectionId - ); - } - - // - return result; - } - - /// - /// Remove Specified Resource ... - /// - /// - /// - /// - /// - public async Task Remove( - string resource, - string language = null, - string connectionId = null - ) - { - // - bool result = false; - - // - // Normalize ... - if (language.IsNullOrEmpty()) - { - language = AppConfiguration.DefaultLanguage; - } - - // - // Validate ... - result = - !language.IsNullOrEmpty() && - !resource.IsNullOrEmpty(); - if (!result) - { - return result; - } - - // - // Check Resource Exists ... - result = await IsResourceExists( - language: language, - resource: resource - ); - if (!result) - { - return result; - } - - // - // Retrieve Entity ... - var entity = await Repository.FindOneAsync(e => - e.ResourceTitle == resource && - e.Language.ToNormalString() == language.ToNormalString() - ); - result = !entity.IsNullOrDefault(); - if (!result) - { - return result; - } - - // - // Delete ... - entity = await Repository.RemoveAsync(entity.Id); - result = !entity.IsNullOrDefault(); - if (result) - { - // - await SendPush( - action: XBaseEntityHubAction.Delete.GetStringValue(), - entityId: entity.Id, - connectionId: connectionId - ); - } - - // - return result; - } - - /// - /// Remove all Resource of Specified XResourceDto ... - /// - /// - /// - /// - public async Task RemoveByResource( - XResourceDto item, - string connectionId = null - ) - { - // - bool isValid = - !item.IsNullOrDefault() && - item.Locales.HasChild() && - !item.Resource.IsNullOrEmpty(); - if (!isValid) - { - XException.InvalidArgs.Throw(); - } - - // - foreach (var locale in item.Locales) - { - // - await Remove( - resource: item.Resource, - language: locale.Language - ); - } - } - #endregion - - // - #region Hub Actions ... - /// - /// Send Specified Hub Action Notifications ... - /// - /// - /// - /// - public async Task SendPush( - string action, - int entityId, - string connectionId = null - ) - { - // - var actions = new List - { - XBaseEntityHubAction.Add.GetStringValue(), - XBaseEntityHubAction.Update.GetStringValue(), - XBaseEntityHubAction.Delete.GetStringValue(), - XBaseEntityHubAction.AddMany.GetStringValue(), - XBaseEntityHubAction.DeleteMany.GetStringValue(), - XBaseEntityHubAction.UpdateMany.GetStringValue(), - XBaseEntityHubAction.AddOrUpdate.GetStringValue(), - }; - - // - // Validate ... - var isValid = - entityId > 0 && - !Hub.IsNull() && - !action.IsNullOrEmpty() && - actions.Contains(action); - if (!isValid) - { - return; - } - - // - // Retrieve and Validate Entity ... - XString entity = null; - try - { - entity = await Repository.GetAsync(entityId); - } - catch { } - isValid = !entity.IsNullOrDefault(); - if (!isValid) - { - return; - } - - // - var clients = Hub.Clients.All; - if (!connectionId.IsNullOrEmpty()) - { - clients = Hub.Clients.AllExcept(connectionId); - } - await clients.SendAsync(action, entity.ToJSON(camelCase: true), connectionId); - } - - /// - /// Send Custom Push Message ... - /// - /// - /// - /// - /// - public async Task SendCustomPush( - string action, - string payLoad, - string connectionId = null - ) - { - // - var customActions = new List - { - XBaseEntityHubAction.DeleteMany.GetStringValue(), - }; - - // - // Validate ... - var isValid = - !Hub.IsNull() && - !action.IsNullOrEmpty() && - customActions.Contains(action); - if (!isValid) - { - return; - } - - // - var clients = Hub.Clients.All; - if (!connectionId.IsNullOrEmpty()) - { - clients = Hub.Clients.AllExcept(connectionId); - } - await clients.SendAsync(action, payLoad, connectionId); - } - #endregion - } -} \ No newline at end of file diff --git a/nuget.config b/nuget.config index 632defe..c6fd40c 100644 --- a/nuget.config +++ b/nuget.config @@ -1,7 +1,8 @@ + - + \ No newline at end of file