Пример #1
0
        public async Task <IActionResult> Create(BarCreateRequest bar)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(bar));
            }

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

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

            var barContract = await this.barService.Create(bar);

            return(RedirectToAction("Index", "Bars"));
        }
Пример #2
0
        public async Task AddCocktailsToBar_When_BarIsCreated()
        {
            var testBarName    = "TestBarName";
            var testBarAddress = "TestBarAddress";

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

            using (var actContext = new AppDBContext(options))
            {
                var sut = new BarService(actContext);
                var barCreateRequest = new BarCreateRequest()
                {
                    Name      = testBarName,
                    Address   = testBarAddress,
                    Cocktails = new List <int>()
                    {
                        1, 2, 3
                    }
                };
                var bar = sut.Create(barCreateRequest);

                await actContext.SaveChangesAsync();

                var barEntity = await actContext.Bars.FirstOrDefaultAsync(x => x.Name == testBarName);

                Assert.AreEqual(3, barEntity.BarCocktails.Count());
            }
        }
Пример #3
0
        public async Task ThrowArgumentException_When_BarAlreadyExists()
        {
            var testBarName      = "TestBarName";
            var options          = TestUtilities.GetOptions(nameof(ThrowArgumentException_When_BarAlreadyExists));
            var barCreateRequest = new BarCreateRequest()
            {
                Name = testBarName
            };

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Bars.AddAsync(new BarEntity()
                {
                    Name = testBarName
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                var sut = new BarService(assertContext);
                await Assert.ThrowsExceptionAsync <ArgumentException>(() => sut.Create(barCreateRequest));
            }
        }
Пример #4
0
        public static BarEntity ToEntity(this BarCreateRequest contract)
        {
            if (contract == null)
            {
                return(null);
            }

            return(new BarEntity
            {
                Name = contract.Name,
                Address = contract.Address,
                IsHidden = contract.IsHidden,
                ImagePath = contract.ImagePath
            });
        }
Пример #5
0
        public async Task <Bar> Create(BarCreateRequest bar)
        {
            if (await this.context.Bars.SingleOrDefaultAsync(x => x.Name == bar.Name) != null)
            {
                throw new ArgumentException("Bar already exists.");
            }

            var barEntity = bar.ToEntity();

            await this.context.Bars.AddAsync(barEntity);

            AddCocktails(barEntity.Id, bar.Cocktails);
            await this.context.SaveChangesAsync();

            return(barEntity.ToContract());
        }
Пример #6
0
        public async Task CreateBar_When_SuchBarDoesNotExist()
        {
            var testBarName    = "TestBarName";
            var testBarAddress = "TestBarAddress";

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

            using (var arrangeContext = new AppDBContext(options))
            {
                await arrangeContext.Bars.AddAsync(new BarEntity()
                {
                    Name = "RandomBarName"
                });

                await arrangeContext.SaveChangesAsync();
            }

            using (var actContext = new AppDBContext(options))
            {
                var sut = new BarService(actContext);
                var barCreateRequest = new BarCreateRequest()
                {
                    Name = testBarName, Address = testBarAddress
                };
                var bar = sut.Create(barCreateRequest);
                await actContext.SaveChangesAsync();
            }

            using (var assertContext = new AppDBContext(options))
            {
                Assert.AreEqual(2, await assertContext.Bars.CountAsync());
                var bar = await assertContext.Bars.FirstOrDefaultAsync(x => x.Name == testBarName);

                Assert.IsNotNull(bar);
                Assert.AreEqual(testBarAddress, bar.Address);
            }
        }