public IActionResult Create([FromBody] CreateBeerDto beer) { Brewery brewery = breweryRepository.GetById(beer.BreweryId); if (brewery == null) { return(BadRequest($"Brewery with id {beer.BreweryId} does not exists")); } Result <BeerName> beerNameResult = BeerName.Create(beer.Name); Result <BeerPrice> beerPriceResult = BeerPrice.Create(beer.Price); Result <BeerAlcoholDegree> beerAlcoholDegreeResult = BeerAlcoholDegree.Create(beer.AlcoholDegree); Result result = Result.Combine(beerNameResult, beerPriceResult, beerAlcoholDegreeResult); if (result.IsFailure) { return(BadRequest(beerNameResult.Error)); } Result <Brewery> breweryResult = brewery.AddBeer(beerNameResult.Value, beerPriceResult.Value, beerAlcoholDegreeResult.Value); if (breweryResult.IsFailure) { return(BadRequest(breweryResult.Error)); } return(Ok(breweryResult.Value)); }
public void Create_beer_price_with_too_precis_value_should_return_an_error() { Result <BeerPrice> beerPriceResult = BeerPrice.Create(2.555m); beerPriceResult.IsFailure.ShouldBeTrue(); beerPriceResult.Error.ShouldNotBeEmpty(); }
public void Create_beer_price_with_negative_value_should_return_an_error() { Result <BeerPrice> beerPriceResult = BeerPrice.Create(-5); beerPriceResult.IsFailure.ShouldBeTrue(); beerPriceResult.Error.ShouldNotBeEmpty(); }
public void Create_beer_with_null_beer_brewery_should_raise_an_exception() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); var beer = new Beer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value, null); }
public void Add_beer_without_error() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); Result <Brewery> breweryResult = brewery.AddBeer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value); breweryResult.IsFailure.ShouldBeFalse(); }
public void Create_beer_price_without_error() { Result <BeerPrice> beerPriceResult = BeerPrice.Create(2.55m); beerPriceResult.IsFailure.ShouldBeFalse(); BeerPrice beerPrice = beerPriceResult.Value; beerPrice.Value.ShouldBeEqualTo(2.55m); }
public Result <Brewery> AddBeer(BeerName beerName, BeerPrice beerPrice, BeerAlcoholDegree beerAlcoholDegree) { if (beers.Any(x => x.Name == beerName)) { return(Result.Failure <Brewery>($"Beer {beerName.Value} with same name already exists in this brewery")); } beers.Add(new Beer(beerName, beerPrice, beerAlcoholDegree, this)); return(Result.Ok(this)); }
public void Create_beer_without_error() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); var beer = new Beer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value, brewery); beer.ShouldNotBeNull(); }
public void Create_beer_provider_with_null_beer_provider_quantity_should_raise_an_exception() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); var beer = new Beer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value, brewery); var provider = new Provider(ProviderName.Create("GeneDrinks").Value); var beerProvider = new BeerProvider(null, beer, provider); }
public void Add_beer_that_already_exists_should_return_an_error() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); brewery.AddBeer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value); Result <Brewery> breweryResult = brewery.AddBeer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value); breweryResult.IsFailure.ShouldBeTrue(); }
public void Create_beer_provider_without_error() { var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); var beer = new Beer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value, brewery); var provider = new Provider(ProviderName.Create("GeneDrinks").Value); BeerProvider beerProvider = new BeerProvider(BeerProviderQuantity.Create(10).Value, beer, provider); beerProvider.ShouldNotBeNull(); }
public void Add_beer_without_error() { var provider = new Provider(ProviderName.Create("GeneDrinks").Value); var brewery = new Brewery(BreweryName.Create("Abbaye de Leffe").Value); var beer = new Beer(BeerName.Create("Leffe Blonde").Value, BeerPrice.Create(2.55m).Value, BeerAlcoholDegree.Create(5.5m).Value, brewery); Result <Provider> providerResult = provider.ProvideBeer(beer, BeerProviderQuantity.Create(10).Value); providerResult.IsFailure.ShouldBeFalse(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity <Beer>(x => { x.ToTable("Beer").HasKey(k => k.Id); x.Property(p => p.Id).HasColumnName("Id"); x.Property(p => p.Name) .HasConversion(p => p.Value, p => BeerName.Create(p).Value); x.Property(p => p.Price) .HasConversion(p => p.Value, p => BeerPrice.Create(p).Value); x.Property(p => p.AlcoholDegree) .HasConversion(p => p.Value, p => BeerAlcoholDegree.Create(p).Value); x.HasOne(p => p.Brewery).WithMany(p => p.Beers); }); modelBuilder.Entity <Provider>(x => { x.ToTable("Provider").HasKey(k => k.Id); x.Property(p => p.Id).HasColumnName("Id"); x.Property(p => p.Name) .HasConversion(p => p.Value, p => ProviderName.Create(p).Value); }); modelBuilder.Entity <BeerProvider>(x => { x.ToTable("BeerProvider").HasKey(k => k.Id); x.Property(p => p.Id).HasColumnName("Id"); x.Property(p => p.BeerProviderQuantity) .HasConversion(p => p.Value, p => BeerProviderQuantity.Create(p).Value).HasColumnName("AvailableQuantity"); x.HasOne(bp => bp.Beer).WithMany(b => b.BeerProviders); x.HasOne(bp => bp.Provider).WithMany(p => p.BeerProviders); }); modelBuilder.Entity <Brewery>(x => { x.ToTable("Brewery").HasKey(k => k.Id); x.Property(p => p.Id).HasColumnName("Id"); x.Property(p => p.Name) .HasConversion(p => p.Value, p => BreweryName.Create(p).Value); }); }