Skip to content

Commit

Permalink
feat: for last page add calculate if first page starting at 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dovchik committed May 21, 2024
1 parent 4d4bb59 commit c26c479
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Sinch/Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,20 @@ public static T ParseEnum<T>(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>(T obj) where T : class
Expand Down Expand Up @@ -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,
}
}
16 changes: 16 additions & 0 deletions tests/Sinch.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit c26c479

Please sign in to comment.