Skip to content

Commit 8a79489

Browse files
committed
Added limit & offset query
1 parent 262fae1 commit 8a79489

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Classes/ApiControllerBase.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.EntityFrameworkCore;
66
using NSwag.Annotations;
7+
using ToLiAPI.Api;
78
using ToLiAPI.Models;
89

910
namespace ToLiAPI {
10-
public class ApiController<T> : Controller where T : Entity, new() {
11+
public abstract class ApiController<T> : Controller where T : Entity, new() {
1112
protected readonly DbSet<T> data;
1213
protected readonly ToLiDbContext dataContext;
1314

@@ -17,8 +18,16 @@ public ApiController(ToLiDbContext dataContext, DbSet<T> data) {
1718
}
1819

1920
[HttpGet]
20-
public async Task<JsonResult> Get() {
21-
return Json(await data.ToListAsync());
21+
public async Task<JsonResult> GetAll([FromQuery] ApiQuery queryOptions) {
22+
IQueryable<T> query = data.OrderBy(e => e.Id);
23+
24+
if(queryOptions.Offset.HasValue)
25+
query = query.Skip(queryOptions.Offset.Value);
26+
27+
if(queryOptions.Limit.HasValue)
28+
query = query.Take(queryOptions.Limit.Value);
29+
30+
return Json(await query.ToListAsync());
2231
}
2332

2433
[HttpGet("{id}")]

Classes/ApiQuery.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ToLiAPI.Api {
2+
public class ApiQuery {
3+
public int? Limit { get; set; }
4+
public int? Offset { get; set; }
5+
}
6+
}

ToLiAPI.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
55
<UserSecretsId>6e49d710-898f-4c61-b0f2-466158a74242</UserSecretsId>
6+
<VersionPrefix>0.0.2</VersionPrefix>
67
</PropertyGroup>
78

89
<ItemGroup>

0 commit comments

Comments
 (0)