Ejemplo n.º 1
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task <bool> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                foreach (var error in request.ValidationResult.Errors)
                {
                    throw new CustomerDomainException(error.ErrorMessage);
                }
                return(false);
            }

            //encrypt password
            var passwordResult = PasswordWithSaltHasher.ActionEncrypt(request.Password, SaltLength);
            var customer       = new Customer(request.FirstName, request.LastName, request.Email, passwordResult.Salt, passwordResult.Digest);

            var existingCustomer = _customerRepository.FindByEmail(customer.Email);

            if (existingCustomer != null)
            {
                throw new CustomerDomainException("The customer e-mail has already been taken");
            }

            //add customer
            var customerAdded = _customerRepository.Add(customer);

            if (customerAdded == null)
            {
                throw new CustomerDomainException("new customer could not insert");
            }
            return(await _unitOfWork.CommitAsync(cancellationToken));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        public void Add(AddNewCustomerViewModel model)
        {
            const int saltLength            = 64;
            var       createCustomerCommand = _mapper.Map <CreateCustomerCommand>(model);
            var       passwordResult        = PasswordWithSaltHasher.ActionEncrypt(model.Password, saltLength);

            createCustomerCommand.SecurityStamp = passwordResult.Salt;
            createCustomerCommand.PasswordHash  = passwordResult.Digest;

            _commandDispatcher.Send(createCustomerCommand);
        }