Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public BeerDTO UpdateBeer(Guid id, BeerDTO beerDto)
- {
- if (!this.context.Breweries.Any(b => b.Name == beerDto.BreweryName))
- {
- throw new InvalidOperationException(); // no brewery with this id
- }
- if (!this.context.Styles.Any(s => s.Name == beerDto.StyleName))
- {
- throw new InvalidOperationException(); // no style with this id
- }
- if (!this.context.Countries.Any(c=>c.Name == beerDto.CountryName))
- {
- throw new InvalidOperationException(); // no country with this id
- }
- var beer = this.context.Beers.Where(b => b.IsDeleted == false)
- .FirstOrDefault(b => b.Id == beerDto.Id) ?? throw new ArgumentNullException();
- if (this.context.Beers.Any(b => b.Name == beerDto.Name && b.Id != beerDto.Id))
- {
- throw new InvalidOperationException(); // other beer with same name already exist
- }
- beer.Name = beerDto.Name;
- beer.Brand = beerDto.Brand;
- beer.CountryId = this.countryService.GetId(beerDto.CountryName);
- beer.BreweryId = this.breweryService.GetId(beerDto.BreweryName);
- beer.StyleId = this.styleService.GetId(beerDto.StyleName);
- beer.ABV = beerDto.ABV;
- beer.UpdatedOn = this.dateTimeProvider.GetDateTime();
- this.context.Beers.Update(beer);
- this.context.SaveChanges();
- return beerDto;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement