public async Task ThrowArgumentException_When_CocktailAlreadyExists()
        {
            var testCocktailName      = "TestCocktailName";
            var options               = TestUtilities.GetOptions(nameof(ThrowArgumentException_When_CocktailAlreadyExists));
            var cocktailCreateRequest = new CocktailCreateRequest()
            {
                Name = testCocktailName
            };

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(new CocktailEntity()
                {
                    Name = testCocktailName
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                var sut = new CocktailService(assertContext);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.Create(cocktailCreateRequest));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(CocktailCreateRequest cocktail)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(cocktail));
            }


            if (cocktail.Image != null)
            {
                var(extension, isValid) = GetFileExtension(cocktail.Image.ContentType);

                if (!isValid)
                {
                    TempData["ErrorMessage"] = "Invalid file type, please upload image!";
                    return(RedirectToAction("Create", "Bars"));
                }
                string destinationFolder = Path.Combine(hostingEnvironment.WebRootPath, "images/cocktails");
                string fileName          = Guid.NewGuid().ToString() + "_" + cocktail.Image.FileName;
                string imagePath         = Path.Combine(destinationFolder, fileName);
                cocktail.Image.CopyTo(new FileStream(imagePath, FileMode.Create));
                cocktail.ImagePath = $"/images/cocktails/" + fileName;
            }

            await this.cocktailService.Create(cocktail);

            return(RedirectToAction("Index", "Cocktails"));
        }
 public static CocktailEntity ToEntity(this CocktailCreateRequest contract)
 {
     if (contract == null)
     {
         return(null);
     }
     return(new CocktailEntity
     {
         Name = contract.Name,
         Recipe = contract.Recipe,
         IsHidden = contract.IsHidden,
         ImagePath = contract.ImagePath,
     });
 }
        public async Task IncrementIngredientsCounter_When_CocktailIsCreated()
        {
            var testCocktailId      = 5;
            var testIngredientId    = 3;
            var testIngredidentName = "TestIngredient";

            var cocktailIngredient = new CocktailIngredientEntity()
            {
                CocktailEntityId   = testCocktailId,
                IngredientEntityId = testIngredientId
            };

            var cocktailCreateRequest = new CocktailCreateRequest()
            {
                Name        = testIngredidentName,
                Recipe      = "TestRecipe",
                Ingredients = new List <int>()
                {
                    testIngredientId
                }
            };

            var ingredientEntity = new IngredientEntity()
            {
                Id        = testIngredientId,
                Name      = testIngredidentName,
                TimesUsed = 5,
            };
            var options = TestUtilities.GetOptions(nameof(IncrementIngredientsCounter_When_CocktailIsCreated));

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Ingredients.AddAsync(ingredientEntity);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                var sut = new CocktailService(assertContext);

                await sut.Create(cocktailCreateRequest);

                var testIngredient = await assertContext.Ingredients.SingleOrDefaultAsync(x => x.Id == testIngredientId);

                Assert.AreEqual(6, testIngredient.TimesUsed);
            }
        }
Esempio n. 5
0
        public async Task <Cocktail> Create(CocktailCreateRequest cocktail)
        {
            if (await this.context.Cocktails.SingleOrDefaultAsync(x => x.Name == cocktail.Name) != null)
            {
                throw new ArgumentException("Cocktail already exists.");
            }
            var cocktailEntity = cocktail.ToEntity();

            await this.context.Cocktails.AddAsync(cocktailEntity);

            await this.context.SaveChangesAsync();

            AddIngredients(cocktailEntity.Id, cocktail.Ingredients);
            await IncrementIngredientCounter(cocktail.Ingredients);

            return(cocktailEntity.ToContract());
        }
        public async Task CreateCocktail_When_SuchCocktailDoesNotExist()
        {
            var testCocktailName   = "TestCocktailName";
            var testCocktailRecipe = "Test cocktail recipe";

            var options = TestUtilities.GetOptions(nameof(CreateCocktail_When_SuchCocktailDoesNotExist));

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Cocktails.AddAsync(new CocktailEntity()
                {
                    Name = "RandomCocktailName"
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var actContext = new AppDBContext(options))
            {
                var sut = new CocktailService(actContext);
                var cocktailCreateRequest = new CocktailCreateRequest()
                {
                    Name = testCocktailName, Recipe = testCocktailRecipe
                };
                var cocktail = sut.Create(cocktailCreateRequest);
                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                Assert.AreEqual(2, await assertContext.Cocktails.CountAsync());
                var cocktail = await assertContext.Cocktails.FirstOrDefaultAsync(x => x.Name == testCocktailName);

                Assert.IsNotNull(cocktail);
                Assert.AreEqual(testCocktailRecipe, cocktail.Recipe);
            }
        }