From eafae23cedb6596de21a0a27417221f9c1f6cdb5 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Thu, 28 Sep 2023 15:13:19 +0330 Subject: [PATCH] Fix uniqueidentity content resolver --- .../ContentLanguageHelperTest.cs | 45 ++++++++++++++ .../Common/PersonLanguageContract.cs | 24 ++++++++ .../MicroserviceClientTest.cs | 2 + ...rvices.ContentsMicroservice.Clients.csproj | 2 +- .../Helpers/ContentLanguageHelper.cs | 58 ++++++++++++------- 5 files changed, 109 insertions(+), 22 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/Contracts/Common/PersonLanguageContract.cs diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/ContentLanguageHelperTest.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/ContentLanguageHelperTest.cs index 3505885..03e56e6 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/ContentLanguageHelperTest.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/ContentLanguageHelperTest.cs @@ -52,6 +52,36 @@ async Task OnInitialize() Content-Length: 0 {""result"":{""Data"": ""Hello My Content Language""},""isSuccess"":true,""error"":null}"); + + resourceManager.Append(@$"POST /api/Content/GetAllByKey HTTP/1.1 +Host: localhost:{Port} +Accept: text/plain*RequestSkipBody* + +{{""key"":""1-1-Title""}}" + , + @"HTTP/1.1 200 OK +Content-Type: application/json; charset=utf-8 +Content-Length: 0 + +{ + ""result"": [ + { + ""Data"": ""Hello My Title Language"", + ""Language"": { + ""Name"": ""en-US"" + } + }, + { + ""Data"": ""persian hi"", + ""Language"": { + ""Name"": ""fa-IR"" + } + } + ], + ""isSuccess"": true, + ""error"": null +}"); + } finally { @@ -103,5 +133,20 @@ public async Task ContentLanguagePersonTest() Assert.NotEmpty(personContract.Posts[0].Title); Assert.NotEmpty(personContract.Posts[0].Content); } + + [Fact] + public async Task ContentAllLanguagePersonTest() + { + await OnInitialize(); + + PersonLanguageContract personContract = new PersonLanguageContract(); + personContract.UniqueIdentity = "1-1"; + + ContentLanguageHelper contentLanguageHelper = new ContentLanguageHelper(new Contents.GeneratedServices.ContentClient(_routeAddress, HttpClient)); + await contentLanguageHelper.ResolveContentAllLanguage(personContract); + + Assert.NotEmpty(personContract.Titles); + Assert.True(personContract.Titles.Count > 1); + } } } diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/Contracts/Common/PersonLanguageContract.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/Contracts/Common/PersonLanguageContract.cs new file mode 100644 index 0000000..125f111 --- /dev/null +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/Contracts/Common/PersonLanguageContract.cs @@ -0,0 +1,24 @@ +using Contents.GeneratedServices; +using EasyMicroservices.ContentsMicroservice.Clients.Attributes; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EasyMicroservices.ContentsMicroservice.Clients.Tests.Contracts.Common +{ + public class ServiceLanguageContract + { + public string Language { get; set; } + public string Data { get; set; } + } + public class PersonLanguageContract + { + [ContentLanguage] + public List Titles { get; set; } + public PostContract Post { get; set; } + public List Posts { get; set; } + public string UniqueIdentity { get; set; } + } +} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs index 5637e5a..7669c0c 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients.Tests/MicroserviceClientTest.cs @@ -1,5 +1,7 @@ +using Contents.GeneratedServices; using EasyMicroservices.Laboratory.Engine; using EasyMicroservices.Laboratory.Engine.Net.Http; +using Newtonsoft.Json; namespace EasyMicroservices.ContentsMicroservice.Clients.Tests { diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/EasyMicroservices.ContentsMicroservice.Clients.csproj index 70a77c9..a6ba5ef 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.13 + 0.0.0.14 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 24fbfde..b96e262 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Clients/Helpers/ContentLanguageHelper.cs @@ -42,11 +42,11 @@ public async Task ResolveContentAllLanguage(object contract) { if (contract == null) return; - string uniqueRecordId = default; + string uniqueIdentity = default; - var uidProperty = contract.GetType().GetProperty("UniqueRecordId", BindingFlags.Instance | BindingFlags.Public); + var uidProperty = contract.GetType().GetProperty("UniqueIdentity", BindingFlags.Instance | BindingFlags.Public); if (uidProperty != null) - uniqueRecordId = uidProperty.GetValue(contract) as string; + uniqueIdentity = uidProperty.GetValue(contract) as string; foreach (var property in contract.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { if (property.GetCustomAttribute() != null) @@ -57,7 +57,7 @@ public async Task ResolveContentAllLanguage(object contract) var genericType = property.PropertyType.GetGenericArguments()[0]; var contents = await _contentClient.GetAllByKeyAsync(new GetAllByKeyRequestContract { - Key = $"{uniqueRecordId}-{property.Name}" + Key = $"{uniqueIdentity}-{GetPropertyName(property.Name)}" }); if (contents.IsSuccess) { @@ -116,7 +116,7 @@ async Task ResolveContentLanguage(object contract, string language, HashSet() != null) { @@ -172,19 +172,19 @@ public Task UpdateToContentLanguage(object item return SaveToContentLanguage(item, UpdateToContent); } - async Task SaveToContentLanguage(object item, Func<(string UniqueRecordId, string Name, IEnumerable Languages)[], Task> saveData) + async Task SaveToContentLanguage(object item, Func<(string UniqueIdentity, string Name, IEnumerable Languages)[], Task> saveData) { if (item.Equals(default)) return new CategoryContractMessageContract() { IsSuccess = true, }; - string uniqueRecordId = default; + string uniqueIdentity = default; - var uidProperty = item.GetType().GetProperty("UniqueRecordId", BindingFlags.Instance | BindingFlags.Public); + var uidProperty = item.GetType().GetProperty("UniqueIdentity", BindingFlags.Instance | BindingFlags.Public); if (uidProperty != null) - uniqueRecordId = uidProperty.GetValue(item) as string; - var request = new List<(string UniqueRecordId, string Name, IEnumerable Languages)>(); + uniqueIdentity = uidProperty.GetValue(item) as string; + var request = new List<(string UniqueIdentity, string Name, IEnumerable Languages)>(); foreach (var property in item.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)) { if (property.GetCustomAttribute() != null) @@ -192,7 +192,7 @@ async Task SaveToContentLanguage(object item, F if (property.GetValue(item) is IEnumerable items) { string propertyName = property.Name; - request.Add((uniqueRecordId, propertyName, Map(items))); + request.Add((uniqueIdentity, propertyName, Map(items))); } } } @@ -229,15 +229,15 @@ IEnumerable Map(IEnumerable objects) /// /// /// - /// + /// /// /// /// - async Task AddToContent(string uniqueRecordId, string name, IEnumerable languages) + async Task AddToContent(string uniqueIdentity, string name, IEnumerable languages) { var addNames = await _contentClient.AddContentWithKeyAsync(new Contents.GeneratedServices.AddContentWithKeyRequestContract { - Key = $"{uniqueRecordId}-{name}", + Key = $"{uniqueIdentity}-{name}", LanguageData = languages.ToList(), }); return addNames; @@ -248,12 +248,12 @@ async Task AddToContent(string uniqueRecordId, /// /// /// - async Task AddToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) + async Task AddToContent(params (string UniqueIdentity, string Name, IEnumerable Languages)[] items) { CategoryContractMessageContract result = default; foreach (var item in items) { - result = await AddToContent(item.UniqueRecordId, item.Name, item.Languages); + result = await AddToContent(item.UniqueIdentity, item.Name, item.Languages); if (!result.IsSuccess) return result; } @@ -263,31 +263,47 @@ async Task AddToContent(params (string UniqueRe /// /// /// - /// + /// /// /// /// - async Task UpdateToContent(string uniqueRecordId, string name, IEnumerable languages) + async Task UpdateToContent(string uniqueIdentity, string name, IEnumerable languages) { var addNames = await _contentClient.AddContentWithKeyAsync(new Contents.GeneratedServices.AddContentWithKeyRequestContract { - Key = $"{uniqueRecordId}-{name}", + Key = $"{uniqueIdentity}-{name}", LanguageData = languages.ToList(), }); return addNames; } + string GetPropertyName(string name) + { + string addContrent = ""; + int dropLength = 0; + if (name.EndsWith("ses")) + dropLength = 2; + else if (name.EndsWith("s")) + dropLength = 1; + else if (name.EndsWith("Children")) + { + dropLength = 8; + addContrent = "Child"; + } + return string.Concat(name.Substring(0, name.Length - dropLength), addContrent); + } + /// /// /// /// /// - async Task UpdateToContent(params (string UniqueRecordId, string Name, IEnumerable Languages)[] items) + async Task UpdateToContent(params (string UniqueIdentity, string Name, IEnumerable Languages)[] items) { CategoryContractMessageContract result = default; foreach (var item in items) { - result = await UpdateToContent(item.UniqueRecordId, item.Name, item.Languages); + result = await UpdateToContent(item.UniqueIdentity, item.Name, item.Languages); if (!result.IsSuccess) return result; }