Skip to content

Commit f02f264

Browse files
committed
style(EntityResourceService)
1 parent d92752b commit f02f264

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace JsonApiDotNetCore.Services
1111
{
12-
public class EntityResourceService<TResource> : EntityResourceService<TResource, int>,
12+
public class EntityResourceService<TResource> : EntityResourceService<TResource, int>,
1313
IResourceService<TResource>
1414
where TResource : class, IIdentifiable<int>
1515
{
@@ -21,7 +21,7 @@ public EntityResourceService(
2121
{ }
2222
}
2323

24-
public class EntityResourceService<TResource, TId> : EntityResourceService<TResource, TResource, TId>,
24+
public class EntityResourceService<TResource, TId> : EntityResourceService<TResource, TResource, TId>,
2525
IResourceService<TResource, TId>
2626
where TResource : class, IIdentifiable<TId>
2727
{
@@ -33,7 +33,7 @@ public EntityResourceService(
3333
{ }
3434
}
3535

36-
public class EntityResourceService<TResource, TEntity, TId> :
36+
public class EntityResourceService<TResource, TEntity, TId> :
3737
IResourceService<TResource, TId>
3838
where TResource : class, IIdentifiable<TId>
3939
where TEntity : class, IIdentifiable<TId>
@@ -51,8 +51,7 @@ public EntityResourceService(
5151
// no mapper provided, TResource & TEntity must be the same type
5252
if (typeof(TResource) != typeof(TEntity))
5353
{
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.");
5655
}
5756

5857
_jsonApiContext = jsonApiContext;
@@ -74,11 +73,11 @@ public EntityResourceService(
7473

7574
public virtual async Task<TResource> CreateAsync(TResource resource)
7675
{
77-
var entity = (typeof(TResource) == typeof(TEntity)) ? resource as TEntity :
78-
_mapper.Map<TEntity>(resource);
76+
var entity = MapIn(resource);
77+
7978
entity = await _entities.CreateAsync(entity);
80-
return (typeof(TResource) == typeof(TEntity)) ? entity as TResource :
81-
_mapper.Map<TResource>(entity);
79+
80+
return MapOut(entity);
8281
}
8382

8483
public virtual async Task<bool> DeleteAsync(TId id)
@@ -105,16 +104,12 @@ public virtual async Task<IEnumerable<TResource>> GetAsync()
105104

106105
public virtual async Task<TResource> GetAsync(TId id)
107106
{
108-
TResource dto;
109107
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);
118113
}
119114

120115
public virtual async Task<object> GetRelationshipsAsync(TId id, string relationshipName)
@@ -145,15 +140,14 @@ public virtual async Task<object> GetRelationshipAsync(TId id, string relationsh
145140

146141
public virtual async Task<TResource> UpdateAsync(TId id, TResource resource)
147142
{
148-
var entity = (typeof(TResource) == typeof(TEntity)) ? resource as TEntity :
149-
_mapper.Map<TEntity>(resource);
143+
var entity = MapIn(resource);
144+
150145
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);
153148
}
154149

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)
157151
{
158152
var entity = await _entities.GetAndIncludeAsync(id, relationshipName);
159153
if (entity == null)
@@ -165,6 +159,7 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
165159
.GetContextEntity(typeof(TResource))
166160
.Relationships
167161
.FirstOrDefault(r => r.Is(relationshipName));
162+
168163
var relationshipType = relationship.Type;
169164

170165
// update relationship type with internalname
@@ -174,8 +169,10 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
174169
throw new JsonApiException(404, $"Property {relationship.InternalRelationshipName} " +
175170
$"could not be found on entity.");
176171
}
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;
179176

180177
var relationshipIds = relationships.Select(r => r?.Id?.ToString());
181178

@@ -200,10 +197,9 @@ protected virtual async Task<IEnumerable<TResource>> ApplyPageQueryAsync(IQuerya
200197
$"with {pageManager.PageSize} entities");
201198
}
202199

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);
207203
}
208204

209205
protected virtual IQueryable<TEntity> ApplySortAndFilterQuery(IQueryable<TEntity> entities)
@@ -223,8 +219,7 @@ protected virtual IQueryable<TEntity> ApplySortAndFilterQuery(IQueryable<TEntity
223219
return entities;
224220
}
225221

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)
228223
{
229224
_jsonApiContext.IncludedRelationships = relationships;
230225

@@ -237,22 +232,34 @@ protected virtual IQueryable<TEntity> IncludeRelationships(IQueryable<TEntity> e
237232
private async Task<TResource> GetWithRelationshipsAsync(TId id)
238233
{
239234
var query = _entities.Get().Where(e => e.Id.Equals(id));
235+
240236
_jsonApiContext.QuerySet.IncludedRelationships.ForEach(r =>
241237
{
242238
query = _entities.Include(query, r);
243239
});
240+
244241
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);
247244
}
248245

249246
private bool ShouldIncludeRelationships()
250-
=> (_jsonApiContext.QuerySet?.IncludedRelationships != null &&
247+
=> (_jsonApiContext.QuerySet?.IncludedRelationships != null &&
251248
_jsonApiContext.QuerySet.IncludedRelationships.Count > 0);
252249

253250
private TResource MapOut(TEntity entity)
254251
=> (typeof(TResource) == typeof(TEntity))
255252
? entity as TResource :
256253
_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);
257264
}
258265
}

0 commit comments

Comments
 (0)