From d23a15d31eafa002772c1b3838065585b9d67d7b Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Wed, 27 Sep 2023 16:49:28 +0330 Subject: [PATCH] Add support for ResolveContentAllLanguage --- .../MicroserviceClientTest.cs | 1 + ...rvices.ContentsMicroservice.Clients.csproj | 2 +- .../Helpers/ContentLanguageHelper.cs | 48 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs index 1ed4a0a..5637e5a 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs @@ -45,6 +45,7 @@ async Task OnInitialize() [Fact] public async Task GetAllTestTest() { + var instance = Activator.CreateInstance(typeof(List)); //await OnInitialize(); //var microserviceClient = new Contents.GeneratedServices.MicroserviceClient(_routeAddress, HttpClient); //var microservices = await microserviceClient.GetAllAsync(); diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj index deed7b5..70a77c9 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.12 + 0.0.0.13 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 9ff4095..24fbfde 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs @@ -33,6 +33,54 @@ string GetUniqueIdentity(object contract) return uniqueIdentity.GetValue(contract) as string; } + /// + /// + /// + /// + /// + public async Task ResolveContentAllLanguage(object contract) + { + if (contract == null) + return; + string uniqueRecordId = default; + + var uidProperty = contract.GetType().GetProperty("UniqueRecordId", BindingFlags.Instance | BindingFlags.Public); + if (uidProperty != null) + uniqueRecordId = uidProperty.GetValue(contract) as string; + foreach (var property in contract.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) + { + if (property.GetCustomAttribute() != null) + { + var instance = Activator.CreateInstance(property.PropertyType); + if (instance is IList list) + { + var genericType = property.PropertyType.GetGenericArguments()[0]; + var contents = await _contentClient.GetAllByKeyAsync(new GetAllByKeyRequestContract + { + Key = $"{uniqueRecordId}-{property.Name}" + }); + if (contents.IsSuccess) + { + foreach (var item in contents.Result) + { + var itemInstance = Activator.CreateInstance(genericType); + var languageProperty = itemInstance.GetType().GetProperty(nameof(LanguageDataContract.Language), BindingFlags.Public | BindingFlags.Instance); + if (languageProperty == null) + throw new Exception($"Property {nameof(LanguageDataContract.Language)} not found in type {itemInstance.GetType()}"); + var dataProperty = itemInstance.GetType().GetProperty(nameof(LanguageDataContract.Data), BindingFlags.Public | BindingFlags.Instance); + if (dataProperty == null) + throw new Exception($"Property {nameof(LanguageDataContract.Data)} not found in type {itemInstance.GetType()}"); + languageProperty.SetValue(itemInstance, item.Language.Name); + dataProperty.SetValue(itemInstance, item.Data); + list.Add(itemInstance); + } + } + } + property.SetValue(contract, instance); + } + } + } + /// /// ///