Malik Kashmiri
Malik Kashmiri

Reputation: 5851

Pagination to return total count and total pages

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; }
}

Upvotes: 3

Views: 8187

Answers (1)

Malik Kashmiri
Malik Kashmiri

Reputation: 5851

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

Upvotes: 3

Related Questions