From c26c479334eee6bbaa6f396d4fde4b1e175b85a7 Mon Sep 17 00:00:00 2001 From: Volodymyr Lisovskyi Date: Tue, 21 May 2024 16:09:53 +0200 Subject: [PATCH] feat: for last page add calculate if first page starting at 1 --- src/Sinch/Core/Utils.cs | 21 +++++++++++++++++++-- tests/Sinch.Tests/UtilsTests.cs | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Sinch/Core/Utils.cs b/src/Sinch/Core/Utils.cs index c65b337..b9fb55a 100644 --- a/src/Sinch/Core/Utils.cs +++ b/src/Sinch/Core/Utils.cs @@ -73,9 +73,20 @@ public static T ParseEnum(string value) throw new InvalidOperationException($"Failed to parse {enumType.Name} enum for value {value}"); } - public static bool IsLastPage(int page, int pageSize, int totalCount) + public static bool IsLastPage(int page, int pageSize, int totalCount, PageStart pageStart = PageStart.Zero) { - return (page + 1) * pageSize >= totalCount; + switch (pageStart) + { + case PageStart.Zero: + page += 1; + break; + case PageStart.First: + break; + default: + throw new ArgumentOutOfRangeException(nameof(pageStart), pageStart, null); + } + + return page * pageSize >= totalCount; } public static string ToSnakeCaseQueryString(T obj) where T : class @@ -178,4 +189,10 @@ public static void ThrowNullDeserialization(Type type) throw new InvalidOperationException($"{type.Name} deserialization result is null"); } } + + internal enum PageStart + { + Zero = 0, + First = 1, + } } diff --git a/tests/Sinch.Tests/UtilsTests.cs b/tests/Sinch.Tests/UtilsTests.cs index 282b166..02806d8 100644 --- a/tests/Sinch.Tests/UtilsTests.cs +++ b/tests/Sinch.Tests/UtilsTests.cs @@ -25,6 +25,22 @@ public void NotLastPage() Utils.IsLastPage(4, 1, 6).Should().BeFalse(); } + [Fact] + public void LastPageFirst() + { + Utils.IsLastPage(1, 10, 2, PageStart.First).Should().BeTrue(); + Utils.IsLastPage(1, 10, 9, PageStart.First).Should().BeTrue(); + Utils.IsLastPage(4, 1, 4, PageStart.First).Should().BeTrue(); + } + + [Fact] + public void NotLastPageFirst() + { + Utils.IsLastPage(1, 10, 12, PageStart.First).Should().BeFalse(); + Utils.IsLastPage(1, 10, 11, PageStart.First).Should().BeFalse(); + Utils.IsLastPage(4, 1, 6, PageStart.First).Should().BeFalse(); + } + class Root {