Skip to content

Commit

Permalink
ResponseResultUtility.MapTo added
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Dec 16, 2024
1 parent cb4def3 commit 884aae5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions 01-Core/Jinget.Core/Jinget.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="17.11.20" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
20 changes: 10 additions & 10 deletions 01-Core/Jinget.Core/Utilities/Expressions/ExpressionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal static bool TryParseExpression(Expression? expression, out string? path
}

/// <summary>
/// Removes boxing
/// Remove boxing
/// </summary>
internal static Expression RemoveConvert(Expression expression)
{
Expand Down Expand Up @@ -194,18 +194,18 @@ public static Expression<Func<T, T>> CreateMemberInitExpression<T>(string parame
}

/// <summary>
/// Construct a boolean expression based on a json input
/// Construct a boolean expression based on a JSON input
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json">a key/value structured json string/object, where keys are types property and values are their value</param>
/// <param name="json">a key/value structured JSON string/object, where keys are types property and values are their value</param>
/// <param name="treatNullOrEmptyAsTrueCondition">When <paramref name="json"/> is null or empty, then a default condition will be returned.
/// if this parameter's value is set to true the a default true condition will be returned otherwise a defaule false condition will be returned</param>
/// if this parameter's value is set to true, the default true condition will be returned, otherwise a default false condition will be returned</param>
/// <returns></returns>
#nullable enable
public static Expression<Func<T, bool>> ConstructBinaryExpression<T>(object? json, bool treatNullOrEmptyAsTrueCondition = true)
#nullable disable
{
//if there is no json object specified, then return the default true/false condition
//if there is no JSON object specified, then return the default true/false condition
if (json is null)
{
return treatNullOrEmptyAsTrueCondition ? BooleanUtility.TrueCondition<T>() : BooleanUtility.FalseCondition<T>();
Expand All @@ -219,17 +219,17 @@ public static Expression<Func<T, bool>> ConstructBinaryExpression<T>(object? jso
}

/// <summary>
/// Construct a boolean expression based on a json input
/// Construct a boolean expression based on a JSON input
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json">a key/value structured json string/object, where keys are types property and values are their value.
/// <param name="json">a key/value structured JSON string/object, where keys are types property and values are their value.
/// for example:{'name':'vahid','age':'34'} which translates to a dictionary where 'name' and 'age' are keys and their values are 'vahid' and '34'</param>
/// <param name="treatNullOrEmptyAsTrueCondition">When <paramref name="json"/> is null or empty, then a default condition will be returned.
/// if this parameters value is set to true the default true condition will be returned otherwise a default false condition will be returned</param>
/// if this parameter value is set to true, the default true condition will be returned, otherwise a default false condition will be returned</param>
/// <returns></returns>
public static Expression<Func<T, bool>> ConstructBinaryExpression<T>(string json, bool treatNullOrEmptyAsTrueCondition = true)
{
//if there empty json string specified, then return the default true/false condition
//if there empty JSON string specified, then return the default true/false condition
if (string.IsNullOrWhiteSpace(json.ToString()))
{
return treatNullOrEmptyAsTrueCondition ? BooleanUtility.TrueCondition<T>() : BooleanUtility.FalseCondition<T>();
Expand Down Expand Up @@ -324,7 +324,7 @@ public static Expression<Func<T, bool>> CreateEqualCondition<T, TValueType>(stri

private static MemberExpression GetMemberAccessExpression(Expression expression, string members, Type type)
{
List<string> memberList = [.. members.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)];
List<string> memberList = [.. members.Split(["."], StringSplitOptions.RemoveEmptyEntries)];
string firstMember = memberList.First();
MemberExpression memberExpression = CreateMemberAccessExpression(type, expression, firstMember);

Expand Down
17 changes: 17 additions & 0 deletions 01-Core/Jinget.Core/Utilities/ResponseResultUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Mapster;

namespace Jinget.Core.Utilities;

public class ResponseResultUtility
{
/// <summary>
/// Convert ResponseResult<TSource> to ResponseResult<TDestination>
/// </summary>
public static ResponseResult<TDestination> MapTo<TSource, TDestination>(ResponseResult<TSource> input)
{
ResponseResult<TDestination> result = new(
input.Data.Select(x => x.Adapt<TDestination>()), input.EffectedRowsCount);

return result;
}
}
20 changes: 20 additions & 0 deletions Tests/Jinget.Core.Tests/Utilities/ResponseResultUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Jinget.Core.Tests.Utilities;

[TestClass()]
public class ResponseResultUtilityTests
{
[TestMethod()]
public void should_map_source_data_type_to_destination_data_type()
{
var source = new ResponseResult<SampleModel>(
[new SampleModel { Id = 1, Name = "Vahid" }, new SampleModel { Id = 2, Name = "Ali" }], 2);
var result = ResponseResultUtility.MapTo<SampleModel, SampleViewModel>(source);

Assert.AreEqual(result.Data.GetType(), typeof(List<SampleViewModel>));
Assert.AreEqual(result.Data.First().Name, "Vahid");
Assert.AreEqual(result.Data.Last().Name, "Ali");
Assert.IsTrue(result.IsSuccess);
Assert.IsFalse(result.IsFailure);
Assert.AreEqual(result.EffectedRowsCount, 2);
}
}
8 changes: 8 additions & 0 deletions Tests/Jinget.Core.Tests/_BaseData/SampleModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Jinget.Core.Tests._BaseData
{
public class SampleModel
{
public int Id { get; set; }
public string Name { get; set; }
}
}
7 changes: 7 additions & 0 deletions Tests/Jinget.Core.Tests/_BaseData/SampleViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Jinget.Core.Tests._BaseData
{
public class SampleViewModel
{
public string Name { get; set; }
}
}

0 comments on commit 884aae5

Please sign in to comment.