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));
            }
        }
Пример #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)));
        }
Пример #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)));
        }
Пример #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));
        }
Пример #5
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)));
        }
Пример #6
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)));
            }
        }