diff --git a/NetChris.Core.UnitTests/Paging/MapToTests.cs b/NetChris.Core.UnitTests/Paging/MapToTests.cs new file mode 100644 index 0000000..eaa7b5d --- /dev/null +++ b/NetChris.Core.UnitTests/Paging/MapToTests.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FluentAssertions; +using NetChris.Core.Paging; +using Xunit; + +namespace NetChris.Core.UnitTests.Paging; + +public class MapToTests +{ + private class FromType + { + public string Name { get; set; } = null!; + public int Age { get; set; } + } + + private class ToType + { + public string PersonName { get; set; } = null!; + public TimeSpan Age { get; set; } + } + + + private readonly List _fromTypes = new() + { + new FromType { Name = "Alice", Age = 30 }, + new FromType { Name = "Bob", Age = 25 }, + new FromType { Name = "Charlie", Age = 35 }, + new FromType { Name = "Diana", Age = 28 } + }; + + + private readonly Page _sourcePage; + private readonly Page _targetPage; + + private static ToType MappingFunction(FromType from) + { + return new ToType + { + PersonName = from.Name, + Age = TimeSpan.FromDays(from.Age * 365.25), + }; + } + + public MapToTests() + { + _sourcePage = new Page(_fromTypes, 2, 4, 500); + _targetPage = _sourcePage.MapTo(MappingFunction); + } + + [Fact] + public void TotalPagesShouldBeCorrect() + { + _targetPage.TotalPages.Should().Be(_sourcePage.TotalPages); + } + + [Fact] + public void CurrentPageShouldBeCorrect() + { + _targetPage.CurrentPage.Should().Be(_sourcePage.CurrentPage); + } + + [Fact] + public void PageSizeShouldBeCorrect() + { + _targetPage.PageSize.Should().Be(_sourcePage.PageSize); + } + + [Fact] + public void MappedItemShouldMatch() + { + var charlieFromSourcePage = _sourcePage.Items.ElementAt(2); + var charlieFromTargetPage = _targetPage.Items.ElementAt(2); + + charlieFromTargetPage.PersonName.Should().Be(charlieFromSourcePage.Name); + charlieFromTargetPage.Age.Should().Be( + TimeSpan.FromDays(charlieFromSourcePage.Age * 365.25)); + } +} \ No newline at end of file diff --git a/NetChris.Core/Paging/Extensions.cs b/NetChris.Core/Paging/Extensions.cs new file mode 100644 index 0000000..efa358a --- /dev/null +++ b/NetChris.Core/Paging/Extensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Linq; + +namespace NetChris.Core.Paging; + +/// +/// Extension methods for paging +/// +public static class Extensions +{ + /// + /// Get a page of items of type + /// mapped from a page of items of type + /// + /// The source page + /// The mapping function from to + public static Page MapTo(this Page sourcePage, Func mappingFunction) + { + var resultItems = sourcePage.Items.Select(mappingFunction).ToList(); + + return new Page( + resultItems, + sourcePage.CurrentPage, + sourcePage.PageSize, + sourcePage.TotalItems); + } +} \ No newline at end of file diff --git a/NetChris.Core/Values/SimpleResult.cs b/NetChris.Core/Values/SimpleResult.cs deleted file mode 100644 index 40ab585..0000000 --- a/NetChris.Core/Values/SimpleResult.cs +++ /dev/null @@ -1,43 +0,0 @@ -namespace NetChris.Core.Values; - -/// -/// Lightweight object with an code and message. -/// A glorified key/value pair. -/// -/// -/// -/// Ideal use is in systems and APIs that, for example, return messages -/// or throw errors with unique-ish codes and an explanatory message. -/// -/// -public class SimpleResult -{ - /// - /// Gets the code - /// - /// The result code - public string ResultCode - { - get; - } - - /// - /// Gets the message. - /// - /// The message - public string Message - { - get; - } - - /// - /// Initializes a new instance of the class. - /// - /// The result code - /// The message - public SimpleResult(string resultCode, string message) - { - ResultCode = resultCode; - Message = message; - } -} \ No newline at end of file