Skip to content

Commit

Permalink
feat: implement pagination in crud api controller base
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed May 18, 2022
1 parent 4c1e9e7 commit 73a8e31
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Gateways.Api/Controllers/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class DevicesController : CrudApiControllerBase<Device, DeviceGetModel, D
{
private readonly int maxNumberOfDevicesPerGateway;

public DevicesController(IDeviceService service, IMapper mapper, IConfiguration configuration)
: base(service, mapper, q => q.Include(d => d.Gateway))
public DevicesController(IDeviceService service, IMapper mapper, IConfiguration config)
: base(service, mapper, q => q.Include(d => d.Gateway), q => q.OrderBy(d => d.Vendor))
{
maxNumberOfDevicesPerGateway = int.Parse(configuration[Configs.MaxNumberOfDevicesPerGateway]);
maxNumberOfDevicesPerGateway = config.GetValue(Configs.MaxNumberOfDevicesPerGateway, 10);
}

[HttpPost]
Expand Down
2 changes: 1 addition & 1 deletion Gateways.Api/Controllers/GatewaysController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Gateways.Api.Controllers;
public class GatewaysController : CrudApiControllerBase<Gateway, GatewayGetModel, GatewayGetDetailsModel, GatewayPostModel, GatewayPutModel, string>
{
public GatewaysController(IGatewayService service, IMapper mapper)
: base(service, mapper, q => q.Include(g => g.Devices))
: base(service, mapper, q => q.Include(g => g.Devices), q => q.OrderBy(g => g.Name))
{
}
}
3 changes: 1 addition & 2 deletions Gateways.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
}
},
"AllowedHosts": "*",
"DatabaseName": "GatewayDatabase",
"MaxNumberOfDevicesPerGateway": 10
"DatabaseName": "GatewayDatabase"
}
20 changes: 14 additions & 6 deletions Gateways.Common/Controllers/CrudApiControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,32 @@ namespace Gateways.Common.Controllers;
public class CrudApiControllerBase<TEntity, TGet, TGetDetails, TPost, TPut, TKey> : ApiControllerBase where TEntity : class, IEntity<TKey>
{
private readonly Func<IQueryable<TEntity>, IQueryable<TEntity>>? includer;
private readonly Func<IQueryable<TEntity>, IQueryable<TEntity>>? order;
protected readonly IService<TEntity> service;

public CrudApiControllerBase(
IService<TEntity> service,
IMapper mapper,
Func<IQueryable<TEntity>, IQueryable<TEntity>>? includer = null) : base(mapper)
Func<IQueryable<TEntity>, IQueryable<TEntity>>? includer = null,
Func<IQueryable<TEntity>, IQueryable<TEntity>>? order = null) : base(mapper)
{
this.includer = includer;
this.order = order;
this.service = service;
}

[HttpGet]
public virtual Response<IEnumerable<TGet>> GetAll()
public virtual PaginatedResponse<IEnumerable<TGet>> GetAll([FromQuery] PaginationQueryModel pagination)
{
var models = service
.AsNoTrackingWithIdentityResolution()
.ToList();
return OkResponse<IEnumerable<TGet>>(models);
var skip = pagination.Page * pagination.PageSize;
var take = pagination.PageSize;
var query = service.AsNoTrackingWithIdentityResolution();
if (order != null)
query = order(query);
var models = query.Skip(skip).Take(pagination.PageSize).ToList();
var hasPrevious = skip > 0;
var hasNext = models.Count == pagination.PageSize && query.Skip(skip + take).Any();
return OkResponse<IEnumerable<TGet>>(models, hasPrevious, hasNext);
}

[HttpGet("{id}")]
Expand Down
11 changes: 6 additions & 5 deletions Gateways.Common/Models/PaginationQueryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

namespace Gateways.Common.Models;

public class PaginationQueryModel<T>
public class PaginationQueryModel
{
[FromQuery(Name = "key")]
public T? Key { get; set; } = default;
[Min(0)]
[FromQuery(Name = "page")]
public int Page { get; set; } = default;
[Min(1)]
[FromQuery(Name = "limit")]
public int Limit { get; set; } = 10;
[FromQuery(Name = "pageSize")]
public int PageSize { get; set; } = 10;
}

0 comments on commit 73a8e31

Please sign in to comment.