Beispiel #1
0
        public IActionResult Recipe([FromBody] RecipeView r)
        {
            if (ModelState.IsValid)
            {
                //automapper
                var recipe = new Recipe
                {
                    Name        = r.Name,
                    Description = r.Description
                };

                if (_recipe.AddRecipe(recipe))
                {
                    // see if tags exist, if not add them
                    // then add many-many mapping via recipetag
                    foreach (string t in r.Tags)
                    {
                        var result = _tags.FindTag(t);
                        if (result == null)
                        {
                            result = _tags.AddTag(t);
                        }
                        //add the 'join' between recipe and tag
                        _context.Add(new RecipeTag {
                            RecipeId = recipe.Id, TagId = result.Id
                        });
                        _context.SaveChanges();
                    }
                    var uri = Url.Link("RecipeById", new { recipe.Id });

                    RecipeView rv = RecipeToView(recipe);
                    return(Created(uri, rv));
                }
                else
                {
                    return(BadRequest("Invalid Recipe - cannot add to database"));
                }
            }
            else //model state is invalid
            {
                return(BadRequest("Invalid Model"));
            }
        }
Beispiel #2
0
        public object AddRecipe(Recipe recipe)
        {
            RecipeValidator rv = RecipeValid(recipe);

            int    result       = 0;
            string errorMessage = "";

            if (rv.isValid)
            {
                result = recipeData.AddRecipe(recipe);
            }
            else
            {
                errorMessage = rv.errorMessage;
            }

            return(new
            {
                success = rv.isValid && result == 1,
                errorMessage,
                data = recipe
            });
        }