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
            }));
        }
Beispiel #2
0
        public async Task <IActionResult> CreateRecipeAsync([FromBody] RecipeCreationViewModel model)
        {
            var result = await _service.AddAsync(model);

            result.Url = this.Url.RouteUrl("GetRecipeById", new { id = result.Id });
            return(CreatedAtRoute("GetRecipeById", new { id = result.Id }, result));
        }
        private async Task <bool> SetNewRecipeProperties(Recipe recipe, RecipeCreationViewModel model)
        {
            recipe.AccountId = HttpContext.GetAccountId();
            var findAccount = _recipeService.FindAccount(recipe.AccountId);

            recipe.Created = DateTime.Now;
            recipe.Rating  = 0;
            recipe.Text    = ReformatNewRecipeText(model.Text);
            recipe.Title   = model.Title;
            if (model.Image != null)
            {
                recipe.ImageFilename = GenerateImageFilename(recipe.AccountId, model.Image);
            }
            recipe.Account = await findAccount;
            return(true);
        }
Beispiel #4
0
        public async Task AddRecipe()
        {
            await InitializeAsync();

            using (var service = GetService())
            {
                var newRecipe = new RecipeCreationViewModel()
                {
                    Name             = "New Recipes",
                    Calories         = 123,
                    NumberOfServings = 1,
                    Creator          = "Tester"
                };

                var result = await service.AddAsync(newRecipe);

                var dbRecipe = await DbContext.Recipes.FirstAsync(x => x.Id.Equals(result.Id));

                Assert.Equal(DbContext.Recipes.Count(), this.TestData.Recipes.Count + 1);
            }
        }
Beispiel #5
0
            public RecipeTestData()
            {
                #region Init Recipe

                // ------------------- Generel ----------------------
                Recipe = new Recipe()
                {
                    Name             = "My Recipe",
                    Id               = 1,
                    Calories         = 1,
                    CookedCounter    = 2,
                    Creator          = "Tester",
                    LastTimeCooked   = new DateTime(),
                    NumberOfServings = 3
                };

                // ------------------- Images ----------------------
                var image = new RecipeImage()
                {
                    Recipe   = this.Recipe,
                    Id       = 1,
                    RecipeId = Recipe.Id,
                    Url      = "http://imageUrl.de",
                    Filename = "MyImage.jpg"
                };
                Recipe.Images.Add(image);

                // ------------------- Steps ----------------------

                var steps = new List <RecipeStep>()
                {
                    new RecipeStep()
                    {
                        Recipe = Recipe, Id = 1, RecipeId = Recipe.Id, Order = 0, Description = "Step 1"
                    },
                    new RecipeStep()
                    {
                        Recipe = Recipe, Id = 2, RecipeId = Recipe.Id, Order = 1, Description = "Step 2"
                    }
                };
                Recipe.Steps.AddRange(steps);

                // ------------------- Tags ----------------------

                var tags = new List <RecipeTag>()
                {
                    new RecipeTag()
                    {
                        Id = 1, Name = "Tag 1"
                    },
                    new RecipeTag()
                    {
                        Id = 2, Name = "Tag 2"
                    },
                };

                var recipeTags = new List <RecipeRecipeTag>()
                {
                    new RecipeRecipeTag()
                    {
                        Recipe = Recipe, RecipeId = Recipe.Id, RecipeTag = tags.First(), RecipeTagId = tags.First().Id
                    },
                    new RecipeRecipeTag()
                    {
                        Recipe = Recipe, RecipeId = Recipe.Id, RecipeTag = tags.Last(), RecipeTagId = tags.Last().Id
                    },
                };

                tags.First().Recipes.Add(recipeTags.First());
                tags.Last().Recipes.Add(recipeTags.Last());
                Recipe.Tags.AddRange(recipeTags);

                // ------------------- Source ----------------------

                var source = new RecipeUrlSource()
                {
                    Id   = 1,
                    Name = "WebSource",
                    Url  = "http://www.websource.de"
                };
                var recipeSource = new RecipeSourceRecipe()
                {
                    Page     = 0,
                    Recipe   = Recipe,
                    RecipeId = Recipe.Id,
                    Source   = source,
                    SourceId = source.Id
                };
                source.RecipeSourceRecipes.Add(recipeSource);
                Recipe.Source   = recipeSource;
                Recipe.SourceId = source.Id;

                // ------------------- Ingrediant ----------------------

                var ingrediant = new Ingrediant()
                {
                    Id   = 1,
                    Name = "Ingrediant 1"
                };

                var recipeIngrediant = new RecipeIngrediant()
                {
                    Recipe       = Recipe,
                    RecipeId     = Recipe.Id,
                    Ingrediant   = ingrediant,
                    IngrediantId = ingrediant.Id,
                    Amount       = 1,
                    CookingUnit  = CookingUnit.Gramm
                };
                ingrediant.Recipes.Add(recipeIngrediant);
                Recipe.Ingrediants.Add(recipeIngrediant);

                #endregion
                UpdateModel = new RecipeUpdateViewModel()
                {
                    Id               = 1,
                    Name             = "Old Recipe",
                    Url              = "http://www.webservice.de/recipes/1",
                    Calories         = Recipe.Calories.GetValueOrDefault(),
                    NumberOfServings = Recipe.NumberOfServings
                };
                CreationModel = new RecipeCreationViewModel()
                {
                    Name             = UpdateModel.Name,
                    Calories         = UpdateModel.Calories,
                    NumberOfServings = UpdateModel.NumberOfServings,
                    Creator          = Recipe.Creator
                };
            }
Beispiel #6
0
 /// <summary>
 /// Creates a new Recipe
 /// </summary>
 /// <param name="model">Recipe-Data</param>
 /// <returns></returns>
 public async Task <RecipeUpdateViewModel> CreateRecipeAsync(RecipeCreationViewModel model)
 {
     return(await this.Client.PostAsJsonReturnAsync <RecipeCreationViewModel, RecipeUpdateViewModel>(model, "Recipes"));
 }