Exemplo n.º 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));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateIngredient(IngredientModel model)
        {
            try
            {
                //Checks if model is valid
                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                //Changes photo path from the model
                model.PhotoPath = ControllerHelperMethods.UpdateIngridientPhotoPath(_ingredientPhotoFolderPath, model.Photo.FileName);

                //Converts model to Ingredient type
                Ingredient ingredientEntity = model.ToIngredientEntity();

                //Adds new ingredient to database
                Ingredient newIngredient = await _ingredientRepository.Add(ingredientEntity);

                //Adds photo to server
                await ControllerHelperMethods.SaveIngredientPhoto(model.Photo, _ingredientPhotoFolderPath);

                return(RedirectToAction("IngredientInfo", new { id = newIngredient.Id }));
            }
            catch
            {
                return(View(model));
            }
        }
Exemplo n.º 3
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));
            }
        }