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));
            }
        }
Ejemplo n.º 2
0
        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)));
        }
Ejemplo n.º 3
0
        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)));
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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 }));
        }
Ejemplo n.º 6
0
        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)));
        }
Ejemplo n.º 7
0
        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)));
            }
        }
Ejemplo n.º 8
0
 protected virtual TEntity FromDtoToEntity(TDto dto)
 {
     return(DtoEntityMapper.FromDtoToEntity(dto));
 }
Ejemplo n.º 9
0
 public virtual async Task <IQueryable <TDto> > GetAll(CancellationToken cancellationToken)
 {
     return(DtoEntityMapper.FromEntityQueryToDtoQuery(await Repository.GetAllAsync(cancellationToken).ConfigureAwait(false), membersToExpand: GetODataQueryOptions().GetExpandedProperties()));
 }
Ejemplo n.º 10
0
 public virtual async Task <IQueryable <TDto> > GetAll(CancellationToken cancellationToken)
 {
     return(DtoEntityMapper.FromEntityQueryToDtoQuery(await Repository.GetAllAsync(cancellationToken)));
 }
Ejemplo n.º 11
0
        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))));
        }