Exemplo n.º 1
0
        public ActionResult CreateCustomer(Models.Customer newCustomer)
        {
            if (ModelState.IsValid)
            {
                //Creation of the customer code from first, last and Dob.
                string custCode = CreateCustomerCode(newCustomer.FirstName, newCustomer.LastName, newCustomer.DateOfBirth);
                try
                {
                    // Check to see if customer exists otherwise insert net customer and return message.
                    using (var dataContext = dbContext)
                    {
                        var customerRepository = new Repository.CustomerRepository(dataContext);

                        if (customerRepository.DoesCustomerExist(custCode))
                        {
                            TempData["Message"] = "Customer Already Exists";
                            return(View("CreateCustomer"));
                        }

                        //creation of customer code
                        newCustomer.CustCode = custCode;
                        customerRepository.Insert(newCustomer);
                        dataContext.SaveChanges();
                    }
                    TempData["Message"] = "Registered Successfully";
                    return(RedirectToAction("Index", "Home"));
                }
                // catch all exceptions for things like unable to connect to database
                catch (Exception e)
                {
                    //tempory logging till a logger can be implemented
                    Console.Out.WriteLine(e.Message);
                    TempData["Message"] = "Failed to create customer";
                    return(View("CreateCustomer"));
                }
            }
            return(View());
        }