コード例 #1
0
        public Recipe Create(CreateRecipeBindingModel bindingModel, ApplicationUser author)
        {
            this.ValidateTitle(bindingModel.Title);
            this.ValidateSteps(bindingModel.StepsTexts, bindingModel.StepsMinutes);
            this.ValidateIngredients(
                bindingModel.IngredientsNames,
                bindingModel.IngredientsMeasurementTypes,
                bindingModel.IngredientsQuantities);
            this.ValidateAuthor(author);
            this.ValidateCookingRequiredMinutesForMinutes(bindingModel.MinutesRequiredForCooking);

            var ingredients = this.CreateIngredients(
                bindingModel.IngredientsNames,
                bindingModel.IngredientsMeasurementTypes,
                bindingModel.IngredientsQuantities);
            var images = this.CreateImages(bindingModel.Images, author);
            var steps  = this.CreateSteps(bindingModel.StepsTexts, bindingModel.StepsMinutes);

            var recipe = new Recipe()
            {
                Title                 = bindingModel.Title,
                Ingredients           = ingredients,
                Images                = images,
                Steps                 = steps,
                MinutesRequiredToCook = bindingModel.MinutesRequiredForCooking,
                Author                = author
            };

            this.Set.Add(recipe);
            this.SaveChanges();

            return(recipe);
        }
コード例 #2
0
        public ActionResult Create(CreateRecipeBindingModel createRecipeBindingModel)
        {
            var userId = this.User.Identity.GetUserId();
            var user   = this.context.Users.Find(userId);

            try
            {
                this.recipeService.Create(createRecipeBindingModel, user);
            }
            catch (Exception exception)
            {
                this.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var error = exception.Message;
                return(Json(
                           new
                {
                    error
                }));
            }

            return(null);
        }