public ActionResult EditProfile()
        {
            int customerId = GetCustomerId();

            var customerInDb = _context.Customers.Single(c => c.CustomerId == customerId);

            var editCustomerProfileViewModel = new EditCustomerProfileViewModel
            {
                CustomerId = customerInDb.CustomerId,
                FirstName  = customerInDb.FirstName,
                LastName   = customerInDb.LastName,
                Phone      = customerInDb.Phone,
                Address1   = customerInDb.Address1,
                Address2   = customerInDb.Address2,
                ZipCode    = customerInDb.ZipCode,
                City       = customerInDb.City,
                State      = customerInDb.State
            };

            ViewBag.Name = customerInDb.FirstName + " " + customerInDb.LastName;
            ViewBag.ProfilePictureURL = customerInDb.ProfilePictureURL;
            return(View(editCustomerProfileViewModel));
        }
        public ActionResult Dashboard()
        {
            string email        = GetCustomerEmail();
            var    customerInDb = _context.Customers.Single(c => c.Email == email);

            var editCustomerProfileViewModel = new EditCustomerProfileViewModel
            {
                CustomerId    = customerInDb.CustomerId,
                FirstName     = customerInDb.FirstName,
                LastName      = customerInDb.LastName,
                Phone         = customerInDb.Phone,
                Address1      = customerInDb.Address1,
                Address2      = customerInDb.Address2,
                ZipCode       = customerInDb.ZipCode,
                City          = customerInDb.City,
                State         = customerInDb.State,
                AverageRating = BusinessLogic.GetAverageCustomerRating(customerInDb.CustomerId)
            };

            ViewBag.Name = customerInDb.FirstName + " " + customerInDb.LastName;
            ViewBag.ProfilePictureURL = customerInDb.ProfilePictureURL;
            return(View(editCustomerProfileViewModel));
        }
        public ActionResult EditProfile(EditCustomerProfileViewModel model)
        {
            if (ModelState.IsValid)
            {
                //Unqieness validations
                var checkPhoneUnqiueness = _context
                                           .Customers
                                           .Where(c => c.CustomerId != model.CustomerId)
                                           .SingleOrDefault(t => t.Phone == model.Phone);

                if (checkPhoneUnqiueness != null)
                {
                    ModelState.AddModelError("Phone", "Phone number is already registered.");
                }
                //Unqieness validations

                HttpPostedFileBase file = null;
                bool   isValidFile      = false;
                string continueToUpload = null;
                if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                {
                    file = Request.Files[0];
                    if (!(file.ContentLength > 0 && IsImage(file)))
                    {
                        ModelState.AddModelError("ProfilePictureURL", "Please upload an image of .jpg | .jpeg | .png types only.");
                        file             = null;
                        continueToUpload = "NO";
                    }
                    isValidFile = true;
                }

                if (checkPhoneUnqiueness == null && continueToUpload == null)
                {
                    var customerInDb = _context.Customers.Single(c => c.CustomerId == model.CustomerId);

                    if (isValidFile)
                    {
                        var fileName = customerInDb.CustomerId + "_" + customerInDb.Email + "_PP" + Path.GetExtension(file.FileName);
                        model.ProfilePictureURL = Path.Combine(
                            Server.MapPath("~/Content/customerProfilePictures"), fileName);
                        file.SaveAs(model.ProfilePictureURL);
                        model.ProfilePictureURL = "/Content/customerProfilePictures/" + fileName;
                    }

                    customerInDb.FirstName = model.FirstName.Trim();
                    customerInDb.LastName  = model.LastName.Trim();
                    customerInDb.Phone     = model.Phone.Trim();
                    customerInDb.Address1  = model.Address1.Trim();
                    customerInDb.Address2  = model.Address2;
                    customerInDb.ZipCode   = model.ZipCode.Trim();
                    customerInDb.City      = model.City.Trim();
                    customerInDb.State     = model.State.Trim();
                    if (isValidFile)
                    {
                        customerInDb.ProfilePictureURL = model.ProfilePictureURL;
                    }
                    customerInDb.ModifiedTime = DateTime.Now;

                    _context.SaveChanges();

                    return(RedirectToAction("Dashboard", "Customers"));
                }
            }
            ViewBag.Name = GetCustomerName();
            ViewBag.ProfilePictureURL = GetProfilePictureURL();
            return(View("../Customers/EditProfile", model));
        }