using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using xCommons.Extensions; using xExceptions.Constants; using xModels.Dtos; using xStringService.Interfaces; using xStringService.Interfaces.Dtos; using xStringService.Models.Dtos; namespace xStringService.Providers { public abstract class XBaseItemResourceProvider : IXItemResourceProvider { // #region Props ... /// /// Item Name for Resource Providing ... /// private readonly string item; /// /// an Implementation of Item Resource Provider /// /// private readonly XBaseResourceProvider provider; /// /// String Provider Service ... /// private readonly IXStringServiceProvider stringProvider; #endregion // #region Constructor ... protected XBaseItemResourceProvider( string item, IXStringServiceProvider stringProvider ) { // this.item = item; this.stringProvider = stringProvider; provider = new XBaseResourceProvider( item, stringProvider ); } #endregion // #region Group Actions ... /// /// Retrieve a List of Exists Languages for Current Item ... /// /// /// public virtual async Task> GetLanguages( CancellationToken cancellationToken = default ) { // var result = (await ExtractItems(cancellationToken)) .Select(x => x.Language) .Distinct(); // return result; } /// /// Retrieve all Exists Items of type ... /// /// /// public virtual async Task> GetResourceLocales( CancellationToken cancellationToken = default ) { // var result = await ExtractItems(cancellationToken); // return result; } /// /// Remove all Exists Locales which Related to this Provider ... /// /// /// /// public virtual async Task RemoveResourceLocales( string connectionId = null, CancellationToken cancellationToken = default ) { // var isDelete = false; XStringDto deleted = null; var items = await ExtractItems(cancellationToken); foreach (var item in items) { // // Check Cancellation ... if (cancellationToken.IsCancellationRequested) { break; } // // Remove Item ... deleted = await stringProvider.RemoveAsync( id: item.Id, softDelete: false, saveChanges: true, connectionId: connectionId, cancellationToken: cancellationToken ); // // Check Removation Succeed ... isDelete = !deleted.IsNullOrDefault(); if (!isDelete) { XException.ActionFailed.Throw(); } } } #endregion // #region Item Actions ... /// /// Get all Exists Items for Specified identitifer ... /// /// /// /// public virtual async Task> GetResourceLocales( object identifier, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // // Retrieve Resource List ... var result = await provider .GetResourceLocales(cancellationToken); // return result; } /// /// Get all Exists Items for Specified identitifer ... /// /// /// /// public virtual async Task> GetLocales( object identifier, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // // Retrieve Resource List ... var result = await provider .GetLocales(cancellationToken); // return result; } /// /// Get all Exists Locales Representaion based on Specified Identitifer /// as XResourceDto ... /// /// /// /// public virtual async Task GetResource( object identifier, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // // Retrieve Resource List ... var result = await provider .GetResource(cancellationToken); // return result; } /// /// Add Or Update Locale for Specified Identitifer ... /// /// /// /// /// /// public virtual async Task AddOrUpdateLocale( object identifier, XLocaleResourceDto locale, string connectionId = null, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull() && !locale.IsNullOrDefault() && !locale.Value.IsNullOrEmpty() && !locale.Language.IsNullOrEmpty(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // // Retrieve Resource List ... var result = await provider .AddOrUpdateLocale( locale: locale, connectionId: connectionId, cancellationToken: cancellationToken ); // return result; } /// /// Update a Resource State ... /// /// /// /// /// /// public virtual async Task AddOrUpdateResource( object identifier, XResourceDto resource, string connectionId = null, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Retrieve Identified Resource Title ... var identifiedResource = GetResourceTitle(identifier); isValid = !resource.IsNullOrDefault() && (resource.Resource.IsNullOrEmpty() || resource.Resource == identifiedResource); if (!isValid) { XException.InvalidArgs.Throw(); } if (resource.Resource.IsNullOrEmpty()) { resource.Resource = identifiedResource; } // // Setting Resource Title ... SetResourceTitle(identifier); // // Retrieve Resource List ... var result = await provider .AddOrUpdateResource( resource: resource, connectionId: connectionId, cancellationToken: cancellationToken ); // return result; } /// /// Remove Specified Language Locale of Specified Identifier ... /// /// /// /// /// /// public virtual async Task RemoveLocale( object identifier, string language, string connectionId = null, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull() && !language.IsNullOrEmpty(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Check Locale Exists ... isValid = await HasLocale( language: language, identifier: identifier, cancellationToken: cancellationToken ); if (!isValid) { XException.NotFound.Throw(); } // var locale = (await GetLocales( identifier: identifier, cancellationToken: cancellationToken )) .FirstOrDefault(x => x.Language.Equals(language, StringComparison.InvariantCultureIgnoreCase)); isValid = !locale.IsNullOrDefault(); if (!isValid) { XException.NotFound.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // var result = await provider.RemoveLocale( locale: locale, connectionId: connectionId, cancellationToken: cancellationToken ); // return result; } /// /// Remove all Exists Locales for Specified Identifier ... /// /// /// /// /// public virtual async Task RemoveLocales( object identifier, string connectionId = null, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // var resource = await GetResource( identifier: identifier, cancellationToken: cancellationToken ); isValid = !resource.IsNullOrDefault(); if (!isValid) { XException.NotFound.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // await provider.RemoveResource( resource: resource, connectionId: connectionId, cancellationToken: cancellationToken ); } /// /// Check Specified ocale Exists for Specified Identifier ... /// /// /// /// /// public virtual async Task HasLocale( object identifier, string language, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull() && !language.IsNullOrEmpty(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // var result = await provider.HasLocale( language: language, cancellationToken: cancellationToken ); // return result; } /// /// Get all Exists Languages for Specified Identifier ... /// /// /// /// public virtual async Task> GetLanguages( object identifier, CancellationToken cancellationToken = default ) { // // Validate ... var isValid = !identifier.IsNull(); if (!isValid) { XException.InvalidArgs.Throw(); } // // Setting Resource Title ... SetResourceTitle(identifier); // var result = await provider .GetLanguages(cancellationToken); // return result; } #endregion // #region Resource Title Action ... /// /// Retrieve Resource Title ... /// /// /// public string GetResourceTitle(object identifier = null) { // var result = item; // if (!identifier.IsNull()) { result = $"{item}_{identifier}"; } // return result; } #endregion // #region Private ... /// /// Set Resource Title ... /// /// private void SetResourceTitle(object identifier = null) { // var itemIdentifier = GetResourceTitle(identifier); provider.SetField( field: "resource", value: $"{itemIdentifier}" ); } /// /// xtract all Items which Belongs to Provider ... /// /// /// private async Task> ExtractItems( CancellationToken cancellationToken = default ) { // var result = await stringProvider .FindManyAsync( includeBuilder: null, cancellationToken: cancellationToken, orderBuilder: x => x.OrderBy(y => y.Id), predicate: x => x.ResourceTitle.StartsWith( item, StringComparison.InvariantCultureIgnoreCase) ); return result; } #endregion } }