public async Task Remove(List <string> cocktails, string barId) { if (cocktails != null) { foreach (var item in cocktails) { var cocktailBar = new CocktailBar { BarId = barId, CocktailId = item, }; this.context.CocktailBars.Remove(cocktailBar); } await this.context.SaveChangesAsync(); } }
public async Task RemoveCorrectly() { var options = TestUtilities.GetOptions(nameof(RemoveCorrectly)); var bar = new Bar() { Id = "8", Name = "Bar", }; var cocktail = new Cocktail() { Id = "9", Name = "Cocktail", }; var barcocktail = new CocktailBar() { BarId = "8", CocktailId = "9" }; var cocktailIds = new List <string>(); cocktailIds.Add(cocktail.Id); using (var actContext = new IriOnCocktailServiceDbContext(options)) { await actContext.Cocktails.AddAsync(cocktail); await actContext.Bars.AddAsync(bar); await actContext.CocktailBars.AddAsync(barcocktail); await actContext.SaveChangesAsync(); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { var sut = new BarCocktailsService(assertContext); await sut.Remove(cocktailIds, bar.Id); Assert.AreEqual(0, assertContext.CocktailBars.Where(cb => cb.BarId == bar.Id).Count()); } }
/// <summary> /// Update the bar /// </summary> /// <param name="id">bar ID, Guid</param> /// <param name="barDTO">Object of updated information, BarDTO</param> /// <returns>The bar, DTO</returns> public async Task <BarDTO> UpdateAsync(Guid id, BarDTO barDTO) { try { var bar = await _context.Bars .FirstOrDefaultAsync(b => b.Id == id); if (bar == null) { return(null); } bar.Address = barDTO.Address; bar.Country = barDTO.Country; bar.District = barDTO.District; bar.Email = barDTO.Email; bar.ImageSrc = barDTO.ImageSrc; bar.Name = barDTO.Name; bar.Phone = barDTO.Phone; bar.Town = barDTO.Town; _context.Update(bar); await _context.SaveChangesAsync(); foreach (var item in barDTO.Cocktails) { var dbItem = await _context.CocktailBars .Include(c => c.Bar) .Include(c => c.Cocktail) .FirstOrDefaultAsync(i => i.CocktailId == item.CocktailId && i.BarId == id); if (dbItem == null) { var cocktail = new CocktailBar();// { CocktailId = item.CocktailId, BarId = id }; cocktail.Bar = await _context.Bars.FindAsync(id); cocktail.BarId = cocktail.Bar.Id; cocktail.Cocktail = await _context.Cocktails.FindAsync(item.CocktailId); cocktail.CocktailId = cocktail.Cocktail.Id; await _context.CocktailBars.AddAsync(cocktail); } else { if (item.Remove) { _context.CocktailBars.Remove(dbItem); } } } await _context.SaveChangesAsync(); return(barDTO); } catch (Exception) { throw new ArgumentNullException("Failed to update"); } }
public async Task GetDefinedBarCocktails_WhenSuccessful() { //Arrange var options = Utils.GetOptions(nameof(GetDefinedBarCocktails_WhenSuccessful)); var record = new Bar() { Name = "BestBar", Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, Address = "Галичица 17.", Country = "България.", District = "Лозенец.", Email = "*****@*****.**", Phone = "088888888.", Town = "София.", LocationId = null, }; var cocktailList = new List <Cocktail>(); for (int i = 1; i <= 20; i++) { var cocktail = new Cocktail() { Name = "BestCocktail" + i, Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, IsAlcoholic = false }; cocktailList.Add(cocktail); } using (var context = new BCcontext(options)) { await context.Bars.AddAsync(record); await context.Cocktails.AddRangeAsync(cocktailList); await context.SaveChangesAsync(); var bar = await context.Bars.FirstOrDefaultAsync(); var cocktails = await context.Cocktails.ToListAsync(); foreach (var item in cocktails) { var join = new CocktailBar() { Bar = bar, Cocktail = item, }; await context.CocktailBars.AddAsync(join); } await context.SaveChangesAsync(); } var mockMapper = new Mock <IBarMapper>(); var http = new Mock <IHttpClientFactory>(); var coctailMapper = new Mock <ICocktailMapper>(); coctailMapper.Setup((x) => x.MapEntityToDTO(It.IsAny <Cocktail>())) .Returns((Cocktail b) => new CocktailDTO() { Id = b.Id, Name = b.Name, Rating = b.Rating, TimesRated = b.TimesRated, ImageSrc = b.ImageSrc, IsDeleted = b.IsDeleted, IsAlcoholic = b.IsAlcoholic }); //Act & Assert using (var context = new BCcontext(options)) { var sut = new BarsService(context, mockMapper.Object, http.Object, coctailMapper.Object); var dbResult = await context.CocktailBars .Include(c => c.Cocktail) .Skip(8).Take(8) .ToListAsync(); var dbBar = await context.Bars.FirstOrDefaultAsync(); var result = await sut.GetCocktailsAsync(dbBar.Id, "1", "8", null, true); Assert.AreEqual(result.Count(), dbResult.Count()); foreach (var item in dbResult) { Assert.IsNotNull(result.FirstOrDefault(r => r.Name == item.Cocktail.Name)); } } }
public async Task ThrowArgumentException_WhenParametersIncorrect() { //Arrange var options = Utils.GetOptions(nameof(ThrowArgumentException_WhenParametersIncorrect)); var record = new Bar() { Name = "BestBar", Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, Address = "Галичица 17.", Country = "България.", District = "Лозенец.", Email = "*****@*****.**", Phone = "088888888.", Town = "София.", LocationId = null, }; var cocktailList = new List <Cocktail>(); for (int i = 1; i <= 20; i++) { var cocktail = new Cocktail() { Name = "BestCocktail" + i, Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, IsAlcoholic = false }; cocktailList.Add(cocktail); } using (var context = new BCcontext(options)) { await context.Bars.AddAsync(record); await context.Cocktails.AddRangeAsync(cocktailList); await context.SaveChangesAsync(); var bar = await context.Bars.FirstOrDefaultAsync(); var cocktails = await context.Cocktails.ToListAsync(); foreach (var item in cocktails) { var join = new CocktailBar() { Bar = bar, Cocktail = item, }; await context.CocktailBars.AddAsync(join); } await context.SaveChangesAsync(); } var mockMapper = new Mock <IBarMapper>(); var http = new Mock <IHttpClientFactory>(); var coctailMapper = new Mock <ICocktailMapper>(); coctailMapper.Setup((x) => x.MapEntityToDTO(It.IsAny <Cocktail>())) .Returns((Cocktail b) => new CocktailDTO() { Id = b.Id, Name = b.Name, Rating = b.Rating, TimesRated = b.TimesRated, ImageSrc = b.ImageSrc, IsDeleted = b.IsDeleted, IsAlcoholic = b.IsAlcoholic }); //Act & Assert using (var context = new BCcontext(options)) { var sut = new BarsService(context, mockMapper.Object, http.Object, coctailMapper.Object); var dbBar = await context.Bars.FirstOrDefaultAsync(); await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.GetCocktailsAsync(dbBar.Id, string.Empty, "8", null, true)); } }
public async Task RemoveCocktail_WhenSuccessful() { //Arrange var options = Utils.GetOptions(nameof(RemoveCocktail_WhenSuccessful)); var record = new Bar() { Name = "BestBar", Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, Address = "Галичица 17.", Country = "България.", District = "Лозенец.", Email = "*****@*****.**", Phone = "088888888.", Town = "София.", LocationId = null, }; var cocktail1 = new Cocktail() { Name = "SomeCocktail", Rating = 3, TimesRated = 1, ImageSrc = null, IsDeleted = false, }; var cocktail2 = new Cocktail() { Name = "SomeOtherCocktail", Rating = 3, TimesRated = 1, ImageSrc = null, IsDeleted = false, }; using (var context = new BCcontext(options)) { await context.Cocktails.AddAsync(cocktail1); await context.Cocktails.AddAsync(cocktail2); await context.Bars.AddAsync(record); await context.SaveChangesAsync(); var conection1 = new CocktailBar() { Cocktail = await context.Cocktails.FirstOrDefaultAsync(c => c.Name == "SomeCocktail"), Bar = await context.Bars.FirstOrDefaultAsync(b => b.Name == "BestBar") }; var conection2 = new CocktailBar() { Cocktail = await context.Cocktails.FirstOrDefaultAsync(c => c.Name == "SomeOtherCocktail"), Bar = await context.Bars.FirstOrDefaultAsync(b => b.Name == "BestBar") }; await context.CocktailBars.AddAsync(conection1); await context.CocktailBars.AddAsync(conection2); await context.SaveChangesAsync(); } var bar = new BarDTO() { Name = "BestBarAgain", Rating = 4, TimesRated = 1, ImageSrc = null, IsDeleted = false, Address = "Галичица 17", Country = "България", District = "Лозенец", Email = "*****@*****.**", Phone = "088888888", Town = "София", Cocktails = new List <CocktailBarDTO>() }; var mockMapper = new Mock <IBarMapper>(); mockMapper.Setup((x) => x.MapEntityToDTO(It.IsAny <Bar>())) .Returns((Bar b) => new BarDTO() { Id = b.Id, Name = b.Name, Rating = b.Rating, TimesRated = b.TimesRated, ImageSrc = b.ImageSrc, IsDeleted = b.IsDeleted, Address = b.Address, Country = b.Country, District = b.District, Email = b.Email, LocationId = b.LocationId, Phone = b.Phone, Town = b.Town }); mockMapper.Setup((x) => x.MapDTOToEntity(It.IsAny <BarDTO>())) .Returns((BarDTO b) => new Bar() { Id = b.Id, Name = b.Name, Rating = b.Rating, TimesRated = b.TimesRated, ImageSrc = b.ImageSrc, IsDeleted = b.IsDeleted, Address = b.Address, Country = b.Country, District = b.District, Email = b.Email, LocationId = b.LocationId, Phone = b.Phone, Town = b.Town, }); var http = new Mock <IHttpClientFactory>(); var coctailMapper = new Mock <ICocktailMapper>(); //Act & Assert using (var context = new BCcontext(options)) { var contextCoctail1 = await context.Cocktails.FirstOrDefaultAsync(c => c.Name == "SomeCocktail"); var cocktailBarDTO1 = new CocktailBarDTO() { CocktailId = contextCoctail1.Id, CocktailName = contextCoctail1.Name, Remove = false }; var contextCoctail2 = await context.Cocktails.FirstOrDefaultAsync(c => c.Name == "SomeOtherCocktail"); var cocktailBarDTO2 = new CocktailBarDTO() { CocktailId = contextCoctail2.Id, CocktailName = contextCoctail2.Name, Remove = true }; bar.Cocktails.Add(cocktailBarDTO1); bar.Cocktails.Add(cocktailBarDTO2); var sut = new BarsService(context, mockMapper.Object, http.Object, coctailMapper.Object); var dbResultOld = await context.Bars.FirstOrDefaultAsync(b => b.Name == "BestBar"); var result = await sut.UpdateAsync(dbResultOld.Id, bar); var dbResult = await context.Bars.Include(b => b.Cocktails).FirstOrDefaultAsync(b => b.Name == "BestBarAgain"); Assert.AreEqual(dbResult.Name, bar.Name); Assert.AreEqual(dbResult.Rating, bar.Rating); Assert.AreEqual(dbResult.TimesRated, bar.TimesRated); Assert.AreEqual(dbResult.ImageSrc, bar.ImageSrc); Assert.AreEqual(dbResult.IsDeleted, bar.IsDeleted); Assert.AreEqual(dbResult.Address, bar.Address); Assert.AreEqual(dbResult.Country, bar.Country); Assert.AreEqual(dbResult.District, bar.District); Assert.AreEqual(dbResult.Email, bar.Email); Assert.AreEqual(dbResult.Phone, bar.Phone); Assert.AreEqual(dbResult.Town, bar.Town); Assert.AreEqual(dbResult.Cocktails.Count(), 1); Assert.AreEqual(dbResult.Cocktails.Where(c => c.CocktailId == contextCoctail1.Id).Count(), 1); } }
public async Task GetAllContainedCocktailsDTOCorrectly() { var options = TestUtilities.GetOptions(nameof(GetAllContainedCocktailsDTOCorrectly)); var ingredientServiceMock = new Mock <ICocktailIngredientService>(); var cocktailMapperToDTOMock = new Mock <IDTOServiceMapper <CocktailDTO, Cocktail> >(); var cocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, CocktailDTO> >(); var commentMapperToDTOMock = new Mock <IDTOServiceMapper <CommentDTO, CocktailComment> >(); var commentMapperMock = new Mock <IDTOServiceMapper <CocktailComment, CommentDTO> >(); var addCocktailMapperMock = new Mock <IDTOServiceMapper <Cocktail, AddCocktailDTO> >(); var cocktailRatingToDTOMock = new Mock <IDTOServiceMapper <RatingDTO, CocktailRating> >(); var bar = new Bar() { Id = "1", Name = "name", Address = "address", Motto = "motto", PicUrl = "picture", }; var cocktail = new Cocktail() { Name = "Name", Id = "1", Motto = "Motto", PicUrl = "Pic", }; var cocktail2 = new Cocktail() { Name = "Name", Id = "2", Motto = "Motto", PicUrl = "Pic", }; var cocktailBar = new CocktailBar { CocktailId = "1", BarId = "1", }; var cocktailBar2 = new CocktailBar { CocktailId = "2", BarId = "1", }; //Act //cocktailMapperMock.Setup(m => m.MapFrom(It.IsAny<Cocktail>())).Returns(cocktailDTO); using (var actContext = new IriOnCocktailServiceDbContext(options)) { await actContext.Bars.AddAsync(bar); await actContext.Cocktails.AddAsync(cocktail); await actContext.Cocktails.AddAsync(cocktail2); await actContext.SaveChangesAsync(); await actContext.CocktailBars.AddAsync(cocktailBar); await actContext.CocktailBars.AddAsync(cocktailBar2); await actContext.SaveChangesAsync(); } using (var assertContext = new IriOnCocktailServiceDbContext(options)) { var sut = new CocktailService(assertContext, ingredientServiceMock.Object, cocktailMapperMock.Object, cocktailMapperToDTOMock.Object, commentMapperToDTOMock.Object, commentMapperMock.Object, addCocktailMapperMock.Object, cocktailRatingToDTOMock.Object); var result = await sut.GetAllContainedCocktailsDTO("1"); Assert.AreEqual(2, result.Count); } }