From 2729184a74a9422816db504f762a9bea16150779 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Wed, 27 Sep 2023 15:00:54 +0330 Subject: [PATCH] add more usable methods --- ...rvices.ContentsMicroservice.Clients.csproj | 2 +- .../Helpers/ContentLanguageHelper.cs | 100 +++++++++++++++++- 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj index 3e66a19..deed7b5 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj @@ -5,7 +5,7 @@ AnyCPU;x64;x86 EasyMicroservices true - 0.0.0.11 + 0.0.0.12 client generated code. EasyMicroservices@gmail.com microservice,Content,Contents,client diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs index 329e571..9ff4095 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs @@ -33,6 +33,24 @@ string GetUniqueIdentity(object contract) return uniqueIdentity.GetValue(contract) as string; } + /// + /// + /// + /// + /// + /// + public async Task ResolveContentLanguage(IEnumerable items, string language) + { + if (items == null) + return; + List tasks = new List(); + foreach (var item in items) + { + tasks.Add(ResolveContentLanguage(item, language, new HashSet())); + } + await Task.WhenAll(tasks); + } + /// /// /// @@ -86,6 +104,80 @@ bool IsClass(Type type) return type.GetTypeInfo().IsClass && typeof(string) != type && typeof(char[]) != type; } + /// + /// + /// + /// + /// + public Task AddToContentLanguage(object item) + { + return SaveToContentLanguage(item, AddToContent); + } + + /// + /// + /// + /// + /// + public Task UpdateToContentLanguage(object item) + { + return SaveToContentLanguage(item, UpdateToContent); + } + + async Task SaveToContentLanguage(object item, Func<(string UniqueRecordId, string Name, IEnumerable Languages)[], Task> saveData) + { + if (item.Equals(default)) + return new CategoryContractMessageContract() + { + IsSuccess = true, + }; + string uniqueRecordId = default; + + var uidProperty = item.GetType().GetProperty("UniqueRecordId", BindingFlags.Instance | BindingFlags.Public); + if (uidProperty != null) + uniqueRecordId = uidProperty.GetValue(item) as string; + var request = new List<(string UniqueRecordId, string Name, IEnumerable Languages)>(); + foreach (var property in item.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) + { + if (property.GetCustomAttribute() != null) + { + if (property.GetValue(item) is IEnumerable items) + { + string propertyName = property.Name; + request.Add((uniqueRecordId, propertyName, Map(items))); + } + } + } + var result = await saveData(request.ToArray()); + if (!result.IsSuccess) + return result; + return new CategoryContractMessageContract() + { + IsSuccess = true, + }; + } + + + IEnumerable Map(IEnumerable objects) + { + if (objects != null) + { + foreach (var item in objects) + { + var languageProperty = item.GetType().GetProperty(nameof(LanguageDataContract.Language), BindingFlags.Public | BindingFlags.Instance); + if (languageProperty == null) + throw new Exception($"Property {nameof(LanguageDataContract.Language)} not found in type {item.GetType()}"); + var dataProperty = item.GetType().GetProperty(nameof(LanguageDataContract.Data), BindingFlags.Public | BindingFlags.Instance); + if (dataProperty == null) + throw new Exception($"Property {nameof(LanguageDataContract.Data)} not found in type {item.GetType()}"); + yield return new LanguageDataContract() + { + Language = languageProperty.GetValue(item) as string, + Data = dataProperty.GetValue(item) as string, + }; + } + } + } /// /// /// @@ -93,7 +185,7 @@ bool IsClass(Type type) /// /// /// - public async Task AddToContent(string uniqueRecordId, string name, IEnumerable languages) + async Task AddToContent(string uniqueRecordId, string name, IEnumerable languages) { var addNames = await _contentClient.AddContentWithKeyAsync(new Contents.GeneratedServices.AddContentWithKeyRequestContract { @@ -108,7 +200,7 @@ public async Task AddToContent(string uniqueRec /// /// /// - public async Task AddToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) + async Task AddToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) { CategoryContractMessageContract result = default; foreach (var item in items) @@ -127,7 +219,7 @@ public async Task AddToContent(params (string U /// /// /// - public async Task UpdateToContent(string uniqueRecordId, string name, IEnumerable languages) + async Task UpdateToContent(string uniqueRecordId, string name, IEnumerable languages) { var addNames = await _contentClient.AddContentWithKeyAsync(new Contents.GeneratedServices.AddContentWithKeyRequestContract { @@ -142,7 +234,7 @@ public async Task UpdateToContent(string unique /// /// /// - public async Task UpdateToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) + async Task UpdateToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) { CategoryContractMessageContract result = default; foreach (var item in items)