@@ -18,8 +18,25 @@ public ApiController(ToLiDbContext dataContext, DbSet<T> data) {
18
18
}
19
19
20
20
[ HttpGet ]
21
- public async Task < JsonResult > GetAll ( [ FromQuery ] ApiQuery queryOptions ) {
22
- IQueryable < T > query = data . OrderBy ( e => e . Id ) ;
21
+ public virtual async Task < JsonResult > GetAll ( [ FromQuery ] ApiQuery queryOptions ) {
22
+ var query = ApplyFilters ( data , queryOptions ) ;
23
+
24
+ return Json ( await query . ToListAsync ( ) ) ;
25
+ }
26
+
27
+ [ HttpGet ( "{id}" ) ]
28
+ public async Task < JsonResult > Get ( [ FromRoute ] int id ) {
29
+ return Json ( await data . Where ( item => item . Id == id ) . FirstOrDefaultAsync ( ) ) ;
30
+ }
31
+
32
+ protected IQueryable < T > ApplyFilters ( IQueryable < T > query , ApiQuery queryOptions ) {
33
+ query = data . OrderBy ( e => e . Id ) ;
34
+
35
+ if ( queryOptions . Offset . HasValue )
36
+ query = query . Skip ( queryOptions . Offset . Value ) ;
37
+
38
+ if ( queryOptions . Limit . HasValue )
39
+ query = query . Take ( queryOptions . Limit . Value ) ;
23
40
24
41
if ( queryOptions . Include . HasValue && queryOptions . Include . Value ) {
25
42
var entityType = dataContext . Model . FindEntityType ( typeof ( T ) ) ;
@@ -28,18 +45,7 @@ public async Task<JsonResult> GetAll([FromQuery] ApiQuery queryOptions) {
28
45
}
29
46
}
30
47
31
- if ( queryOptions . Offset . HasValue )
32
- query = query . Skip ( queryOptions . Offset . Value ) ;
33
-
34
- if ( queryOptions . Limit . HasValue )
35
- query = query . Take ( queryOptions . Limit . Value ) ;
36
-
37
- return Json ( await query . ToListAsync ( ) ) ;
38
- }
39
-
40
- [ HttpGet ( "{id}" ) ]
41
- public async Task < JsonResult > Get ( [ FromRoute ] int id ) {
42
- return Json ( await data . Where ( item => item . Id == id ) . FirstOrDefaultAsync ( ) ) ;
48
+ return query ;
43
49
}
44
50
45
51
/* TODO: Let authenticated users add data?
0 commit comments