public ActionResult Create(CustomerViewModel viewCustomer)
        {
            string storeLoc = TempData.Peek("adminLoc").ToString();

            try
            {
                if (!ModelState.IsValid)
                {
                    return(View());
                }

                if (viewCustomer.Password != viewCustomer.ConfirmPassword)
                {
                    ModelState.AddModelError("", "Passwords do not match");
                    return(View());
                }

                CCustomer cCustomer = _storeRepo.GetOneCustomerByEmail(viewCustomer.Email);
                if (cCustomer != null)
                {
                    ModelState.AddModelError("", "This email is already in use, try a different one");
                    return(View());
                }
                else
                {
                    string customerID = Guid.NewGuid().ToString().Substring(0, 10);
                    cCustomer = new CCustomer(customerID, viewCustomer.Firstname, viewCustomer.Lastname, viewCustomer.Phonenumber, viewCustomer.Email);
                    CCredential cCredential = new CCredential(viewCustomer.Email, viewCustomer.Password);

                    // it is possible that the credential gets in and customer profile not
                    _storeRepo.AddOneCredential(cCredential);
                    _storeRepo.StoreAddOneCustomer(storeLoc, cCustomer);
                }
                return(RedirectToAction(nameof(Create)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while trying to register");
                ModelState.AddModelError("", "failed to register");
                return(View());
            }
        }
예제 #2
0
        public ActionResult Register(CustomerViewModel viewCustomer)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Invalid input format");
                    return(View());
                }

                MailAddress result;
                if (!MailAddress.TryCreate(viewCustomer.Email, out result))
                {
                    ModelState.AddModelError("", "Invalid login format");
                    return(View());
                }

                if (viewCustomer.Password != viewCustomer.ConfirmPassword)
                {
                    ModelState.AddModelError("", "Passwords do not match");
                    return(View());
                }

                CCustomer cCustomer = _storeRepo.GetOneCustomerByEmail(viewCustomer.Email);
                if (cCustomer != null)
                {
                    ModelState.AddModelError("", "This email is already in use, try a different one");
                    return(View());
                }
                else
                {
                    // customer don't type in his ID number, is assigned automatically
                    string customerID = Guid.NewGuid().ToString().Substring(0, 10);

                    cCustomer = new CCustomer(customerID, viewCustomer.Firstname, viewCustomer.Lastname, viewCustomer.Phonenumber, viewCustomer.Email);
                    CCredential cCredential = new CCredential(viewCustomer.Email, viewCustomer.Password);
                    // it is possible that the credential gets in and customer profile not
                    _storeRepo.AddOneCredential(cCredential);
                    _storeRepo.AddOneCustomer(cCustomer);

                    TempData["User"] = cCustomer.Email;
                    TempData.Keep("User");
                    // changed to shopping cart later
                    TempData[cCustomer.Email] = 1;
                }
                return(RedirectToAction("Index", "Store"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while trying to register");
                ModelState.AddModelError("", "failed to register");
                return(View());
            }
        }