Пример #1
0
        public dynamic UpdateCustomer(Guid id, CustomerModel model)
        {
            // TODO: validate model state

            var customer = _clientFactory.GetCustomersQueryServiceClient().GetCustomer(model.CustomerId);
            var command  = model.ToSaveCommand(customer);

            try
            {
                var sendEmail = false;

                // REVIEW: This logic doesnt belong here in the controller and should be moved back behind the email service.
                // IMPORTANT - We have to first check to see if the model email address is changing before saving the customer
                // Are we removing an existing address
                // model is null - do nothing
                if (!string.IsNullOrWhiteSpace(model.EmailAddress))
                {
                   // don't send email if customer has no email address or the email address has not changed
                   if (customer.EmailAddress == null || string.IsNullOrWhiteSpace(customer.EmailAddress.Address))
                    {
                        //adding a new address
                        sendEmail = true;
                        _logger.Info("Send Opt-in email for an existing customer with out email address.");
                    }
                    else if (customer.EmailAddress != null
                            && !string.IsNullOrWhiteSpace(customer.EmailAddress.Address)
                            && !string.IsNullOrWhiteSpace(model.EmailAddress)
                            && !customer.EmailAddress.Address.Equals(model.EmailAddress, StringComparison.InvariantCultureIgnoreCase))
                    {
                        // changing existing address
                        // customer has address, model has address but they are different - send email
                        sendEmail = true;
                        _logger.Info("Send Opt-in email for an existing customer with changing email address.");
                    }
                }

                _clientFactory.GetCommandServiceClient().Execute(command);

                try
                {
                    if (sendEmail)
                        _emailService.SendOptInMail(model);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }

                // REST style returns id and hyperlinks in the response
                return new
                {
                    id = command.CustomerId,
                    links = new
                    {
                        self   = new { href = Url.Route("DefaultApi", new { id = command.CustomerId }) },
                        photo  = new { href = Url.Route("Default", new { controller = "customerimages", action = "profile", id = command.CustomerId }) },
                        detail = new { href = Url.Route("Default", new { controller = "customers", action = "detail", id = command.CustomerId }) }
                    }
                };
            }
            catch (CommandException ex)
            {
                throw ApiHelpers.ServerError(ex.Message, string.Join("\r\n", ex.Errors));
            }
        }