diff --git a/Providers/XBaseOwnedItemResourceProvider.cs b/Providers/XBaseOwnedItemResourceProvider.cs index 29dfc76..c24d0e1 100644 --- a/Providers/XBaseOwnedItemResourceProvider.cs +++ b/Providers/XBaseOwnedItemResourceProvider.cs @@ -3,8 +3,11 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using xCommons.Configurations; using xCommons.Extensions; +using xExceptions.Constants; using xModels.Dtos; +using xStringService.Extensions; using xStringService.Interfaces; using xStringService.Interfaces.Dtos; using xStringService.Models.Dtos; @@ -20,6 +23,11 @@ namespace xStringService.Providers /// private readonly string item; + /// + /// Application Configuration ... + /// + private readonly XAppConfiguration appConfig; + /// /// String Provider Service ... /// @@ -30,11 +38,13 @@ namespace xStringService.Providers #region Constructor ... protected XBaseOwnedItemResourceProvider( string item, + XAppConfiguration appConfig, IXStringServiceProvider stringProvider ) { // this.item = item; + this.appConfig = appConfig; this.stringProvider = stringProvider; } #endregion @@ -52,7 +62,19 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var items = await ExtractStrings( + owner: owner, + identifier: null, + cancellationToken: cancellationToken + ); + var result = + items + .Select(x => x.Language) + .Distinct(); + + // + return result; } /// @@ -66,7 +88,15 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var result = await ExtractStrings( + owner: owner, + identifier: null, + cancellationToken: cancellationToken + ); + + // + return result; } /// @@ -82,7 +112,38 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var items = await ExtractStrings( + owner: owner, + identifier: null, + cancellationToken: cancellationToken + ); + + // + foreach (var item in items) + { + // + // Check Cancellation Token ... + if (cancellationToken.IsCancellationRequested) + { + break; + } + + // + // Try to Remove it ... + try + { + // + await stringProvider.RemoveAsync( + id: item.Id, + softDelete: false, + saveChanges: true, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + } + catch { } + } } #endregion @@ -101,7 +162,24 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var resourceTitle = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var result = await stringProvider.FindManyAsync( + includeBuilder: null, + ignoreSoftDeleteds: true, + cancellationToken: cancellationToken, + predicate: x => x.ResourceTitle + .ToNormalString() + .Contains(resourceTitle.ToNormalString()) + ); + + // + return result; } /// @@ -117,7 +195,16 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var result = (await GetResourceLocales( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + )) + .Select(x => x.ToXLocaleResourceDto()); + + // + return result; } /// @@ -134,7 +221,25 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var resource = GetResourceTitle( + owner: owner + ); + var locales = await GetLocales( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + + // + var result = new XResourceDto + { + Locales = locales, + Resource = resource + }; + + // + return result; } /// @@ -154,7 +259,30 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var resource = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var resourceDto = await stringProvider.AddOrUpdateByLocaleResource( + item: locale, + resource: resource, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + // Retrieve XResourceDto ... + var result = await GetResource( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + + // + return result; } /// @@ -174,7 +302,70 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var resourceTitle = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var isValid = + !resource.IsNullOrDefault() && + resource.Resource == resourceTitle; + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Retrieve Exists Resource ... + var result = await GetResource( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + isValid = + !result.IsNullOrDefault() && + result.Locales.HasChild(); + if (isValid) + { + await RemoveLocales( + owner: owner, + identifier: identifier, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + } + + // + foreach (var locale in resource.Locales) + { + // + var item = new XStringDto + { + Language = locale.Language, + ResourceTitle = resourceTitle, + TranslatedValue = locale.Value, + }; + + // + await stringProvider.AddAsync( + item: item, + saveChanges: true, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + } + + // + result = await GetResource( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + + // + return result; } /// @@ -194,7 +385,72 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + XResourceDto result = null; + language = NormalizedLanguage(language); + + // + // Check Locale Exists ... + var isValid = await HasLocale( + owner: owner, + language: language, + identifier: identifier, + cancellationToken: cancellationToken + ); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + var resourceTitle = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var item = await stringProvider.FindOneAsync( + includeBuilder: null, + ignoreSoftDeleteds: true, + cancellationToken: cancellationToken, + predicate: x => + x.Language.Equals( + language, + StringComparison.InvariantCultureIgnoreCase) && + x.ResourceTitle.Equals( + resourceTitle, + StringComparison.InvariantCultureIgnoreCase) + ); + isValid = !item.IsNullOrDefault(); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + // Remove XStringDto ... + await stringProvider.RemoveAsync( + id: item.Id, + softDelete: false, + saveChanges: true, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + try + { + // + result = await GetResource( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + } + catch { } + + // + return result; } /// @@ -212,7 +468,34 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var resourceTitle = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var items = await stringProvider.FindManyAsync( + includeBuilder: null, + ignoreSoftDeleteds: true, + cancellationToken: cancellationToken, + predicate: x => + x.ResourceTitle.Equals( + resourceTitle, + StringComparison.InvariantCultureIgnoreCase) + ); + var isValid = !item.IsNull(); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + await stringProvider.RemoveResource( + resource: resourceTitle, + connectionId: connectionId, + cancellationToken: cancellationToken + ); } /// @@ -230,7 +513,22 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + language = NormalizedLanguage(language); + var locales = await GetLocales( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + var result = + language.HasChild() && + locales.Any(l => l.Language.Equals( + language, + StringComparison.InvariantCultureIgnoreCase) + ); + + // + return result; } /// @@ -246,7 +544,19 @@ namespace xStringService.Providers CancellationToken cancellationToken = default ) { - throw new NotImplementedException(); + // + var items = await ExtractStrings( + owner: owner, + identifier: identifier, + cancellationToken: cancellationToken + ); + var result = + items + .Select(x => x.Language) + .Distinct(); + + // + return result; } #endregion @@ -298,6 +608,30 @@ namespace xStringService.Providers // #region Private ... + private string NormalizedLanguage( + string language = null + ) + { + // + var result = language; + if (result.IsNullOrEmpty()) + { + result = GetDefaultLanguage(); + } + + // + return result; + } + + private string GetDefaultLanguage() + { + // + var result = appConfig.DefaultLanguage; + + // + return result; + } + private async Task> ExtractStrings( string resourceTitle, CancellationToken cancellationToken = default @@ -317,10 +651,32 @@ namespace xStringService.Providers includeBuilder: null, ignoreSoftDeleteds: true, cancellationToken: cancellationToken, + orderBuilder: x => x.OrderBy(y => y.Id), predicate: x => x.ResourceTitle .ToNormalString() - .Contains(resourceTitle), - orderBuilder: x => x.OrderBy(y => y.Id) + .Contains(resourceTitle.ToNormalString()) + ); + + // + return result; + } + + private async Task> ExtractStrings( + object owner, + object identifier, + CancellationToken cancellationToken = default + ) + { + // + var resourceTitle = GetResourceTitle( + owner: owner, + identifier: identifier + ); + + // + var result = await ExtractStrings( + resourceTitle: resourceTitle, + cancellationToken: cancellationToken ); //