Пример #1
0
        public HttpResponseMessage PostIngredient(string name)
        {
            var ingredient = new Ingredient { Name = name };
            IngredientRepo.Add(ingredient);
            IngredientRepo.SaveContext();

            var response = Request.CreateResponse(HttpStatusCode.Created);
            return response;
        }
Пример #2
0
        public HttpResponseMessage PostRecipe([FromBody] RecipePostData postData)
        {
            var instructions = postData.Instructions;
            var ingredients = postData.Ingredients;
            var equipment = postData.Equipment;

            var recipe = new Recipe
            {
                Name = postData.name,
                Description = postData.description,
                MealType = postData.mealType,
                PreparationTime = postData.prepTime,
                CookTime = postData.cookTime,
                NumberOfServings = postData.numberOfServings,
                Author = postData.author,
                ImageSource = postData.imageSource
            };

            RecipeRepo.Add(recipe);
            RecipeRepo.SaveContext();

            foreach (var detailedInstruction in instructions)
            {
                var instruction = new Instruction
                {
                    StepNumber = detailedInstruction.StepNumber,
                    StepDescription = detailedInstruction.StepDescription,
                    RecipeID = recipe.RecipeID
                };
                InstructionRepo.Add(instruction);
            }

            foreach (var detailedRecipeIngredient in ingredients)
            {
                var ingredient = IngredientRepo.GetIngredientByName(detailedRecipeIngredient.Name);

                if (ingredient == null)
                {
                    ingredient = new Ingredient
                    {
                        Name = detailedRecipeIngredient.Name
                    };

                    IngredientRepo.Add(ingredient);
                    IngredientRepo.SaveContext();
                }

                var recipeIngredient = new RecipeIngredient
                {
                    RecipeID = recipe.RecipeID,
                    IngredientID = ingredient.IngredientID,
                    Amount = detailedRecipeIngredient.Amount,
                    Units = detailedRecipeIngredient.Units,
                    Description = detailedRecipeIngredient.Description
                };

                RecipeIngredientRepo.Add(recipeIngredient);
            }

            foreach (var detailedEquipment in equipment)
            {
                var singleEquipment = EquipmentRepo.GetEquipmentByName(detailedEquipment.Name);

                if (singleEquipment == null)
                {
                    singleEquipment = new Equipment
                    {
                        Name = detailedEquipment.Name,
                    };

                    EquipmentRepo.Add(singleEquipment);
                    EquipmentRepo.SaveContext();
                }

                var recipeEquipment = new RecipeEquipment
                {
                    RecipeID = recipe.RecipeID,
                    EquipmentID = singleEquipment.EquipmentID
                };

                RecipeEquipmentRepo.Add(recipeEquipment);
            }

            InstructionRepo.SaveContext();
            RecipeIngredientRepo.SaveContext();
            RecipeEquipmentRepo.SaveContext();

            var response = Request.CreateResponse(HttpStatusCode.Created, new DetailedRecipe(recipe));
            return response;
        }