public async Task Delete(string id, DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory) { var foundEntity = await GetById(id, context, mandatoryPredicateFactory.GetMandatoryPredicates()); var idRaw = GetIdRaw(id); if (foundEntity == null) { var predicate = LinqKit.PredicateBuilder.New <TEntity>(true); var getByIdSpec = new GetByIdSpecification <TEntity, TKey>((TKey)idRaw); predicate.And(getByIdSpec.IsSatisfiedBy()); predicate.And(_onlyTenantEntitiesSpecification.IsSatisfiedBy()); foundEntity = await context.Set <TEntity>().AsExpandable().FirstOrDefaultAsync(predicate); if (foundEntity == null) { throw new EntityNotFoundException(); } } foundEntity.IsDeleted = true; context.Set <TEntity>().Update(foundEntity); await context.SaveChangesAsync(); }
protected async Task <TEntity> GetById(string id, DbContext context, ExpressionStarter <TEntity> predicate) { var idRaw = GetIdRaw(id); var getByIdSpec = new GetByIdSpecification <TEntity, TKey>((TKey)idRaw); predicate.And(getByIdSpec.IsSatisfiedBy()); var query = context.Set <TEntity>().AsQueryable(); if (_parameters.IncludeProperties != null) { foreach (var property in _parameters.IncludeProperties) { query = query.Include(property); } } var entity = await query.AsExpandable().FirstOrDefaultAsync(predicate); return(entity); }