Beispiel #1
0
        public async Task <IActionResult> Settings()
        {
            HttpResponseMessage response = await ControllerHelperMethods.CallApi(HttpMethod.Get, "https://restcountries.eu/rest/v2/all", _httpClientFactory);

            if (response.IsSuccessStatusCode)
            {
                //Getting countrie from API and storing them in ViewBag to display them as select list in AppUserModel
                Country[] countries = await response.Content.ReadFromJsonAsync <Country[]>();

                ViewBag.Countries = new SelectList(countries, "Name", "Name", "Name");
            }
            else
            {
                //In case API call fails, notify the user
                ModelState.AddModelError("Country", "Problem with loading countries, please try again later.");
            }

            //Gets currently logged in user as AppUserModel type
            AppUserModel currentUser = await _appUserManager.GetUserAsync(User)
                                       .ContinueWith(u => u.Result.ToAppUserModelBaseInfo());

            //To pass necessary data for view
            ViewBag.CurrentUser = currentUser.ToAppUserEntity();
            return(View(currentUser));
        }
Beispiel #2
0
        public async Task <IActionResult> Settings(AppUserModel model)
        {
            try
            {
                //Checks ModelState
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                //Checks if user changed profile photo, if so, it changes photo path from the model
                if (model.Photo != null)
                {
                    model.PhotoPath = ControllerHelperMethods.UpdatePhotoPath(_userPhotoFolderPath, model.Photo.FileName, model.Id);
                }

                //Converts AppUserModel to entity type, so it can be updated
                AppUser        user   = model.ToAppUserEntity();
                IdentityResult result = await _appUserManager.UpdateUserAsync(user);

                //Checks if update has succeeded
                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }

                    return(View(model));
                }

                //Deletes old photo from server, saves new photo, if no photos were supplied, returns
                await ControllerHelperMethods.SavePhoto(model.Photo, _userPhotoFolderPath, model.Id);

                return(RedirectToAction("Profile", "User", new { id = model.Id }));
            }
            catch (Exception ex)
            {
                //In case of exception, returns the View with model and errors
                ModelState.AddModelError("", ex.Message);

                return(View(model));
            }
        }