public virtual async Task <TResource> CreateAsync(TResource entity) { entity = IsNull(_hookExecutor) ? entity : _hookExecutor.BeforeCreate(AsList(entity), ResourcePipeline.Post).SingleOrDefault(); entity = await _repository.CreateAsync(entity); if (_includeService.Get().Any()) { entity = await GetWithRelationshipsAsync(entity.Id); } if (!IsNull(_hookExecutor, entity)) { _hookExecutor.AfterCreate(AsList(entity), ResourcePipeline.Post); entity = _hookExecutor.OnReturn(AsList(entity), ResourcePipeline.Get).SingleOrDefault(); } return(entity); }
/// <summary> /// Applies include queries /// </summary> protected virtual IQueryable <TResource> ApplyInclude(IQueryable <TResource> entities, RelationshipAttribute chainPrefix = null) { var type = entities.ElementType; var allowedRelations = _fieldsToSerialize.GetAllowedRelationships(type); var chains = _includeService.Get(); var fakeChains = chains.ToList(); // sven // loop through and only add missing ones foreach (var relation in allowedRelations) { // add each allowedRelation to single list because then they get treated separately. // otherwise it would get treated as a follow-up relation bool isIncluded = fakeChains.Select(x => x.First()).Any(x => x.Equals(relation)); if (!isIncluded) { var subList = new List <RelationshipAttribute> { relation }; //var relationType = relation.RightType; //var subAllowed = _fieldsToSerialize.GetAllowedRelationships(relationType); //subList.AddRange(subAllowed); fakeChains.Add(subList); } } if (chainPrefix != null) { chains.Add(new List <RelationshipAttribute>()); } // sven foreach (var inclusionChain in fakeChains) { if (chainPrefix != null) { inclusionChain.Insert(0, chainPrefix); } entities = _repository.Include(entities, inclusionChain); } return(entities); }
/// <summary> /// Inspects the included relationship chains (see <see cref="IIncludeService"/> /// to see if <paramref name="relationship"/> should be included or not. /// </summary> private bool ShouldInclude(RelationshipAttribute relationship, out List <List <RelationshipAttribute> > inclusionChain) { inclusionChain = _includeService.Get()?.Where(l => l.First().Equals(relationship)).ToList(); if (inclusionChain == null || !inclusionChain.Any()) { return(false); } return(true); }
public virtual async Task <TResource> CreateAsync(TResource entity) { _logger.LogTrace($"Entering {nameof(CreateAsync)}({(entity == null ? "null" : "object")})."); entity = IsNull(_hookExecutor) ? entity : _hookExecutor.BeforeCreate(AsList(entity), ResourcePipeline.Post).SingleOrDefault(); entity = await _repository.CreateAsync(entity); if (_includeService.Get().Any()) { entity = await GetWithRelationshipsAsync(entity.Id); } if (!IsNull(_hookExecutor, entity)) { _hookExecutor.AfterCreate(AsList(entity), ResourcePipeline.Post); entity = _hookExecutor.OnReturn(AsList(entity), ResourcePipeline.Get).SingleOrDefault(); } return(entity); }
/// <summary> /// Applies include queries /// </summary> protected virtual IQueryable <TResource> ApplyInclude(IQueryable <TResource> entities, RelationshipAttribute chainPrefix = null) { var chains = _includeService.Get(); if (chainPrefix != null) { chains.Add(new List <RelationshipAttribute>()); } foreach (var inclusionChain in chains) { if (chainPrefix != null) { inclusionChain.Insert(0, chainPrefix); } entities = _repository.Include(entities, inclusionChain); } return(entities); }
/// <inheritdoc/> public void BeforeRead <TResource>(ResourcePipeline pipeline, string stringId = null) where TResource : class, IIdentifiable { var hookContainer = _executorHelper.GetResourceHookContainer <TResource>(ResourceHook.BeforeRead); hookContainer?.BeforeRead(pipeline, false, stringId); var calledContainers = new List <LeftType> { typeof(TResource) }; foreach (var chain in _includeService.Get()) { RecursiveBeforeRead(chain, pipeline, calledContainers); } }
/// <summary> /// Inspects the included relationship chains (see <see cref="IIncludeService"/> /// to see if <paramref name="relationship"/> should be included or not. /// </summary> private bool ShouldInclude(RelationshipAttribute relationship, out List <List <RelationshipAttribute> > inclusionChain) { inclusionChain = _includeService.Get()?.Where(l => l.First().Equals(relationship)).ToList(); return(inclusionChain != null && inclusionChain.Any()); }