Skip to content

Commit

Permalink
Merge pull request #24 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Fix uniqueidentity content resolver
  • Loading branch information
Ali-YousefiTelori authored Sep 28, 2023
2 parents ecfb580 + eafae23 commit c17b0c1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ServiceLanguageContract> Titles { get; set; }
public PostContract Post { get; set; }
public List<PostContract> Posts { get; set; }
public string UniqueIdentity { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.13</Version>
<Version>0.0.0.14</Version>
<Description>client generated code.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>microservice,Content,Contents,client</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContentLanguageAttribute>() != null)
Expand All @@ -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)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ async Task ResolveContentLanguage(object contract, string language, HashSet<obje
return;
var type = contract.GetType();
mappedItems.Add(contract);
foreach (var property in contract.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
foreach (var property in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (property.GetCustomAttribute<ContentLanguageAttribute>() != null)
{
Expand Down Expand Up @@ -172,27 +172,27 @@ public Task<CategoryContractMessageContract> UpdateToContentLanguage(object item
return SaveToContentLanguage(item, UpdateToContent);
}

async Task<CategoryContractMessageContract> SaveToContentLanguage(object item, Func<(string UniqueRecordId, string Name, IEnumerable<LanguageDataContract> Languages)[], Task<CategoryContractMessageContract>> saveData)
async Task<CategoryContractMessageContract> SaveToContentLanguage(object item, Func<(string UniqueIdentity, string Name, IEnumerable<LanguageDataContract> Languages)[], Task<CategoryContractMessageContract>> 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<LanguageDataContract> Languages)>();
uniqueIdentity = uidProperty.GetValue(item) as string;
var request = new List<(string UniqueIdentity, string Name, IEnumerable<LanguageDataContract> Languages)>();
foreach (var property in item.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (property.GetCustomAttribute<ContentLanguageAttribute>() != null)
{
if (property.GetValue(item) is IEnumerable items)
{
string propertyName = property.Name;
request.Add((uniqueRecordId, propertyName, Map(items)));
request.Add((uniqueIdentity, propertyName, Map(items)));
}
}
}
Expand Down Expand Up @@ -229,15 +229,15 @@ IEnumerable<LanguageDataContract> Map(IEnumerable objects)
/// <summary>
///
/// </summary>
/// <param name="uniqueRecordId"></param>
/// <param name="uniqueIdentity"></param>
/// <param name="name"></param>
/// <param name="languages"></param>
/// <returns></returns>
async Task<CategoryContractMessageContract> AddToContent(string uniqueRecordId, string name, IEnumerable<LanguageDataContract> languages)
async Task<CategoryContractMessageContract> AddToContent(string uniqueIdentity, string name, IEnumerable<LanguageDataContract> languages)
{
var addNames = await _contentClient.AddContentWithKeyAsync(new Contents.GeneratedServices.AddContentWithKeyRequestContract
{
Key = $"{uniqueRecordId}-{name}",
Key = $"{uniqueIdentity}-{name}",
LanguageData = languages.ToList(),
});
return addNames;
Expand All @@ -248,12 +248,12 @@ async Task<CategoryContractMessageContract> AddToContent(string uniqueRecordId,
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
async Task<CategoryContractMessageContract> AddToContent(params (string UniqueRecordId, string Name, IEnumerable<LanguageDataContract> Languages)[] items)
async Task<CategoryContractMessageContract> AddToContent(params (string UniqueIdentity, string Name, IEnumerable<LanguageDataContract> 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;
}
Expand All @@ -263,31 +263,47 @@ async Task<CategoryContractMessageContract> AddToContent(params (string UniqueRe
/// <summary>
///
/// </summary>
/// <param name="uniqueRecordId"></param>
/// <param name="uniqueIdentity"></param>
/// <param name="name"></param>
/// <param name="languages"></param>
/// <returns></returns>
async Task<CategoryContractMessageContract> UpdateToContent(string uniqueRecordId, string name, IEnumerable<LanguageDataContract> languages)
async Task<CategoryContractMessageContract> UpdateToContent(string uniqueIdentity, string name, IEnumerable<LanguageDataContract> 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);
}

/// <summary>
///
/// </summary>
/// <param name="items"></param>
/// <returns></returns>
async Task<CategoryContractMessageContract> UpdateToContent(params (string UniqueRecordId, string Name, IEnumerable<LanguageDataContract> Languages)[] items)
async Task<CategoryContractMessageContract> UpdateToContent(params (string UniqueIdentity, string Name, IEnumerable<LanguageDataContract> 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;
}
Expand Down

0 comments on commit c17b0c1

Please sign in to comment.