Example #1
0
        public async Task AddBarsAsync(int cocktailId, List <int> barsId)
        {
            if (!await context.Cocktails.AnyAsync(c => c.Id == cocktailId && c.IsDeleted == false))
            {
                throw new InvalidOperationException(OutputConstants.InvalidId);
            }

            foreach (var id in barsId)
            {
                if (!await context.Bars.AnyAsync(b => b.Id == id && b.IsDeleted == false))
                {
                    throw new InvalidOperationException(OutputConstants.InvalidId);
                }

                var barCocktail = await context.BarCocktails.FirstOrDefaultAsync(bc => bc.CocktailId == cocktailId && bc.BarId == id);

                if (barCocktail == null)
                {
                    barCocktail = barCocktailFactory.Create(id, cocktailId);
                    context.BarCocktails.Add(barCocktail);
                }
            }

            await context.SaveChangesAsync();
        }
        public async Task AddCocktailsAsync(int barId, List <int> cocktailsToAdd)
        {
            if (!await context.Bars.AnyAsync(b => b.Id == barId && b.IsDeleted == false))
            {
                throw new InvalidOperationException(OutputConstants.InvalidId);
            }

            foreach (var cocktailId in cocktailsToAdd)
            {
                var barCocktail = await context.BarCocktails.FirstOrDefaultAsync(b => b.BarId == barId && b.CocktailId == cocktailId);

                barCocktail = barCocktailFactory.Create(barId, cocktailId);
                context.BarCocktails.Add(barCocktail);
            }
            await context.SaveChangesAsync();
        }