Example #1
0
        public void SaveRecipe(Services.IRecipe recipe)
        {
            using var context = new HomeAppDbContext(myDbOptions);

            // I create the library
            var dbRecipe = new DbRecipe
            {
                Id    = recipe.Id,
                Name  = recipe.Name,
                Steps = recipe.Steps.Select(rs => Convert(rs)).ToList()
            };

            var ingredients = recipe.Ingredients.Select(r => new DbIngredient
            {
                Recipe           = dbRecipe,
                ProductId        = r.ProductId,
                UnitQuantity     = r.UnitQuantity,
                UnitQuantityType = r.UnitQuantityType
            }).ToList();

            // Linking the books (Library2Book table) to the library
            dbRecipe.Ingredients.AddRange(ingredients);

            // Adding the data to the DbContext.
            context.Recipes.Add(dbRecipe);

            // Save the changes and everything should be working!
            context.SaveChanges();
        }
Example #2
0
        public Task <bool> AddIngredient(Guid recipeId, IIngredient ingredient)
        {
            return(Task.Run(() =>
            {
                using var context = new HomeAppDbContext(myDbOptions);

                DbProduct product = context.Products.FirstOrDefault(p => p.Id.Equals(ingredient.ProductId));
                if (product == null)
                {
                    return false;
                }
                DbRecipe recipe = context.Recipes.FirstOrDefault(p => p.Id.Equals(recipeId));
                if (product == null)
                {
                    return false;
                }
                context.Ingredients.AddAsync(new DbIngredient
                {
                    Product = product,
                    Recipe = recipe,
                    UnitQuantity = ingredient.UnitQuantity,
                    UnitQuantityType = ingredient.UnitQuantityType
                });
                context.SaveChanges();
                return true;
            }));
        }
Example #3
0
 private RecipeApiModel DbRecipeToRecipe(DbRecipe dbRecipe)
 {
     return(new RecipeApiModel
     {
         Id = dbRecipe.Id,
         Name = dbRecipe.Name,
         Steps = dbRecipe.Steps.Select(r => Convert(r)),
         Ingredients = dbRecipe.Ingredients.Select(ConvertDbIngredient)
     });
 }
Example #4
0
 private RecipeApiModel ConvertDbRecipeToRecipe(DbRecipe recipe)
 {
     return(new RecipeApiModel
     {
         Name = recipe.Name,
         Id = recipe.Id,
         Type = recipe.Type,
         Steps = recipe.Steps.Select(s => Convert(s)),
         Ingredients = recipe.Ingredients.Select(i => ConvertDbIngredient(i))
     });
 }