Пример #1
0
        public async Task <CreateCustomerCommandResult> Handle(CreateCustomerCommand command, CancellationToken cancellationToken)
        {
            var result = await _customerRepository.GetByEmail(command.Email);

            if (result != null)
            {
                throw new InvalidOperationException("Customer with same email already exists !");
            }
            else
            {
                var stripeCustomer = await _stripeService.GetByEmail(command.Email);

                //if (stripeCustomer == null)
                //{
                //  stripeCustomer = await _stripeService.Create(command);
                //}


                //We don't get null object, "stripeCustomer.Created" always filled
                if (stripeCustomer.Email == null)
                {
                    stripeCustomer = await _stripeService.Create(command);
                }

                var customer = _mapper.Map <Domain.DataModels.Customer>(command);

                customer.Id = Guid.NewGuid();
                customer.StripeCustomerId = stripeCustomer.Id;
                //customer.StripeCustomerName = stripeCustomer.Name;
                customer.StripeCustomerName = command.FirstName + " " + command.LastName;

                var response = await _customerRepository.Create(customer);

                if (response)
                {
                    return(new CreateCustomerCommandResult()
                    {
                        Payload = _mapper.Map <CustomerDto>(customer)
                    });
                }
                else
                {
                    return(new CreateCustomerCommandResult()
                    {
                        Payload = null
                    });
                }
            }
        }