// GET: CustomerController/Delete/5
        public ActionResult Delete(string id)
        {
            var         cCustomer    = _storeRepo.GetOneCustomer(id);
            CCredential cCredential  = _storeRepo.GetOneCredential(cCustomer.Email);
            var         viewCustomer = ViewModelMapper.MapSingleCustomerWithCredential(cCustomer, cCredential);

            return(View(viewCustomer));
        }
Esempio n. 2
0
        /// <summary>
        /// Performs basic initialization of a new instance of the Context class.
        /// Initialization is not complete until the ContextHandle property has been set
        /// and the Initialize method has been called.
        /// </summary>
        /// <param name="cred"></param>
        protected CContext(CCredential cred)
        {
            this.Credential = cred;

            this.ContextHandle = new CSafeContextHandle();

            this.Disposed    = false;
            this.Initialized = false;
        }
        public ActionResult Edit(string id, CustomerViewModel viewCustomer)
        {
            string storeLoc = TempData.Peek("adminLoc").ToString();

            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "invalid input format");
                    return(View());
                }

                // concurrent
                CCustomer foundCustomer = _storeRepo.GetOneCustomer(id);
                if (foundCustomer == null)
                {
                    ModelState.AddModelError("", "Another Admin has just deleted this customer");
                    return(View());
                }
                CCredential foundCredential = _storeRepo.GetOneCredential(foundCustomer.Email);
                if (foundCredential == null)
                {
                    ModelState.AddModelError("", "Another Admin has just deleted this email");
                    return(View());
                }


                // if you have changed email
                if (foundCustomer.Email != viewCustomer.Email)
                {
                    // check if the changed email has already been used by someone else
                    CCustomer editedCustomer1 = _storeRepo.GetOneCustomerByEmail(viewCustomer.Email);
                    if (editedCustomer1 != null)
                    {
                        ModelState.AddModelError("", "This email is already in use");
                        return(View());
                    }
                }
                var editedCustomer   = new CCustomer(id, viewCustomer.Firstname, viewCustomer.Lastname, viewCustomer.Phonenumber, viewCustomer.Email);
                var editedCredential = new CCredential(viewCustomer.Email, viewCustomer.Password);
                _storeRepo.DeleteOneCustomer(storeLoc, id);
                _storeRepo.DelelteOneCredential(foundCustomer.Email);
                // drop dependcy issue
                //_storeRepo.EditOneCredential(foundCredential.Email,editedCredential);
                _storeRepo.AddOneCredential(editedCredential);
                _storeRepo.StoreAddOneCustomer(storeLoc, editedCustomer);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while trying to edit a customer");
                ModelState.AddModelError("", "failed to edit a customer");
                return(View());
            }
        }
Esempio n. 4
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());
            }
        }
Esempio n. 5
0
        public void AddOneCredential(CCredential credential)
        {
            using var context = new Project0databaseContext(_contextOptions);
            Credential cCredential = new Credential
            {
                Email    = credential.Email,
                Password = credential.Password
            };

            context.Credentials.Add(cCredential);
            context.SaveChanges();
        }
Esempio n. 6
0
        }// not mapped

        // credential level
        public CCredential GetOneCredential(string email)
        {
            using var context = new Project0databaseContext(_contextOptions);
            var dbCredential = context.Credentials.FirstOrDefault(x => x.Email == email);

            if (dbCredential == null)
            {
                return(null);
            }
            CCredential c = new CCredential(dbCredential.Email, dbCredential.Password);

            return(c);
        }
        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());
            }
        }
        public ActionResult Details(string id)
        {
            CCustomer   cCustomer   = _storeRepo.GetOneCustomer(id);
            CCredential cCredential = _storeRepo.GetOneCredential(cCustomer.Email);

            // concurrent
            if (cCustomer == null)
            {
                ModelState.AddModelError("", "Another admin has just deleted this customer");
                return(View());
            }
            if (cCredential == null)
            {
                ModelState.AddModelError("", "Another admin has just deleted this email");
                return(View());
            }
            var viewCustomer = ViewModelMapper.MapSingleCustomerWithCredential(cCustomer, cCredential);

            return(View(viewCustomer));
        }
Esempio n. 9
0
        public ActionResult Index(LoginViewModel viewLogin)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Invalid login format");
                    return(View());
                }

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

                // admin login
                CAdmincredential cAdmin = _storeRepo.GetOneAdminCredential(viewLogin.Email);
                if (cAdmin != null)
                {
                    if (cAdmin.Password == viewLogin.Password)
                    {
                        // admin successful login
                        TempData["User"] = viewLogin.Email;
                        TempData.Keep("User");
                        // each user can store some information
                        TempData[viewLogin.Email] = 1;
                        return(RedirectToAction("Index", "Admin"));
                    }
                }

                // memeber login
                CCredential cCredential = _storeRepo.GetOneCredential(viewLogin.Email);
                if (cCredential == null)
                {
                    ModelState.AddModelError("", "This email address has not been registered");
                    return(View());
                }

                if (cCredential.Password == viewLogin.Password)
                {
                    // user successful login
                    TempData["User"] = viewLogin.Email;
                    TempData.Keep("User");
                    TempData[viewLogin.Email] = 1;
                }
                else
                {
                    ModelState.AddModelError("", "Password does not match");
                    return(View());
                }
                // relative path
                return(RedirectToAction("Index", "Store"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while tring to login");
                ModelState.AddModelError("", "failed to login");
                return(View());
            }
        }