public Beer updateBeer(BeerDTO newbeer) { var beerInDataBase = getBeerById(newbeer.id); if (beerInDataBase == null) { return(null); } beerInDataBase.Name = newbeer.name; beerInDataBase.Tagline = newbeer.tagline; beerInDataBase.FirstBrewed = newbeer.first_brewed; beerInDataBase.Description = newbeer.description; beerInDataBase.Abv = newbeer.abv; beerInDataBase.Ibu = newbeer.ibu; beerInDataBase.TargetFg = newbeer.target_fg; beerInDataBase.TargetOg = newbeer.target_og; beerInDataBase.Ebc = newbeer.ebc; beerInDataBase.Srm = newbeer.srm; beerInDataBase.Ph = newbeer.ph; beerInDataBase.AttenuationLevel = newbeer.attenuation_level; beerInDataBase.Food = null; //Sorry sé que después debo encontrar como hacerlo beerInDataBase.BrewersTips = newbeer.brewers_tips; beerInDataBase.ContributedBy = newbeer.contributed_by; dataBase.SaveChanges(); return(beerInDataBase); }
public static Beer MapDTOToBeer(this BeerDTO dto) { try { var beer = new Beer() { ID = dto.ID, Name = dto.Name, Rating = dto.Rating, ABV = dto.ABV, IsDeleted = dto.IsDeleted, CountryID = dto.CountryID, //Country = new Country() { Name = dto.Country.Name},//dto.Country.MapDTOToCountry(), StyleID = dto.StyleID, //Style = new BeerStyle() {Name = dto.Style.Name },//dto.Style.MapDTOToStyle(), BreweryID = dto.BreweryID, //Brewery = new Brewery() { Name = dto.Brewery.Name, Country = new Country() { Name = dto.Brewery.Country} },//dto.Brewery.MapDTOToBrewery() }; if (dto.Reviews != null) { beer.Reviews = dto.Reviews.Select(r => r.MapDTOToReview()).ToList(); } else { beer.Reviews = null; } return(beer); } catch (Exception) { return(new Beer()); } }
public void MapDTOToBeer_ShouldReturnCorrectID() { //Arrange var cut = new BeerDTO { ID = 1, ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new CountryDTO() { Name = "Bulgaria" }, Brewery = new BreweryDTO() { Name = "Brewery", Country = "Germany" }, Style = new BeerStyleDTO() { Name = "Ale" } }; //Act var sut = cut.MapDTOToBeer(); //Assert Assert.AreEqual(sut.ID, 1); }
public void MapDTOToBeer_ShouldReturnCorrectNameAndCountryAndBreweryAndStyle() { //Arrange var cut = new BeerDTO { ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new CountryDTO() { Name = "Bulgaria" }, Brewery = new BreweryDTO() { Name = "Brewery", Country = "Germany" }, Style = new BeerStyleDTO() { Name = "Ale" } }; //Act var sut = cut.MapDTOToBeer(); //Assert Assert.AreEqual(sut.Name, "Carlsberg"); Assert.AreEqual(sut.Country.Name, "Bulgaria"); Assert.AreEqual(sut.Brewery.Name, "Brewery"); Assert.AreEqual(sut.Brewery.Country.Name, "Germany"); Assert.AreEqual(sut.Style.Name, "Ale"); Assert.AreEqual(sut.ABV, 4.5f); Assert.AreEqual(sut.Rating, 2); }
public void MapDTOToBeer_ShouldReturnBrewery() { //Arrange var cut = new BeerDTO { ID = 1, ABV = 4.5f, Rating = 2, Name = "Carlsberg", Country = new CountryDTO() { Name = "Bulgaria" }, Brewery = new BreweryDTO() { Name = "Brewery", Country = "Germany" }, Style = new BeerStyleDTO() { Name = "Ale" } }; //Act var sut = cut.MapDTOToBeer(); //Assert Assert.IsInstanceOfType(sut, typeof(Beer)); }
public async Task ThrowNullExc_If_BeerAlreadyExists() { var options = TestUtils.GetOptions(nameof(ThrowNullExc_If_BeerAlreadyExists)); var mockDateTime = new Mock <IDateTimeProvider>(); var beer = new Beer { Name = "Ariana" }; var beerDTO = new BeerDTO { Name = "Ariana" }; using (var arrangeContext = new BeeroverflowContext(options)) { await arrangeContext.Beers.AddAsync(beer); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new BeeroverflowContext(options)) { var sut = new BeerService(assertContext, mockDateTime.Object); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateBeerAsync(beerDTO)); } }
public BeerDTO GetBeer(int id) { var beer = _context.Beers .Include(b => b.Type) .Include(b => b.Brewery) .ThenInclude(br => br.Country) .FirstOrDefault(b => b.Id == id); if (beer == null) { throw new ArgumentNullException(); } var beerDTO = new BeerDTO { Id = beer.Id, Name = beer.Name, BeerType = beer.Type.Name, Brewery = beer.Brewery.Name, BreweryCountry = beer.Brewery.Country.Name, AlcoholByVolume = beer.AlcoholByVolume, }; return(beerDTO); }
public Beer addBeer(BeerDTO newbeer) { var newBeerInDB = new Beer(); newBeerInDB.Name = newbeer.name; newBeerInDB.Tagline = newbeer.tagline; newBeerInDB.FirstBrewed = newbeer.first_brewed; newBeerInDB.Description = newbeer.description; newBeerInDB.Abv = newbeer.abv; newBeerInDB.Ibu = newbeer.ibu; newBeerInDB.TargetFg = newbeer.target_fg; newBeerInDB.TargetOg = newbeer.target_og; newBeerInDB.Ebc = newbeer.ebc; newBeerInDB.Srm = newbeer.srm; newBeerInDB.Ph = newbeer.ph; newBeerInDB.AttenuationLevel = newbeer.attenuation_level; newBeerInDB.Food = null; //Sorry sé que después debo encontrar como hacerlo newBeerInDB.BrewersTips = newbeer.brewers_tips; newBeerInDB.ContributedBy = newbeer.contributed_by; dataBase.Beer.Add(newBeerInDB); dataBase.SaveChanges(); return(newBeerInDB); }
/// <summary> /// Updates the Beer's Name, Style, Brewery and Country /// </summary> /// <param name="id">ID of the Beer to be updated.</param> /// <param name="model">Input object with update information.</param> /// <returns>Returns the reevaluated input object</returns> public async Task <BeerDTO> UpdateAsync(int?id, BeerDTO model) { var beer = await this._context.Beers .Include(b => b.Country) .Include(b => b.Style) .Include(b => b.Brewery) .FirstOrDefaultAsync(br => br.ID == id); if (beer == null) { return(null); } beer.ABV = model.ABV; beer.Name = model.Name; beer.Country = _context.Countries.FirstOrDefault(c => c.Name == model.Country.Name); beer.Style = _context.BeerStyles.FirstOrDefault(s => s.Name == model.Style.Name); beer.Brewery = _context.Breweries.FirstOrDefault(b => b.Name == model.Brewery.Name); beer.ModifiedOn = DateTime.UtcNow; model.ID = beer.ID; _context.Beers.Update(beer); try { await this._context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BeerExists(id)) { return(null); } } return(model); }
public void CreateBeer_Success() { //Arrange var beerDTO = new BeerDTO { BeerName = "Boliarka", BeerTypeId = 1, BreweryId = 1, AlcByVol = (double)3.2, Description = "Stava", }; var options = Utils.GetOptions(nameof(CreateBeer_Success)); //Act & Assert using (var assertContext = new BeerOverflowContext(options)) { var sut = new BeerService(assertContext); var result = sut.CreateBeer(beerDTO); Assert.AreEqual(beerDTO.BeerName, result.BeerName); Assert.AreEqual(beerDTO.BeerTypeId, result.BeerTypeId); Assert.AreEqual(beerDTO.BreweryId, result.BreweryId); Assert.AreEqual(beerDTO.AlcByVol, result.AlcByVol); Assert.AreEqual(beerDTO.Description, result.Description); } }
public void Save(BeerDTO beerDTO) { //_context.Database.BeginTransaction(); if (beerDTO.Id.HasValue) { var beerModel = _beerRepository.GetById(beerDTO.Id.Value); if (beerModel == null) { //_context.Database.RollbackTransaction(); throw new NotFoundException("L'identifiant de la bière n'est pas valide!"); } beerModel.Name = beerDTO.Name; beerModel.Type = beerDTO.Type; _beerRepository.Edit(beerModel); } else { var beerModel = new Beer() { Id = null, CreationDate = DateTime.Now, Name = beerDTO.Name, Type = beerDTO.Type }; _beerRepository.Create(beerModel); } //_context.Database.CommitTransaction(); }
public async Task <IActionResult> Put(int id, [FromBody] BeerDTO beer) { if (beer == null) { return(BadRequest()); } var theNewBeer = await this._service.UpdateAsync(id, beer); return(Ok(theNewBeer)); }
public async Task <IActionResult> Post([FromBody] BeerDTO beer) { if (beer == null) { return(BadRequest()); } var theNewBeer = await this._service.CreateAsync(beer); return(Created("Post", theNewBeer)); }
public async Task SaveRating([FromBody] RatingDTO dto) { /// /// Persist RatingDTO /// To Query the existing data using the beerid /// If Found update, if not Insert /// var row = data.Where(x => x.id == dto.beerid).FirstOrDefault(); if (row == null) { BeerWithRatingsDTO temp = new BeerWithRatingsDTO(); temp.comments.Add(new RatingShortDTO() { username = dto.username, userrating = dto.userrating, comment = dto.comment }); /////////////////////// //Need to populate temp's Beer Information using Punk API!!!! /////////////////////// BeerDTO beer = await new PunkRepository().GetBeersById(dto.beerid); temp.name = beer.name; temp.description = beer.description; data.Add(temp); } else ////row!=null { var comment = row.comments.Where(x => x.username == dto.username).FirstOrDefault(); if (comment == null) { row.comments.Add(new RatingShortDTO() { username = dto.username, userrating = dto.userrating, comment = dto.comment }); } else { comment.username = dto.username; comment.userrating = dto.userrating; comment.comment = dto.comment; } } //persist the data back into File string newString = JsonConvert.SerializeObject(data, Formatting.Indented); File.WriteAllText("data.json", newString); /////////////////////// ///Need to ask BeerWithRatingsControler to reload! /////////////////////// BeerWithRatingsController.ReloadDataFromFile(); }
public ActionResult Update(int id, [FromBody] BeerDTO beer) { if (!ModelState.IsValid) { return(BadRequest("Invalid Model")); } _beerService.Save(beer); return(Ok()); }
public void UpdateBeer_BeerName_Success() { //Arange, Act & Assert using (var assertContext = Utils.GetContextWithData(nameof(UpdateBeer_BeerName_Success))) { var id = 1; var sut = new BeerService(assertContext); var oldName = sut.GetBeer(id).BeerName; var nameToUpdate = "Gayda"; BeerDTO result = sut.UpdateBeer(id, nameToUpdate, null, null, 1, 1); Assert.AreNotEqual(oldName, nameToUpdate); } }
public async Task <BeerDTO> CreateBeerAsync(BeerDTO beerDTO) { var check = await this.context.Beers .FirstOrDefaultAsync(x => x.Name == beerDTO.Name); if (check != null) { throw new ArgumentNullException("Beer already exist."); } var brewery = await this.context.Breweries .Include(x => x.Country) .Where(x => x.Name == beerDTO.Brewery) .FirstOrDefaultAsync(); if (brewery == null) { throw new ArgumentNullException("Brewery does not exist."); } var country = brewery.Country; var style = await this.context.Styles .Where(x => x.Name == beerDTO.Style) .FirstOrDefaultAsync(); if (style == null) { throw new ArgumentNullException("Style does not exist."); } var beer = new Beer { Id = beerDTO.Id, Name = beerDTO.Name, Abv = beerDTO.Abv, BreweryId = brewery.Id, CountryId = country.Id, StyleId = style.Id, CreatedOn = this.dateTimeProvider.GetDateTime(), }; await this.context.Beers.AddAsync(beer); await this.context.SaveChangesAsync(); beerDTO.Id = beer.Id; return(beerDTO); }
public async Task <IActionResult> Create([FromBody] BeerViewModel model) { try { var beerDto = new BeerDTO(model.BeerName, model.BeerTypeId, model.BreweryId, (double)model.AlcByVol, model.Description); var beer = await this.beerService.CreateBeerAsync(beerDto); return(Created("Post", beer)); } catch (Exception) { throw; } }
public BeerDTO CreateBeer(BeerDTO beerDTO) { try { var beer = new Beer(beerDTO.BeerName, beerDTO.BeerTypeId, beerDTO.BreweryId, beerDTO.AlcByVol, beerDTO.Description); _beerOverflowContext.Beers.Add(beer); _beerOverflowContext.SaveChanges(); return(beerDTO); } catch (Exception) { throw; } }
public async Task CreateBeer_When_ParamsAreValid() { var options = TestUtils.GetOptions(nameof(CreateBeer_When_ParamsAreValid)); var mockDateTime = new Mock <IDateTimeProvider>(); var country = new Country { Name = "Romania" }; var brewery = new Brewery { Name = "Ariana", CountryId = 1 }; var style = new Style { Name = "Pale" }; var beerDTO = new BeerDTO { Name = "Birichka", Country = "Romania", Brewery = "Ariana", Style = "Pale" }; using (var arrangeContext = new BeeroverflowContext(options)) { await arrangeContext.Countries.AddAsync(country); await arrangeContext.Breweries.AddAsync(brewery); await arrangeContext.Styles.AddAsync(style); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new BeeroverflowContext(options)) { var sut = new BeerService(assertContext, mockDateTime.Object); var result = await sut.CreateBeerAsync(beerDTO); var comparison = assertContext.Beers.Where(x => x.Id == 1).FirstOrDefault(); Assert.AreEqual("Birichka", comparison.Name); } }
public async Task <IActionResult> Create([Bind("BeerName,AlcByVol,Description,BeerTypeId,BreweryId")] BeerViewModel model) { if (ModelState.IsValid) { var beerDto = new BeerDTO(model.BeerName, model.BeerTypeId, model.BreweryId, (double)model.AlcByVol, model.Description); var beer = beerService.CreateBeer(beerDto); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["BeerTypeId"] = new SelectList(_context.BeerTypes, "Id", "Type"); ViewData["BreweryId"] = new SelectList(_context.Breweries, "Id", "Name"); ViewData["CountryId"] = new SelectList(_context.Countries, "Id", "Name"); return(View(model)); }
public async Task <BeerDTO> CreateBeerAsync(BeerDTO beerDTO) { try { var beer = new Beer(beerDTO.BeerName, beerDTO.BeerTypeId, beerDTO.BreweryId, beerDTO.AlcByVol, beerDTO.Description); await _beerOverflowContext.Beers.AddAsync(beer); await _beerOverflowContext.SaveChangesAsync(); return(beerDTO); } catch (Exception) { throw; } }
private bool MatchesSearch(BeerDTO beer, SearchBeerDTO searchData) { if (!String.IsNullOrWhiteSpace(searchData.Name)) { if (!beer.Name.ToLower().Contains(searchData.Name.ToLower())) { return(false); } } return(((searchData.Style != null && beer.Style != searchData.Style) || beer.Alcohol < searchData.AlcoholMin || beer.Alcohol > searchData.AlcoholMax || beer.Ibu < searchData.IbuMin || beer.Ibu > searchData.IbuMax || beer.Blg < searchData.BlgMin || beer.Blg > searchData.BlgMax) ? false : true); }
public async Task ThrowNullExcFroBeer_If_StyleDoesNotExist() { var options = TestUtils.GetOptions(nameof(ThrowNullExcFroBeer_If_StyleDoesNotExist)); var mockDateTime = new Mock <IDateTimeProvider>(); var country = new Country { Name = "Romania" }; var brewery = new Brewery { Name = "Ariana", CountryId = 1 }; var style = new Style { Name = "Pale" }; var beerDTO = new BeerDTO { Name = "Birichka", Country = "Romania", Brewery = "Ariana", Style = "Dark Lager" }; using (var arrangeContext = new BeeroverflowContext(options)) { await arrangeContext.Countries.AddAsync(country); await arrangeContext.Breweries.AddAsync(brewery); await arrangeContext.Styles.AddAsync(style); await arrangeContext.SaveChangesAsync(); } using (var assertContext = new BeeroverflowContext(options)) { var sut = new BeerService(assertContext, mockDateTime.Object); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateBeerAsync(beerDTO)); } }
public static BeerViewModel MapBeerDTOToView(this BeerDTO dto) { var beerView = new BeerViewModel() { ID = dto.ID, Name = dto.Name, Style = dto.Style.Name, Country = dto.Country.Name, Brewery = dto.Brewery.Name, Rating = dto.Rating, ABV = dto.ABV, IsDeleted = dto.IsDeleted, }; if (dto.Reviews != null) { beerView.Reviews = dto.Reviews.Select(r => r.MapDTOToReviewView()).ToList(); } return(beerView); }
public async Task <IActionResult> Rate(int id, [FromBody] BeerDTO beerDTO) { var beerID = beerDTO.ID; if (id <= 0 || beerID == default) { return(BadRequest()); } try { var model = await this._service.Rate(id, beerDTO.ID, (int)beerDTO.Rating); return(Ok(model)); } catch (Exception e) { return(NotFound(e.Message)); } }
public async Task <IActionResult> Post([FromBody] BeerViewModel model) { if (model == null) { return(BadRequest()); } var beerDTO = new BeerDTO { Id = model.Id, Name = model.Name, Abv = model.Abv, Country = model.Country, Brewery = model.Brewery, Style = model.Style }; var newBeer = await this.beerService.CreateBeerAsync(beerDTO); return(Created("Post", newBeer)); }
public IActionResult Post([FromBody] BeerViewModel beerViewModel) { if (beerViewModel == null) { return(BadRequest()); } if (BeerExists(beerViewModel.Name)) { return(ValidationProblem($"Beer with name {beerViewModel.Name} already exists.")); } var beerDTO = new BeerDTO { Name = beerViewModel.Name, BeerType = beerViewModel.BeerType, Brewery = beerViewModel.Brewery, AlcoholByVolume = beerViewModel.AlcoholByVolume }; var beer = _beerService.CreateBeer(beerDTO); return(Created("Post", beerViewModel)); }
public static BeerViewModel GetViewModel(this BeerDTO dto) { if (dto == null) { throw new ArgumentException(); } ; return(new BeerViewModel { Id = dto.Id, Name = dto.Name, Abv = dto.Abv, CountryId = dto.CountryId, Country = dto.Country, BreweryId = dto.BreweryId, Brewery = dto.Brewery, StyleId = dto.StyleId, Style = dto.Style, Rating = Math.Round(dto.Rating, 2), Reviews = dto.Reviews.GetViewModels() }); }
public static BeerDTO MapBeerToDTO(this Beer beer) { try { var beerDTO = new BeerDTO { ID = beer.ID, Name = beer.Name, Rating = beer.Rating, ABV = beer.ABV, IsDeleted = beer.IsDeleted, Country = new CountryDTO() { Name = beer.Country.Name }, //beer.Country.MapCountryToDTO(), Style = new BeerStyleDTO() { Name = beer.Style.Name }, //beer.Style.MapStyleToDTO(), Brewery = new BreweryDTO() { Name = beer.Brewery.Name, Country = beer.Brewery.Country.Name } //beer.Brewery.MapBreweryToDTO(), }; if (beer.Reviews != null) { beerDTO.Reviews = beer.Reviews.Select(r => r.MapReviewToDTO()).ToList(); } else { beerDTO.Reviews = null; } return(beerDTO); } catch (Exception) { return(new BeerDTO()); } }