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

            var command = model.ToCreateCommand();

            try
            {
                _clientFactory.GetCommandServiceClient().Execute(command);
                model.CustomerId = command.CustomerId;

                try
                {
                    // REVIEW: this check is redundant. The email service does all validaiton of the model
                    // Did the user enter an email address for this customer?
                    if (!string.IsNullOrWhiteSpace(model.EmailAddress))
                    {
                        _emailService.SendOptInMail(model);
                        _logger.Info("Send Opt-in email for new customer with email address.");

                        _emailService.SendInviteToRegister(model);
                        _logger.Info("Send InviteToRegister email for new customer with email address.");
                    }
                }
                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));
            }
        }