9
9
10
10
namespace JsonApiDotNetCore . Services
11
11
{
12
- public class EntityResourceService < TResource > : EntityResourceService < TResource , int > ,
12
+ public class EntityResourceService < TResource > : EntityResourceService < TResource , int > ,
13
13
IResourceService < TResource >
14
14
where TResource : class , IIdentifiable < int >
15
15
{
@@ -21,7 +21,7 @@ public EntityResourceService(
21
21
{ }
22
22
}
23
23
24
- public class EntityResourceService < TResource , TId > : EntityResourceService < TResource , TResource , TId > ,
24
+ public class EntityResourceService < TResource , TId > : EntityResourceService < TResource , TResource , TId > ,
25
25
IResourceService < TResource , TId >
26
26
where TResource : class , IIdentifiable < TId >
27
27
{
@@ -33,7 +33,7 @@ public EntityResourceService(
33
33
{ }
34
34
}
35
35
36
- public class EntityResourceService < TResource , TEntity , TId > :
36
+ public class EntityResourceService < TResource , TEntity , TId > :
37
37
IResourceService < TResource , TId >
38
38
where TResource : class , IIdentifiable < TId >
39
39
where TEntity : class , IIdentifiable < TId >
@@ -51,8 +51,7 @@ public EntityResourceService(
51
51
// no mapper provided, TResource & TEntity must be the same type
52
52
if ( typeof ( TResource ) != typeof ( TEntity ) )
53
53
{
54
- throw new InvalidOperationException ( "Resource and Entity types are NOT the same. " +
55
- "Please provide a mapper." ) ;
54
+ throw new InvalidOperationException ( "Resource and Entity types are NOT the same. Please provide a mapper." ) ;
56
55
}
57
56
58
57
_jsonApiContext = jsonApiContext ;
@@ -74,11 +73,11 @@ public EntityResourceService(
74
73
75
74
public virtual async Task < TResource > CreateAsync ( TResource resource )
76
75
{
77
- var entity = ( typeof ( TResource ) == typeof ( TEntity ) ) ? resource as TEntity :
78
- _mapper . Map < TEntity > ( resource ) ;
76
+ var entity = MapIn ( resource ) ;
77
+
79
78
entity = await _entities . CreateAsync ( entity ) ;
80
- return ( typeof ( TResource ) == typeof ( TEntity ) ) ? entity as TResource :
81
- _mapper . Map < TResource > ( entity ) ;
79
+
80
+ return MapOut ( entity ) ;
82
81
}
83
82
84
83
public virtual async Task < bool > DeleteAsync ( TId id )
@@ -105,16 +104,12 @@ public virtual async Task<IEnumerable<TResource>> GetAsync()
105
104
106
105
public virtual async Task < TResource > GetAsync ( TId id )
107
106
{
108
- TResource dto ;
109
107
if ( ShouldIncludeRelationships ( ) )
110
- dto = await GetWithRelationshipsAsync ( id ) ;
111
- else
112
- {
113
- TEntity entity = await _entities . GetAsync ( id ) ;
114
- dto = ( typeof ( TResource ) == typeof ( TEntity ) ) ? entity as TResource :
115
- _mapper . Map < TResource > ( entity ) ;
116
- }
117
- return dto ;
108
+ return await GetWithRelationshipsAsync ( id ) ;
109
+
110
+ TEntity entity = await _entities . GetAsync ( id ) ;
111
+
112
+ return MapOut ( entity ) ;
118
113
}
119
114
120
115
public virtual async Task < object > GetRelationshipsAsync ( TId id , string relationshipName )
@@ -145,15 +140,14 @@ public virtual async Task<object> GetRelationshipAsync(TId id, string relationsh
145
140
146
141
public virtual async Task < TResource > UpdateAsync ( TId id , TResource resource )
147
142
{
148
- var entity = ( typeof ( TResource ) == typeof ( TEntity ) ) ? resource as TEntity :
149
- _mapper . Map < TEntity > ( resource ) ;
143
+ var entity = MapIn ( resource ) ;
144
+
150
145
entity = await _entities . UpdateAsync ( id , entity ) ;
151
- return ( typeof ( TResource ) == typeof ( TEntity ) ) ? entity as TResource :
152
- _mapper . Map < TResource > ( entity ) ;
146
+
147
+ return MapOut ( entity ) ;
153
148
}
154
149
155
- public virtual async Task UpdateRelationshipsAsync ( TId id , string relationshipName ,
156
- List < DocumentData > relationships )
150
+ public virtual async Task UpdateRelationshipsAsync ( TId id , string relationshipName , List < DocumentData > relationships )
157
151
{
158
152
var entity = await _entities . GetAndIncludeAsync ( id , relationshipName ) ;
159
153
if ( entity == null )
@@ -165,6 +159,7 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
165
159
. GetContextEntity ( typeof ( TResource ) )
166
160
. Relationships
167
161
. FirstOrDefault ( r => r . Is ( relationshipName ) ) ;
162
+
168
163
var relationshipType = relationship . Type ;
169
164
170
165
// update relationship type with internalname
@@ -174,8 +169,10 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
174
169
throw new JsonApiException ( 404 , $ "Property { relationship . InternalRelationshipName } " +
175
170
$ "could not be found on entity.") ;
176
171
}
177
- relationship . Type = relationship . IsHasMany ? entityProperty . PropertyType . GetGenericArguments ( ) [ 0 ] :
178
- entityProperty . PropertyType ;
172
+
173
+ relationship . Type = relationship . IsHasMany
174
+ ? entityProperty . PropertyType . GetGenericArguments ( ) [ 0 ]
175
+ : entityProperty . PropertyType ;
179
176
180
177
var relationshipIds = relationships . Select ( r => r ? . Id ? . ToString ( ) ) ;
181
178
@@ -200,10 +197,9 @@ protected virtual async Task<IEnumerable<TResource>> ApplyPageQueryAsync(IQuerya
200
197
$ "with { pageManager . PageSize } entities") ;
201
198
}
202
199
203
- var pagedEntities = await _entities . PageAsync ( entities , pageManager . PageSize ,
204
- pageManager . CurrentPage ) ;
205
- return ( typeof ( TResource ) == typeof ( TEntity ) ) ? pagedEntities as IEnumerable < TResource > :
206
- _mapper . Map < IEnumerable < TResource > > ( pagedEntities ) ;
200
+ var pagedEntities = await _entities . PageAsync ( entities , pageManager . PageSize , pageManager . CurrentPage ) ;
201
+
202
+ return MapOut ( pagedEntities ) ;
207
203
}
208
204
209
205
protected virtual IQueryable < TEntity > ApplySortAndFilterQuery ( IQueryable < TEntity > entities )
@@ -223,8 +219,7 @@ protected virtual IQueryable<TEntity> ApplySortAndFilterQuery(IQueryable<TEntity
223
219
return entities ;
224
220
}
225
221
226
- protected virtual IQueryable < TEntity > IncludeRelationships ( IQueryable < TEntity > entities ,
227
- List < string > relationships )
222
+ protected virtual IQueryable < TEntity > IncludeRelationships ( IQueryable < TEntity > entities , List < string > relationships )
228
223
{
229
224
_jsonApiContext . IncludedRelationships = relationships ;
230
225
@@ -237,22 +232,34 @@ protected virtual IQueryable<TEntity> IncludeRelationships(IQueryable<TEntity> e
237
232
private async Task < TResource > GetWithRelationshipsAsync ( TId id )
238
233
{
239
234
var query = _entities . Get ( ) . Where ( e => e . Id . Equals ( id ) ) ;
235
+
240
236
_jsonApiContext . QuerySet . IncludedRelationships . ForEach ( r =>
241
237
{
242
238
query = _entities . Include ( query , r ) ;
243
239
} ) ;
240
+
244
241
var value = await _entities . FirstOrDefaultAsync ( query ) ;
245
- return ( typeof ( TResource ) == typeof ( TEntity ) ) ? value as TResource :
246
- _mapper . Map < TResource > ( value ) ;
242
+
243
+ return MapOut ( value ) ;
247
244
}
248
245
249
246
private bool ShouldIncludeRelationships ( )
250
- => ( _jsonApiContext . QuerySet ? . IncludedRelationships != null &&
247
+ => ( _jsonApiContext . QuerySet ? . IncludedRelationships != null &&
251
248
_jsonApiContext . QuerySet . IncludedRelationships . Count > 0 ) ;
252
249
253
250
private TResource MapOut ( TEntity entity )
254
251
=> ( typeof ( TResource ) == typeof ( TEntity ) )
255
252
? entity as TResource :
256
253
_mapper . Map < TResource > ( entity ) ;
254
+
255
+ private IEnumerable < TResource > MapOut ( IEnumerable < TEntity > entities )
256
+ => ( typeof ( TResource ) == typeof ( TEntity ) )
257
+ ? entities as IEnumerable < TResource >
258
+ : _mapper . Map < IEnumerable < TResource > > ( entities ) ;
259
+
260
+ private TEntity MapIn ( TResource resource )
261
+ => ( typeof ( TResource ) == typeof ( TEntity ) )
262
+ ? resource as TEntity
263
+ : _mapper . Map < TEntity > ( resource ) ;
257
264
}
258
265
}
0 commit comments