示例#1
0
        public HttpResponseMessage AddIngredientRecipes([FromBody] RecipeIngredientDTO ing)
        {
            var ingredient = new RecipeIngredient()
            {
                IngredientId = ing.IngredientId,
                RecipeId     = ing.RecipeId,
                Quantity     = ing.Quantity
            };

            using (CookBookEntities cookbook = new CookBookEntities())
            {
                cookbook.RecipeIngredients.Add(ingredient);
                cookbook.SaveChanges();
            }

            return(Request.CreateResponse(HttpStatusCode.OK, ingredient));
        }
示例#2
0
        public static RecipeDTO RecipeToRecipeDTO(Recipe recipe)
        {
            RecipeDTO recipeDTO = new RecipeDTO();

            recipeDTO.Id          = recipe.Id;
            recipeDTO.Name        = recipe.Name;
            recipeDTO.Description = recipe.Description;
            recipeDTO.Ingredients = recipe.Ingredients.Select(x =>
            {
                RecipeIngredientDTO riDTO = new RecipeIngredientDTO();
                riDTO.Id             = x.Id;
                riDTO.IngredientId   = x.IngredientId;
                riDTO.IngredientName = x.Ingredient.Name;
                riDTO.Quantity       = x.Quantity;
                return(riDTO);
            }).ToArray();
            return(recipeDTO);
        }
示例#3
0
        public async void Can_post_recipe()
        {
            using var context = new RecAPIContext(ContextOptions);
            var controller = new RecipesController(context);

            var riFusilliDTO = new RecipeIngredientDTO
            {
                IngredientId   = 2,
                IngredientName = "Fusilli",
                Amount         = 200,
                Unit           = (Unit)1
            };

            var riKetchupDTO = new RecipeIngredientDTO
            {
                IngredientId   = 3,
                IngredientName = "Ketchup",
                Amount         = 200,
                Unit           = (Unit)1
            };

            var recipeDTO = new RecipeDTO
            {
                Name              = "Pasta with ketchup",
                Description       = "Better than with garlic",
                Instructions      = "Boil pasta. Add ketchup.",
                RecipeIngredients = new List <RecipeIngredientDTO> {
                    riFusilliDTO, riKetchupDTO
                }
            };

            var recipeActionResult = await controller.PostRecipe(recipeDTO);

            var recipe = (recipeActionResult.Result as CreatedAtActionResult).Value as RecipeDTO;

            Assert.Equal("Pasta with ketchup", recipe.Name);
            Assert.Equal("Fusilli", recipe.RecipeIngredients.FirstOrDefault().IngredientName);
        }