Example #1
0
        [HttpPost] // (TH) Kept original above.
        public async Task <ActionResult> Update(UpdateUserViewModel userViewModel, HttpPostedFileBase file)
        {
            var currentUser = userViewModel.CurrentUser;

            currentUser.ID = (_sessionHelper.Get("CurrentUser") as User).ID;

            currentUser.UserTypeID = (int)UserType.UserTypes.User;
            //TOOD Refactor Move logic to manager class
            if (ModelState.IsValid)
            {
                if (file == null || file.ContentLength <= 0)
                {
                    if (_azureManager.UserImageExists(currentUser))
                    {
                        currentUser.ProfileImagePath = _azureManager.GetUserUrl(currentUser);
                        await _userManger.Update(currentUser);

                        ViewBag.UpdateMessage = "Account Successfully updated with no image change!";
                    }
                    else
                    {
                        currentUser.ProfileImagePath = _azureManager.GetDefaultUrl();
                        await _userManger.Update(currentUser);

                        ViewBag.UpdateMessage = "Account Successfully updated with default image!";
                    }
                }
                else
                {
                    var convertedfile = ConvertToBytes(file.InputStream);
                    _azureManager.UploadImage(currentUser, convertedfile);
                    currentUser.ProfileImagePath = _azureManager.GetUserUrl(currentUser);
                    await _userManger.Update(currentUser);

                    ViewBag.UpdateMessage = "Account Successfully updated with new image!";
                }

                userViewModel.Genders       = _genderManager.GetAll();
                userViewModel.Nationalities = _nationalityManager.GetAll();
                //ViewBag.UpdateMessage = "Account Successfully updated.";

                return(View(nameof(Update), userViewModel));
            }
            else
            {
                userViewModel.Genders       = _genderManager.GetAll();
                userViewModel.Nationalities = _nationalityManager.GetAll();

                ViewBag.Message = "An error occurred when updating the account.";
                return(View(nameof(Update), userViewModel));
            }
        }