Ejemplo n.º 1
0
        protected Page PageResults(int total, int ordinal = -1)
        {
            var pageSize = RequestContext.ContainsHeader(WebHeaders.PerPage) ? int.Parse(RequestContext.GetHeaderValue(WebHeaders.PerPage)) : 10;
            //TODO: need to populate according to specific device settings

            int start = 1, end = total;

            if (!RequestContext.ContainsHeader(WebHeaders.Page) && !RequestContext.ContainsHeader(WebHeaders.PageForId))
            {
                ResponseContext.SetProperties(Request.Properties, WebHeaders.AcceptRanges, WebHeaders.Page);
            }
            else
            {
                var maxPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
                if (Request != null)
                {
                    ResponseContext.SetProperties(Request.Properties, WebHeaders.MaxPage, maxPage.ToString());
                }
                var page = -1;

                if (ordinal > 0)
                {
                    var previousPage = (ordinal % pageSize == 0) ? (ordinal / pageSize) - 1 : ordinal / pageSize;
                    page = previousPage + 1;

                    start = previousPage * pageSize + 1;
                    end   = (start + pageSize - 1) <= total ? (start + pageSize - 1) : total;
                }
                else
                {
                    page = int.Parse(RequestContext.GetHeaderValue(WebHeaders.Page));

                    if (page <= maxPage)
                    {
                        start = ((page - 1) * pageSize) + 1;
                        end   = page * pageSize <= total ? page * pageSize : total;
                    }
                    else
                    {
                        start = 0;
                        end   = 0;
                    }
                }

                if (start > 0 && end > 0 && page > 0)
                {
                    if (Request != null)
                    {
                        ResponseContext.SetProperties(Request.Properties, WebHeaders.ContentRange, string.Format("{0}-{1}/{2}", start, end, total));
                        ResponseContext.SetProperties(Request.Properties, WebHeaders.PreviousPage, (start == 1 ? 0 : page - 1).ToString());
                        ResponseContext.SetProperties(Request.Properties, WebHeaders.NextPage, (end < total ? page + 1 : 0).ToString());
                    }
                }
            }

            return(new Page()
            {
                Start = start, End = end
            });
        }
Ejemplo n.º 2
0
 protected void AppendLocation(string resourceLocation, int resourceId)
 {
     if (resourceId > 0)
     {
         ResponseContext.SetProperties(Request.Properties, WebHeaders.Location, Request.RequestUri.AbsoluteUri + resourceLocation + resourceId);
     }
     else
     {
         throw new WebApiException(ErrorList.UnableToProcess, System.Net.HttpStatusCode.NotAcceptable);
     }
 }