Beispiel #1
0
        public IActionResult CreateRecipe([FromBody] RecipeViewModel.Create r)
        {
            r.CreatorId = User.Claims.FirstOrDefault(c => c.Type == "UserId").Value;
            List <string> errorCodes = ControlRecipe(r);

            if (errorCodes.Count > 0)
            {
                return(BadRequest(errorCodes));
            }

            Recipe recipe = RecipeManager.Add(r);

            return(CreatedAtRoute("GetRecipe", new { recipe.Id }, recipe));
            //return CreatedAtRoute("CreateRecipe", recipe);
        }
Beispiel #2
0
        private static List <string> ControlRecipe(RecipeViewModel.Create recipe)
        {
            List <string> errors = new List <string>();

            if (recipe.Name == null)
            {
                errors.Add("NameMissing");
            }
            else if (recipe.Name.Count() < 5 || recipe.Name.Count() > 70)
            {
                errors.Add("NameWrongLength");
            }
            if (recipe.Description == null)
            {
                errors.Add("DescriptionMissing");
            }
            else if (recipe.Description.Count() < 10 || recipe.Description.Count() > 300)
            {
                errors.Add("DescriptionWrongLength");
            }
            if (recipe.Directions == null)
            {
                errors.Add("DirectionsMissing");
            }
            else
            {
                foreach (var d in recipe.Directions)
                {
                    if (d.Order == default(int))
                    {
                        errors.Add("DirectionOrderMissing");
                    }
                    if (d.Description == null)
                    {
                        errors.Add("DirectionDescriptionMissing");
                    }
                    else if (d.Description.Count() < 5 || d.Description.Count() > 120)
                    {
                        errors.Add("DirectionDescriptionWrongLength");
                    }
                }
            }

            return(errors);
        }
Beispiel #3
0
 public Recipe Add(RecipeViewModel.Create r)
 {
     try
     {
         Recipe recipe = new Recipe();
         recipe.Name        = r.Name;
         recipe.Description = r.Description;
         recipe.Directions  = r.Directions;
         recipe.CreatorId   = r.CreatorId;
         recipe.Created     = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
         DbContext.Add(recipe);
         DbContext.SaveChanges();
         return(recipe);
     }
     catch (Exception)
     {
         throw;
     }
 }