public virtual async Task <TDto> Update(TKey key, TDto dto, CancellationToken cancellationToken) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } TEntity entity = FromDtoToEntity(dto); if (!EqualityComparer <TKey> .Default.Equals(key, GetKey(dto))) { throw new BadRequestException(); } entity = await Repository.UpdateAsync(entity, cancellationToken); try { return(await GetById(key, cancellationToken)); } catch (ResourceNotFoundException) { return(DtoEntityMapper.FromEntityToDto(entity)); } }
public virtual async Task <CustomerDto> ActiveCustomer(CancellationToken cancellationToken) { Guid customerId = Guid.Parse(UserInformationProvider.GetCurrentUserId()); Customer customer = await(await CustomersRepository.GetAllAsync(cancellationToken)).FirstOrDefaultAsync(c => c.Id == customerId); customer.IsActive = true; return(DtoEntityMapper.FromEntityToDto(await CustomersRepository.UpdateAsync(customer, cancellationToken))); }
public virtual async Task <SingleResult <TDto> > Create(TDto dto, CancellationToken cancellationToken) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } TEntity entity = await Repository.AddAsync(FromDtoToEntity(dto), cancellationToken).ConfigureAwait(false); return(SingleResult(await GetQueryById(GetKey(DtoEntityMapper.FromEntityToDto(entity)), cancellationToken).ConfigureAwait(false))); }
public virtual async Task <TDto> Create(TDto dto, CancellationToken cancellationToken) { if (dto == null) { throw new ArgumentNullException(nameof(dto)); } TEntity entity = await Repository.AddAsync(FromDtoToEntity(dto), cancellationToken); return(await GetById(GetKey(DtoEntityMapper.FromEntityToDto(entity)), cancellationToken)); }
public override async Task <IQueryable <ChangeSetDto> > GetAll(CancellationToken cancellationToken) { // We can declare a view for following sql query in database and then map our dto directly to view using ef db context: // select *, (case (((select count(1) from Deliveries as Delivery where ChangeSet.Id = Delivery.ChangeSetId ))) when (select count(1) from Customers) then 1 else 0 end) as IsDeliveredToAll from ChangeSets as ChangeSet // or we can use ef core execute sql which returns IQueryable // Note: _changeSetsRepository.GetAll(changeSet => new ChangeSetDto { Id = changeSet.Id , ... , IsDeliveredToAll = changeSet.Deliveries.Count() == customersQuery.Count() }); results into problematic sql. // The downside of following code are its database round trips. int customersCount = await(await CustomersRepository.GetAllAsync(cancellationToken)).CountAsync(cancellationToken); return(DtoEntityMapper.FromEntityQueryToDtoQuery((await Repository.GetAllAsync(cancellationToken)), parameters: new { customersCount = customersCount })); }
public virtual async Task <CustomerDto> SendVerificationCodeAgain(RegisterCustomerArgs args, CancellationToken cancellationToken) { if (!CustomerValidator.IsValid(args.customer, out string errorMessage)) { throw new DomainLogicException(errorMessage); } Customer customer = DtoEntityMapper.FromDtoToEntity(args.customer); customer = await(await CustomersRepository.GetAllAsync(cancellationToken)) .Where(cu => cu.NationalCode == customer.NationalCode || cu.Mobile == customer.Mobile) .FirstOrDefaultAsync(cancellationToken); customer.VerifyCode = await SmsService.SendVerifyCode(customer.Mobile); return(DtoEntityMapper.FromEntityToDto(await CustomersRepository.UpdateAsync(customer, cancellationToken))); }
public virtual async Task <CustomerDto> RegisterCustomer(RegisterCustomerArgs args, CancellationToken cancellationToken) { if (!CustomerValidator.IsValid(args.customer, out string errorMessage)) { throw new DomainLogicException(errorMessage); } Customer customer = DtoEntityMapper.FromDtoToEntity(args.customer); bool existingCustomer = await(await CustomersRepository.GetAllAsync(cancellationToken)) .Where(cu => cu.NationalCode == customer.NationalCode || cu.Mobile == customer.Mobile) .AnyAsync(cancellationToken); if (existingCustomer) { throw new DomainLogicException("CustomerIsAlreadyRegistered"); } else { customer.VerifyCode = await SmsService.SendVerifyCode(customer.Mobile); return(DtoEntityMapper.FromEntityToDto(await CustomersRepository.AddAsync(customer, cancellationToken))); } }
protected virtual TEntity FromDtoToEntity(TDto dto) { return(DtoEntityMapper.FromDtoToEntity(dto)); }
public virtual async Task <IQueryable <TDto> > GetAll(CancellationToken cancellationToken) { return(DtoEntityMapper.FromEntityQueryToDtoQuery(await Repository.GetAllAsync(cancellationToken).ConfigureAwait(false), membersToExpand: GetODataQueryOptions().GetExpandedProperties())); }
public virtual async Task <IQueryable <TDto> > GetAll(CancellationToken cancellationToken) { return(DtoEntityMapper.FromEntityQueryToDtoQuery(await Repository.GetAllAsync(cancellationToken))); }
public virtual async Task <SingleResult <CustomerDto> > GetCurrentCustomer(CancellationToken cancellationToken) { Guid customerId = Guid.Parse(UserInformationProvider.GetCurrentUserId()); return(SingleResult.Create(DtoEntityMapper.FromEntityQueryToDtoQuery((await CustomersRepository.GetAllAsync(cancellationToken)).Where(c => c.Id == customerId)))); }