コード例 #1
0
        public async Task <IActionResult> CreateRecipe([FromBody] RecipeModel model)
        {
            string recipeError = ValidateRecipe(model);

            if (!String.IsNullOrEmpty(recipeError))
            {
                return(BadRequest(recipeError));
            }

            // create Recipe
            Recipe recipe = new Recipe();

            recipe.Name            = model.name;
            recipe.PreparationTime = model.preparationTime;
            recipe.Servings        = model.servings;
            context.Recipe.Add(recipe);

            // create RecipeFrontImage
            RecipeFrontImage recipeFrontImage = new RecipeFrontImage();

            recipeFrontImage.FrontImage = FileHelper.EncodeString(model.frontImage);
            context.RecipeFrontImage.Add(recipeFrontImage);
            recipeFrontImage.RecipeNavigation = recipe;

            // create multiple RecipeIngredientMeasure
            foreach (IngredientModel ingredient in model.ingredients)
            {
                RecipeIngredientMeasure recipeIngredientMeasure = new RecipeIngredientMeasure();
                recipeIngredientMeasure.Ingredient       = Int32.Parse(ingredient.ingredientValue);
                recipeIngredientMeasure.Amount           = ingredient.amount;
                recipeIngredientMeasure.Measure          = Int32.Parse(ingredient.measureValue);
                recipeIngredientMeasure.RecipeNavigation = recipe;
                context.RecipeIngredientMeasure.Add(recipeIngredientMeasure);
            }

            // create multiple RecipeIngredientMeasure
            foreach (DirectionModel direction in model.directions)
            {
                RecipeDirection recipeDirection = new RecipeDirection();
                recipeDirection.Sort             = direction.sortNumber;
                recipeDirection.Direction        = direction.description.Trim();
                recipeDirection.RecipeNavigation = recipe;
                context.RecipeDirection.Add(recipeDirection);
            }

            // create multiple RecipeCategory
            foreach (FilterModel category in model.categories)
            {
                RecipeCategory recipeCategory = new RecipeCategory();
                recipeCategory.Category         = Int32.Parse(category.value);
                recipeCategory.RecipeNavigation = recipe;
                context.RecipeCategory.Add(recipeCategory);
            }

            await context.SaveChangesAsync();

            return(Ok(recipe.Id));
        }
コード例 #2
0
        public async Task <IActionResult> UpdateRecipe(int recipeId, [FromBody] RecipeModel model)
        {
            string recipeError = ValidateRecipe(model);

            if (!String.IsNullOrEmpty(recipeError))
            {
                return(BadRequest(recipeError));
            }

            // update Recipe
            Recipe recipe = await context.Recipe.SingleOrDefaultAsync(x => x.Id == recipeId);

            if (recipe == null)
            {
                return(NotFound("Not Found Recipe"));
            }
            recipe.Name = model.name;
            context.Recipe.Update(recipe);

            // update RecipeFrontImage
            RecipeFrontImage recipeFrontImage = await context.RecipeFrontImage.SingleOrDefaultAsync(x => x.Recipe == recipeId);

            if (recipeFrontImage == null)
            {
                return(NotFound("Not Found Recipe Front Image"));
            }
            recipeFrontImage.FrontImage = FileHelper.EncodeString(model.frontImage);
            context.RecipeFrontImage.Update(recipeFrontImage);

            // delete old RecipeIngredientMeasures
            var recipeIngredientMeasures = await context.RecipeIngredientMeasure.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeIngredientMeasure.RemoveRange(recipeIngredientMeasures);
            // create multiple RecipeIngredientMeasure
            foreach (IngredientModel ingredient in model.ingredients)
            {
                RecipeIngredientMeasure recipeIngredientMeasure = new RecipeIngredientMeasure();
                recipeIngredientMeasure.Ingredient       = Int32.Parse(ingredient.ingredientValue);
                recipeIngredientMeasure.Amount           = ingredient.amount;
                recipeIngredientMeasure.Measure          = Int32.Parse(ingredient.measureValue);
                recipeIngredientMeasure.RecipeNavigation = recipe;
                context.RecipeIngredientMeasure.Add(recipeIngredientMeasure);
            }

            // delete old RecipeDirections
            var recipeDirections = await context.RecipeDirection.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeDirection.RemoveRange(recipeDirections);
            // create multiple RecipeDirections
            foreach (DirectionModel direction in model.directions)
            {
                RecipeDirection recipeDirection = new RecipeDirection();
                recipeDirection.Sort             = direction.sortNumber;
                recipeDirection.Direction        = direction.description.Trim();
                recipeDirection.RecipeNavigation = recipe;
                context.RecipeDirection.Add(recipeDirection);
            }

            // delete old RecipeCategory
            var recipeCategories = await context.RecipeCategory.Where(x => x.Recipe == recipeId).ToListAsync();

            context.RecipeCategory.RemoveRange(recipeCategories);
            // create multiple RecipeCategory
            foreach (FilterModel category in model.categories)
            {
                RecipeCategory recipeCategory = new RecipeCategory();
                recipeCategory.Category         = Int32.Parse(category.value);
                recipeCategory.RecipeNavigation = recipe;
                context.RecipeCategory.Add(recipeCategory);
            }

            await context.SaveChangesAsync();

            return(Ok(recipe.Id));
        }