public void Create(AddCustomerQueryRequest request)
        {
            
            var shopContext = new ShopContext("Shop");
            var customer = shopContext.Customers.Add(request.Customer);
            customer.CustomerProductQueries = new List<CustomerProductQuery>();
            foreach (var productId in request.ProductIds)
            {
                customer.CustomerProductQueries.Add(new CustomerProductQuery() { ProductId = productId });
            }

            shopContext.SaveChanges();

            // send email to sales team - in reality would refactor out to a seperate service - more of these settings should probably go in config
            SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
            client.Credentials = new NetworkCredential("username", "password");
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("*****@*****.**");
            mailMessage.To.Add("*****@*****.**");
            var productNames = shopContext.Products.Where(p => request.ProductIds.Contains(p.Id)).Select(p => p.Name);
            mailMessage.Body =
                $"Customer {customer.FirstName} {customer.LastName} has a price query about Product Ids {string.Join(",", productNames)}";
            mailMessage.Subject = "subject";
            try
            {
                client.Send(mailMessage);
            }
            catch (Exception)
            {
                //dont allow email error to cause issue to user
                //log email error here
            }


        }
        public ActionResult Create(ProductQueryCreateViewModel model)
        {

            if (!ModelState.IsValid)
            {
                model = _productQueryCreateViewModelFactory.CreateWithExistingModel(model);
                return View(model);
            }
            //call service - would wrap this in seperate testable class if more time
            var customer = new Customer() {AddressLine1 = model.AddressLine1, City = model.City, CountryId = model.SelectedCountryId, DateCreated = DateTime.Now,EmailAddress = model.EmailAddress,FirstName = model.FirstName, LastName = model.LastName,Postcode = model.Postcode};
            var query= new AddCustomerQueryRequest () {Customer = customer, ProductIds = model.SelectedProductIds.ToList()};
            
            new ProductService().CreateProductQuery(query);
            return RedirectToAction("CreateConfirm");
        }
示例#3
0
        public void CreateProductQuery(AddCustomerQueryRequest query)
        {
            using (var client = new HttpClient())
            {
                AddCommonSettings(client);
                HttpResponseMessage response = client.PostAsJsonAsync("api/customerproductquery", query).Result;
                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpRequestException("Problem calling webapi service to create product query");

                }

            }

            

        }