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(); }
public async Task <TEditEntityDto> Get(string id, DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory) { var entity = await GetById(id, context, mandatoryPredicateFactory.GetMandatoryPredicates()); if (entity == null) { return(null); } var entityDto = _mapper.Map <TEditEntityDto>(entity); return(entityDto); }
public async Task <PagedResult <TFilterableEntity> > FilterAsync(DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory, FilterParameters filterParameters) { var orderByClause = CreateOrderBy(filterParameters); var paggingClause = CreatePagging(filterParameters); var whereParameters = CreateWhere(filterParameters); var sqlCount = _parameters.Sql .Replace(OrderByClause, string.Empty, StringComparison.OrdinalIgnoreCase) .Replace(PaggingClause, string.Empty, StringComparison.OrdinalIgnoreCase) .Replace(WhereClause, whereParameters.sql, StringComparison.OrdinalIgnoreCase); var sqlResult = _parameters.Sql .Replace(OrderByClause, orderByClause, StringComparison.OrdinalIgnoreCase) .Replace(PaggingClause, paggingClause, StringComparison.OrdinalIgnoreCase) .Replace(WhereClause, whereParameters.sql, StringComparison.OrdinalIgnoreCase); var count = await context.Set <TFilterableEntity>().FromSqlRaw(sqlCount, whereParameters.parameters).LongCountAsync(); var result = await context.Set <TFilterableEntity>().FromSqlRaw(sqlResult, whereParameters.parameters).ToArrayAsync(); return(new PagedResult <TFilterableEntity>(count, result)); }
public async Task <(TEditEntityDto entityDto, TEntity entity)> Put(string id, TEditEntityDto entityDto, DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory, List <IEntityCorrector <TEntity, TCreateEntityDto, TEditEntityDto, TKey> > correctors) { var foundEntity = await GetById(id, context, mandatoryPredicateFactory.GetMandatoryPredicates()); if (foundEntity == null) { throw new EntityNotFoundException(); } _mapper.Map(entityDto, foundEntity); var correctorContext = new EntityCorrectorContext { OperationKind = CrudOperationKind.Put }; await correctors.CorrectEditEntityAsync(correctorContext, foundEntity, entityDto); context.Set <TEntity>().Update(foundEntity); await context.SaveChangesAsync(); foundEntity = await GetById(id, context, mandatoryPredicateFactory.GetMandatoryPredicates()); entityDto = _mapper.Map <TEditEntityDto>(foundEntity); await correctors.CorrectEditEntityDtoAsync(correctorContext, entityDto, foundEntity); return(entityDto, foundEntity); }
public async Task <PagedResult <TFilterableEntity> > FilterAsync(DbContext context, IResourceMandatoryPredicateFactory <TEntity, TKey> mandatoryPredicateFactory, FilterParameters filterParameters) { var mandatoryPredicate = mandatoryPredicateFactory.GetMandatoryPredicates(); var predicate = LinqKit.PredicateBuilder.New <TEntity>(true); foreach (var filter in filterParameters.Statements) { ISpecification <TEntity> specification = Create(filter); if (specification == null) { continue; } if (filter.LogicalOperator == FilterLogicalOperators.Or) { predicate.Or(specification.IsSatisfiedBy()); } else { predicate.And(specification.IsSatisfiedBy()); } } var count = await context.Set <TEntity>().AsExpandable().Where(mandatoryPredicate).Where(predicate).LongCountAsync(); var entityQuery = context.Set <TEntity>().AsQueryable(); if (_parameters.IncludeProperties != null) { foreach (var includedProperty in _parameters.IncludeProperties) { entityQuery = entityQuery.Include(includedProperty); } } var query = entityQuery.AsExpandable().Where(mandatoryPredicate).Where(predicate); if (!string.IsNullOrWhiteSpace(filterParameters.OrderBy)) { var sortOrder = filterParameters.OrderKind == OrderKind.Asc? ListSortDirection.Ascending: ListSortDirection.Descending; var orderByColumn = filterParameters.OrderBy.ToLower(); if (_parameters.SortFieldsMaping.ContainsKey(orderByColumn)) { orderByColumn = _parameters.SortFieldsMaping[orderByColumn]; } query = query.OrderBy(orderByColumn, sortOrder); } var pageData = PageDataExtensions.GetPageData(filterParameters.PageSize, filterParameters.CurrentPage); if (pageData.skip >= 0) { query = query.Skip(pageData.skip).Take(pageData.take); } TFilterableEntity[] entities; if (_parameters.DirectProject) { entities = await _mapper.ProjectTo <TFilterableEntity>(query).ToArrayAsync(); } else { var queryEntities = await query.ToArrayAsync(); entities = _mapper.Map <TFilterableEntity[]>(queryEntities); } return(new PagedResult <TFilterableEntity>(count, entities)); }