Added unit tests for the new pagination helper, got it working now

This commit is contained in:
tidusjar 2019-04-16 22:43:24 +01:00
commit 68ea440dbb
4 changed files with 138 additions and 10797 deletions

View file

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ombi.Helpers
{
public static class PaginationHelper
{
public static List<PagesToLoad> GetNextPages(int currentlyLoaded, int toTake, int maxItemsPerPage)
{
var result = new List<PagesToLoad>();
var firstPage = currentlyLoaded / maxItemsPerPage + 1;
var startPos = currentlyLoaded % maxItemsPerPage + 1;
var lastItemIndex = currentlyLoaded + toTake - 1;
var lastPage = lastItemIndex / maxItemsPerPage + 1;
var stopPos = lastItemIndex % maxItemsPerPage + 1;
if (currentlyLoaded > maxItemsPerPage)
{
currentlyLoaded = currentlyLoaded - maxItemsPerPage;
}
var page1 = new PagesToLoad {Page = firstPage, Skip = currentlyLoaded, Take = toTake};
if (toTake + startPos - 1 > maxItemsPerPage)
{
page1.Take = maxItemsPerPage - startPos + 1;
result.Add(page1);
for (var i = firstPage + 1; i < lastPage; i++)
{
var nextPage = new PagesToLoad {Page = i, Skip = 0, Take = maxItemsPerPage};
result.Add(nextPage);
}
var pageN = new PagesToLoad {Page = lastPage, Skip = 0, Take = stopPos};
result.Add(pageN);
}
else
{
result.Add(page1);
}
return result;
}
}
public class PagesToLoad
{
public int Page { get; set; }
public int Take { get; set; }
public int Skip { get; set; }
}
}

View file

@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Ombi.Helpers
{
public static class PaginationHelper
{
public static List<PagesToLoad> GetNextPages(int currentlyLoaded, int toLoad, int maxItemsPerPage)
{
var pagesToLoad = new List<PagesToLoad>();
if (currentlyLoaded == maxItemsPerPage)
{
currentlyLoaded++;
}
var a = currentlyLoaded / maxItemsPerPage;
//var currentPage = Convert.ToInt32(Math.Round((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero));
var currentPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)currentlyLoaded / (decimal)maxItemsPerPage)));
if (currentlyLoaded < maxItemsPerPage)
{
currentPage = 1;
}
var toBeLoaded = (currentlyLoaded + toLoad)+1;
//var toBeLoadedPage = Convert.ToInt32(Math.Round((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage), 2, MidpointRounding.AwayFromZero));
var toBeLoadedPage = Convert.ToInt32(Math.Ceiling((decimal)((decimal)toBeLoaded / (decimal)maxItemsPerPage)));
if (currentlyLoaded == 0)
{
// If we have not loaded any yet, then we should only load
// the first page
currentPage = toBeLoadedPage;
}
var extraPageNeeded = (toBeLoadedPage != currentPage);
if(extraPageNeeded)
{
// Add the first page
pagesToLoad.Add(new PagesToLoad
{
Page = currentPage,
Skip = currentlyLoaded
});
// Add extra page
pagesToLoad.Add(new PagesToLoad
{
Page = toBeLoadedPage,
Skip = (currentlyLoaded + toLoad) - maxItemsPerPage,
Take = toLoad
});
}
else
{
pagesToLoad.Add(new PagesToLoad
{
Page = currentPage,
Skip = currentlyLoaded,
Take = toLoad
});
}
return pagesToLoad;
}
}
public class PagesToLoad
{
public int Page { get; set; }
public int Take { get; set; }
public int Skip { get; set; }
}
}