Advertisement
nassss

Untitled

Apr 22nd, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. public BeerDTO UpdateBeer(Guid id, BeerDTO beerDto)
  2. {
  3.  
  4. if (!this.context.Breweries.Any(b => b.Name == beerDto.BreweryName))
  5. {
  6. throw new InvalidOperationException(); // no brewery with this id
  7. }
  8. if (!this.context.Styles.Any(s => s.Name == beerDto.StyleName))
  9. {
  10. throw new InvalidOperationException(); // no style with this id
  11. }
  12. if (!this.context.Countries.Any(c=>c.Name == beerDto.CountryName))
  13. {
  14. throw new InvalidOperationException(); // no country with this id
  15. }
  16.  
  17. var beer = this.context.Beers.Where(b => b.IsDeleted == false)
  18. .FirstOrDefault(b => b.Id == beerDto.Id) ?? throw new ArgumentNullException();
  19.  
  20. if (this.context.Beers.Any(b => b.Name == beerDto.Name && b.Id != beerDto.Id))
  21. {
  22. throw new InvalidOperationException(); // other beer with same name already exist
  23. }
  24.  
  25. beer.Name = beerDto.Name;
  26. beer.Brand = beerDto.Brand;
  27. beer.CountryId = this.countryService.GetId(beerDto.CountryName);
  28. beer.BreweryId = this.breweryService.GetId(beerDto.BreweryName);
  29. beer.StyleId = this.styleService.GetId(beerDto.StyleName);
  30. beer.ABV = beerDto.ABV;
  31.  
  32. beer.UpdatedOn = this.dateTimeProvider.GetDateTime();
  33.  
  34. this.context.Beers.Update(beer);
  35. this.context.SaveChanges();
  36.  
  37. return beerDto;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement