示例#1
0
        public async Task <bool> AddIngredientToDessertAsync(string dessertId, string name)
        {
            var isAdded = true;

            var ingredientId = await this.ingredientsService.GetIngredientIdByNameAsync(name);

            if (ingredientId == null)
            {
                return(!isAdded);
            }

            var isAlreadyAdded = await this.dessertIngredientsRepository
                                 .All()
                                 .AnyAsync(di => di.IngredientId == ingredientId && di.DessertId == dessertId);

            if (isAlreadyAdded)
            {
                return(!isAdded);
            }

            var dessertIngredient = new DessertIngredient()
            {
                DessertId    = dessertId,
                IngredientId = ingredientId,
            };

            await this.dessertIngredientsRepository.AddAsync(dessertIngredient);

            await this.dessertIngredientsRepository.SaveChangesAsync();

            return(isAdded);
        }
        public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (!dbContext.Desserts.Any())
            {
                var dessertsData = JsonConvert
                                   .DeserializeObject <List <DessertDto> >(File.ReadAllText(GlobalConstants.DessertSeederPath))
                                   .ToList();

                List <Dessert>           desserts           = new List <Dessert>();
                List <DessertIngredient> dessertIngredients = new List <DessertIngredient>();
                List <DessertTag>        dessertTags        = new List <DessertTag>();

                foreach (var currentDessertData in dessertsData)
                {
                    var category = await dbContext.Categories
                                   .FirstOrDefaultAsync(c => c.Name == currentDessertData.Category);

                    var dessert = new Dessert()
                    {
                        Name        = currentDessertData.Name,
                        Description = currentDessertData.Description,
                        Price       = currentDessertData.Price,
                        Picture     = currentDessertData.Picture,
                    };

                    if (category != null)
                    {
                        dessert.CategoryId = category.Id;
                    }

                    foreach (var currentTag in currentDessertData.DessertTags)
                    {
                        var tag = await dbContext.Tags
                                  .FirstOrDefaultAsync(sp => sp.Name == currentTag);

                        var dessertTag = new DessertTag()
                        {
                            DessertId = dessert.Id,
                            TagId     = tag.Id,
                        };

                        dessertTags.Add(dessertTag);
                    }

                    foreach (var currentIngredient in currentDessertData.DessertIngredients)
                    {
                        var ingredient = await dbContext.Ingredients
                                         .FirstOrDefaultAsync(sp => sp.Name == currentIngredient);

                        var dessertIngredient = new DessertIngredient()
                        {
                            DessertId    = dessert.Id,
                            IngredientId = ingredient.Id,
                        };

                        dessertIngredients.Add(dessertIngredient);
                    }

                    desserts.Add(dessert);
                }

                await dbContext.DessertIngredients.AddRangeAsync(dessertIngredients);

                await dbContext.DessertTags.AddRangeAsync(dessertTags);

                await dbContext.Desserts.AddRangeAsync(desserts);

                await dbContext.SaveChangesAsync();
            }
        }