Pagination to return total count and total pages

asp.net-core c# entity-framework-core pagination

Question

Problem

I am creating an API and apply filter to it to paginate on the query. when send response now I need to send Totalcount and TotalPages with the result. by this method Now I am getting this kind of response in case of success

result

{
  "error": "string",
  "success": "string",
  "statusCode": 0,
  "result": {
      [ {object}, ]
   }
}

Now I need this result instead of above result How can I achieve this need some good suggestion.

Needed result

{
  "error": "string",
  "success": "string",
  "statusCode": 0,
  "result": {
      "TotalPages":"50",
      "TotalCount":"908945",
      "model":[ {object},]
   }
}

interface

public interface IPagedList<T> : IList<T>
{
    int PageIndex { get; }
    int PageSize { get; }
    int TotalCount { get; }
    int TotalPages { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
}

PageList Class

public class PagedList<T> : List<T>, IPagedList<T>
{

    public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
    {
        int total = source.Count();
        this.TotalCount = total;
        this.TotalPages = total / pageSize;

        if (total % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    }


    public PagedList(IList<T> source, int pageIndex, int pageSize)
    {
        TotalCount = source.Count();
        TotalPages = TotalCount / pageSize;

        if (TotalCount % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
    }


    public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
    {
        TotalCount = totalCount;
        TotalPages = TotalCount / pageSize;

        if (TotalCount % pageSize > 0)
            TotalPages++;

        this.PageSize = pageSize;
        this.PageIndex = pageIndex;
        this.AddRange(source);
    }

    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }

    public bool HasPreviousPage
    {
        get { return (PageIndex > 0); }
    }
    public bool HasNextPage
    {
        get { return (PageIndex + 1 < TotalPages); }
    }
}

Service

public IPagedList<Model> SomeMethod(int id, int pageIndex = 0, int pageSize = int.MaxValue)
{
    var query = //select query

    var reslt= new PagedList<Model>(query, pageIndex, pageSize);
    return reslt;


}

API

[HttpGet("{id}")]
public ApiResponse GetApi([FromRoute] int id, int pageIndex = 0, int pageSize = int.MaxValue)
{
    if (id == 0)
        return new ApiResponse(StatusCodes.Status400BadRequest, error: "id must be provided");


    var result = someMethod(id, pageIndex, pageSize);
    return new ApiResponse(StatusCodes.Status200OK, result, "success");
}

ApiResponse

public class ApiResponse
{

    public ApiResponse(int statusCode, object result = null, string success = "", string error = "")
    {
        Error = error;
        Success = success;
        StatusCode = statusCode;
        Result = result;      
    }

    public string Error { get; set; }
    public string Success { get; set; }
    public int StatusCode { get; set; }
    public Object Result { get; set; }
}
1
3
8/23/2017 12:53:03 PM

Accepted Answer

Solution

Just do this and my code woks as I expected

var TotalCount =result.TotalCount
var TotalPages =result.TotalPages
//after getting these value pass in the result
return new ApiResponse(StatusCodes.Status200OK, result: new {result : result, totalCount : TotalCount, totalPages : TotalPages}, "success"); 

because the properties of total pages and total count already in the IPagedList interface and it inherited in PagedList

2
8/24/2017 12:03:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow