diff --git a/dotnet-client-libraries/HowToUseMapster/.config/dotnet-tools.json b/dotnet-client-libraries/HowToUseMapster/.config/dotnet-tools.json index 0c61f2d390..16b3b8353f 100644 --- a/dotnet-client-libraries/HowToUseMapster/.config/dotnet-tools.json +++ b/dotnet-client-libraries/HowToUseMapster/.config/dotnet-tools.json @@ -3,7 +3,7 @@ "isRoot": true, "tools": { "mapster.tool": { - "version": "8.3.0", + "version": "10.0.11", "commands": [ "dotnet-mapster" ] diff --git a/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/Config/MapsterConfig.cs b/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/Config/MapsterConfig.cs index 5a32078817..058c3a6ae0 100644 --- a/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/Config/MapsterConfig.cs +++ b/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/Config/MapsterConfig.cs @@ -13,7 +13,8 @@ public static void RegisterMapsterConfiguration(this IServiceCollection services .NewConfig() .Map(dest => dest.FullName, src => $"{src.Title} {src.FirstName} {src.LastName}") .Map(dest => dest.Age, - src => DateTime.Now.Year - src.DateOfBirth.Value.Year, + src => CalculateAge(DateOnly.FromDateTime(src.DateOfBirth!.Value), + DateOnly.FromDateTime(DateTime.UtcNow)), srcCond => srcCond.DateOfBirth.HasValue) .Map(dest => dest.FullAddress, src => $"{src.Address.Street} {src.Address.PostCode} - {src.Address.City}"); @@ -28,5 +29,17 @@ public static void RegisterMapsterConfiguration(this IServiceCollection services TypeAdapterConfig.GlobalSettings.Scan(Assembly.GetExecutingAssembly()); } + + public static int CalculateAge(DateOnly birthDate, DateOnly today) + { + var age = today.Year - birthDate.Year; + + if (birthDate > today.AddYears(-age)) + { + age--; + } + + return age; + } } } diff --git a/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/HowToUseMapster.csproj b/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/HowToUseMapster.csproj index cc3807f8f2..69aa2d907d 100644 --- a/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/HowToUseMapster.csproj +++ b/dotnet-client-libraries/HowToUseMapster/HowToUseMapster/HowToUseMapster.csproj @@ -1,14 +1,14 @@ - net6.0 + net10.0 enable enable - - + + diff --git a/dotnet-client-libraries/HowToUseMapster/Tests/Tests.cs b/dotnet-client-libraries/HowToUseMapster/Tests/Tests.cs index 44adfd8a62..d49224ad7f 100644 --- a/dotnet-client-libraries/HowToUseMapster/Tests/Tests.cs +++ b/dotnet-client-libraries/HowToUseMapster/Tests/Tests.cs @@ -1,5 +1,7 @@ using HowToUseMapster; +using HowToUseMapster.Config; using HowToUseMapster.Data; +using System; using System.Collections.Generic; using System.Linq; using Xunit; @@ -46,5 +48,27 @@ public void WhenMappingPersonDtoToPersonEntity_ThenEntityNotNullAndFirstNameIsEq Assert.NotNull(entity); Assert.Equal(_person.FirstName, entity.FirstName); } + + [Fact] + public void GivenBirthdayAlreadyPassedThisYear_WhenCalculateAge_ThenReturnsFullYears() + { + var birthDate = new DateOnly(1990, 1, 1); + var today = new DateOnly(2026, 8, 15); + + var age = MapsterConfig.CalculateAge(birthDate, today); + + Assert.Equal(36, age); + } + + [Fact] + public void GivenLeapDayBirthdayInNonLeapYear_WhenCalculateAgeBeforeMarch_ThenBirthdayNotYetCounted() + { + var birthDate = new DateOnly(2000, 2, 29); + var today = new DateOnly(2025, 2, 28); + + var age = MapsterConfig.CalculateAge(birthDate, today); + + Assert.Equal(24, age); + } } } \ No newline at end of file diff --git a/dotnet-client-libraries/HowToUseMapster/Tests/Tests.csproj b/dotnet-client-libraries/HowToUseMapster/Tests/Tests.csproj index 4f713e7ade..687272c08d 100644 --- a/dotnet-client-libraries/HowToUseMapster/Tests/Tests.csproj +++ b/dotnet-client-libraries/HowToUseMapster/Tests/Tests.csproj @@ -1,20 +1,21 @@ - net6.0 + net10.0 enable false + true - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all