Skip to content

Commit

Permalink
Page data on the server.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosh-hamedani committed Apr 26, 2017
1 parent ce84a13 commit f3a73dc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Controllers/Resources/VehicleQueryResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class VehicleQueryResource
public int? ModelId { get; set; }
public string SortBy { get; set; }
public bool IsSortAscending { get; set; }

public int Page { get; set; }
public byte PageSize { get; set; }
}
}
2 changes: 2 additions & 0 deletions Core/Models/VehicleQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public class VehicleQuery : IQueryObject
public int? ModelId { get; set; }
public string SortBy { get; set; }
public bool IsSortAscending { get; set; }
public int Page { get; set; }
public byte PageSize { get; set; }
}
}
2 changes: 2 additions & 0 deletions Extensions/IQueryObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ public interface IQueryObject
{
string SortBy { get; set; }
bool IsSortAscending { get; set; }
int Page { get; set; }
byte PageSize { get; set; }
}
}
11 changes: 11 additions & 0 deletions Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,16 @@ public static IQueryable<T> ApplyOrdering<T>(this IQueryable<T> query, IQueryObj
else
return query.OrderByDescending(columnsMap[queryObj.SortBy]);
}

public static IQueryable<T> ApplyPaging<T>(this IQueryable<T> query, IQueryObject queryObj)
{
if (queryObj.Page <= 0)
queryObj.Page = 1;

if (queryObj.PageSize <= 0)
queryObj.PageSize = 10;

return query.Skip((queryObj.Page - 1) * queryObj.PageSize).Take(queryObj.PageSize);
}
}
}
2 changes: 2 additions & 0 deletions Persistence/VehicleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public async Task<IEnumerable<Vehicle>> GetVehicles(VehicleQuery queryObj)

query = query.ApplyOrdering(queryObj, columnsMap);

query = query.ApplyPaging(queryObj);

return await query.ToListAsync();
}

Expand Down

0 comments on commit f3a73dc

Please sign in to comment.