66using JsonApiDotNetCore . Extensions ;
77using JsonApiDotNetCore . Routing ;
88using Microsoft . EntityFrameworkCore ;
9+ using System . Collections ;
910
1011namespace JsonApiDotNetCore . Data
1112{
@@ -20,7 +21,15 @@ public ResourceRepository(JsonApiContext context)
2021
2122 public List < object > Get ( )
2223 {
23- return ( GetDbSetFromContext ( _context . Route . BaseRouteDefinition . ContextPropertyName ) as IEnumerable < object > ) ? . ToList ( ) ;
24+ IQueryable dbSet ;
25+ var filter = _context . Route . Query . Filter ;
26+ if ( filter != null ) {
27+ dbSet = FilterEntities ( _context . Route . BaseModelType , filter . PropertyName , filter . PropertyValue , null ) ;
28+ }
29+ else {
30+ dbSet = GetDbSet ( _context . Route . BaseModelType , null ) ;
31+ }
32+ return ( ( IEnumerable < object > ) dbSet ) . ToList ( ) ;
2433 }
2534
2635 public object Get ( string id )
@@ -35,28 +44,33 @@ private object GetRelated(string id, RelationalRoute relationalRoute)
3544 return relationalRoute . BaseModelType . GetProperties ( ) . FirstOrDefault ( pi => pi . Name . ToCamelCase ( ) == relationalRoute . RelationshipName . ToCamelCase ( ) ) ? . GetValue ( entity ) ;
3645 }
3746
38- private IQueryable GetDbSetFromContext ( string propName )
47+ private IQueryable GetDbSet ( Type modelType , string includedRelationship )
3948 {
4049 var dbContext = _context . DbContext ;
41- return ( IQueryable ) dbContext . GetType ( ) . GetProperties ( ) . FirstOrDefault ( pI => pI . Name . ToProperCase ( ) == propName . ToProperCase ( ) ) ? . GetValue ( dbContext , null ) ;
50+ return ( IQueryable ) new GenericDataAccessAbstraction ( _context . DbContext , modelType , includedRelationship ) . GetDbSet ( ) ;
4251 }
4352
4453 private object GetEntityById ( Type modelType , string id , string includedRelationship )
4554 {
4655 return new GenericDataAccessAbstraction ( _context . DbContext , modelType , includedRelationship ) . SingleOrDefault ( "Id" , id ) ;
4756 }
4857
58+ private IQueryable FilterEntities ( Type modelType , string property , string value , string includedRelationship )
59+ {
60+ return new GenericDataAccessAbstraction ( _context . DbContext , modelType , includedRelationship ) . Filter ( property , value ) ;
61+ }
62+
4963 public void Add ( object entity )
5064 {
51- var dbSet = GetDbSetFromContext ( _context . Route . BaseRouteDefinition . ContextPropertyName ) ;
65+ var dbSet = GetDbSet ( _context . Route . BaseModelType , null ) ;
5266 var dbSetAddMethod = dbSet . GetType ( ) . GetMethod ( "Add" ) ;
5367 dbSetAddMethod . Invoke ( dbSet , new [ ] { entity } ) ;
5468 }
5569
5670 public void Delete ( string id )
5771 {
5872 var entity = Get ( id ) ;
59- var dbSet = GetDbSetFromContext ( _context . Route . BaseRouteDefinition . ContextPropertyName ) ;
73+ var dbSet = GetDbSet ( _context . Route . BaseModelType , null ) ;
6074 var dbSetAddMethod = dbSet . GetType ( ) . GetMethod ( "Remove" ) ;
6175 dbSetAddMethod . Invoke ( dbSet , new [ ] { entity } ) ;
6276 }
0 commit comments