-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
vahid
committed
Dec 16, 2024
1 parent
cb4def3
commit 884aae5
Showing
6 changed files
with
63 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
Tests/Jinget.Core.Tests/Utilities/ResponseResultUtilityTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |