生成第一个/下一个/上一个/最后一个链接,其中 api 仅提供偏移量

我正在尝试编写一些带有offset值的代码,然后生成第一个/下一个/上一个/最后一个链接。


我目前有类似下面的内容,但一直在考虑边缘情况,我的计算结果出现了偏差。我想有人一定已经在某个地方做过这个,但找不到可以查看/使用的库。


public class PagedItems<T>

{

    public PagedItems()

    {

    }


    public PagedItems(int offset, int totalSize, IEnumerable<T> subset)

    {

        this.Offset = offset;

        this.TotalSize = totalSize;

        this.Data = subset;

        this.Links = new Dictionary<string, Uri>();


        var pageCount = (totalSize - offset) / 20;


        if (offset > 20)

        {

            this.Links.Add("prev", new Uri($"/accounts?skip={offset - 20}"));

        }


        if (offset < (totalSize - offset))

        {

            this.Links.Add("last", new Uri($"/accounts/skip={totalSize - 20}"));

        }

    }


    public int Offset { get; }


    public int Size => this.Data.Count();


    public int TotalSize { get; }


    public IEnumerable<T> Data { get; }


    public Dictionary<string, Uri> Links { get; }

}


慕森卡
浏览 76回答 1
1回答

明月笑刀无情

对于任何想要这个的人,我可以使用此代码和围绕逻辑进行单元测试:public class PagedItems<T>{&nbsp; &nbsp; private readonly int pageSize;&nbsp; &nbsp; public PagedItems()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public PagedItems(int offset, int pageSize, int totalSize, IEnumerable<T> subset, Uri baseUri)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.Offset = offset;&nbsp; &nbsp; &nbsp; &nbsp; this.pageSize = pageSize;&nbsp; &nbsp; &nbsp; &nbsp; this.TotalSize = totalSize;&nbsp; &nbsp; &nbsp; &nbsp; this.Data = subset;&nbsp; &nbsp; &nbsp; &nbsp; this.Links = new Dictionary<string, string>();&nbsp; &nbsp; &nbsp; &nbsp; if (ShouldDisplayPrevAndFirstLink(offset))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var linkValue = Math.Max(offset - pageSize, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link = QueryHelpers.AddQueryString(baseUri.OriginalString, "skip", linkValue.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Links.Add("prev", link);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (ShouldDisplayNextAndLastLink(offset, totalSize, pageSize))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var linkValue = offset + pageSize;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link = QueryHelpers.AddQueryString(baseUri.OriginalString, "skip", linkValue.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Links.Add("next", link);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (ShouldDisplayPrevAndFirstLink(offset))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link = QueryHelpers.AddQueryString(baseUri.OriginalString, "skip", "0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Links.Add("first", link);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (ShouldDisplayNextAndLastLink(offset, totalSize, pageSize))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var linkValue = totalSize - pageSize;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var link = QueryHelpers.AddQueryString(baseUri.OriginalString, "skip", linkValue.ToString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Links.Add("last", link);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static bool ShouldDisplayNextAndLastLink(int offset, int totalSize, int pageSize) => offset < totalSize - pageSize;&nbsp; &nbsp; private static bool ShouldDisplayPrevAndFirstLink(int offset) => offset > 0;&nbsp; &nbsp; public int Offset { get; }&nbsp; &nbsp; public int Size => this.Data.Count();&nbsp; &nbsp; public int TotalSize { get; }&nbsp; &nbsp; public IEnumerable<T> Data { get; }&nbsp; &nbsp; public Dictionary<string, string> Links { get; }}
打开App,查看更多内容
随时随地看视频慕课网APP