示例#1
0
        public async Task <IActionResult> Create([Bind("Title,Text,Image,TagNames")] RecipeCreationViewModel model)
        {
            if (ModelState.IsValid)
            {
                var recipe = new Recipe();
                await SetNewRecipeProperties(recipe, model);

                if (await _recipeService.Add(recipe, model.TagNames).Success())
                {
                    if (model.Image != null)
                    {
                        if (FileIsInvalid(model.Image, out JsonResult jsonResult))
                        {
                            return(jsonResult);
                        }

                        // If it fails, ignore and let the user upload it again later
                        await _fileUploadService.TryUpload(model.Image, recipe.ImageFilename, _hostingEnv);
                    }
                    return(Json(new {
                        success = true,
                        message = "",
                        redirectLocation = $"/recipes/details/{recipe.RecipeId}"
                    }));
                }
            }
            return(Json(new {
                success = false,
                message = "The recipe could not be saved due to an error. Please try again later.",
                elementId = CreationSubmitErrorId
            }));
        }
示例#2
0
        public async Task <IActionResult> EditProfilePicture([FromForm(Name = "Image")] IFormFile file)
        {
            var accountId   = HttpContext.GetAccountId();
            var findAccount = _accountService.Find(accountId);

            if (FileIsInvalid(file, out JsonResult jsonResult))
            {
                return(jsonResult);
            }

            var    account  = await findAccount;
            string fileName = GenerateImageFilename(account, file.FileName);

            _fileUploadService.DeleteIfNotNull(account.PictureFilename, _hostingEnv);

            if (await _fileUploadService.TryUpload(file, fileName, _hostingEnv))
            {
                account.PictureFilename = GenerateImageFilename(account, file.FileName);

                if (await _accountService.Update(account).Success())
                {
                    return(Json(new
                    {
                        success = true,
                        message = "",
                        elementId = ProfilePictureId,
                        imageSrc = Program.GetRequestPath(account.PictureFilename),
                        errorElementIds = new string[] { ProfilePictureErrorId }
                    }));
                }
                else
                {
                    account.PictureFilename = null;
                }
            }
            return(Json(new
            {
                success = false,
                message = "The picture could not be saved due to an error. Please try again later.",
                elementId = SubmitPicErrorId
            }));
        }