public IActionResult UpdateBeerQuantity(long id, [FromBody] UpdateBeerQuantityDto updateBeerQuantity)
        {
            Provider provider = providerRepository.GetById(id);

            if (provider == null)
            {
                return(BadRequest($"Provider with id {id} does not exists"));
            }

            Beer beer = beerRepository.GetById(updateBeerQuantity.BeerId);

            if (beer == null)
            {
                return(BadRequest($"Beer with id {updateBeerQuantity.BeerId} does not exists"));
            }

            Result <BeerProviderQuantity> beerProviderQuantityResult =
                BeerProviderQuantity.Create(updateBeerQuantity.AvailableQuantity);

            if (beerProviderQuantityResult.IsFailure)
            {
                return(BadRequest(beerProviderQuantityResult.Error));
            }

            Result <BeerProvider> beerProvider = provider.UpdateBeerQuantity(beer, beerProviderQuantityResult.Value);

            if (beerProvider.IsFailure)
            {
                return(BadRequest(beerProvider.Error));
            }

            beerProviderRepository.Save(beerProvider.Value);

            return(Ok(provider));
        }
Example #2
0
        public void Update_beer_quantity_without_error()
        {
            Mock <Beer> beerMock = new Mock <Beer>();

            beerMock.SetupGet(x => x.Id).Returns(1);
            beerMock.SetupGet(x => x.Name).Returns(BeerName.Create("Leffe blonde").Value);

            Mock <BeerProvider> beerProviderMock = new Mock <BeerProvider>();

            beerProviderMock.SetupGet(x => x.Beer).Returns(beerMock.Object);
            beerProviderMock.SetupGet(x => x.BeerProviderQuantity).Returns(BeerProviderQuantity.Create(10).Value);

            Mock <Provider> providerMock = new Mock <Provider>();

            providerMock.SetupGet(x => x.BeerProviders).Returns(new List <BeerProvider>()
            {
                beerProviderMock.Object
            });

            Result <BeerProvider> beerProviderResult =
                providerMock.Object.UpdateBeerQuantity(beerMock.Object, BeerProviderQuantity.Create(20).Value);

            beerProviderResult.IsFailure.ShouldBeFalse();

            beerProviderResult.Value.BeerProviderQuantity.Value.ShouldBeEqualTo(20);
        }
        public IActionResult AddBeerToProvider([FromBody] AddBeerToProviderDto addBeerToProvider)
        {
            Provider provider = providerRepository.GetById(addBeerToProvider.ProviderId);

            if (provider == null)
            {
                return(BadRequest($"Provider with id {addBeerToProvider.ProviderId} does not exists"));
            }

            Beer beer = beerRepository.GetById(addBeerToProvider.BeerId);

            if (beer == null)
            {
                return(BadRequest($"Beer with id {addBeerToProvider.BeerId} does not exists"));
            }

            Result <BeerProviderQuantity> beerProviderQuantityResult =
                BeerProviderQuantity.Create(addBeerToProvider.AvailableQuantity);

            if (beerProviderQuantityResult.IsFailure)
            {
                return(BadRequest(beerProviderQuantityResult.Error));
            }

            Result <Provider> result = provider.ProvideBeer(beer, beerProviderQuantityResult.Value);

            if (result.IsFailure)
            {
                return(BadRequest(result.Error));
            }

            return(Ok(result.Value));
        }
        public void Create_beer_provider_quantity_with_negative_value_should_return_an_error()
        {
            Result <BeerProviderQuantity> beerProviderQuantityResult = BeerProviderQuantity.Create(-5);

            beerProviderQuantityResult.IsFailure.ShouldBeTrue();

            beerProviderQuantityResult.Error.ShouldNotBeEmpty();
        }
        public void Create_beer_provider_quantity_without_error()
        {
            Result <BeerProviderQuantity> beerProviderQuantityResult = BeerProviderQuantity.Create(10);

            beerProviderQuantityResult.IsFailure.ShouldBeFalse();

            BeerProviderQuantity beerProviderQuantity = beerProviderQuantityResult.Value;

            beerProviderQuantity.Value.ShouldBeEqualTo(10);
        }
Example #6
0
        public void Create_beer_provider_with_null_provider_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 beerProvider = new BeerProvider(BeerProviderQuantity.Create(10).Value, beer, null);
        }
Example #7
0
        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();
        }
Example #8
0
        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);
     });
 }
Example #10
0
        public void Update_beer_quantity_with_beer_that_does_not_exists_for_this_provider_should_return_an_error()
        {
            Mock <Beer> beerMock = new Mock <Beer>();

            beerMock.SetupGet(x => x.Id).Returns(1);
            beerMock.SetupGet(x => x.Name).Returns(BeerName.Create("Leffe blonde").Value);

            Mock <BeerProvider> beerProviderMock = new Mock <BeerProvider>();

            beerProviderMock.SetupGet(x => x.Beer).Returns(beerMock.Object);
            beerProviderMock.SetupGet(x => x.BeerProviderQuantity).Returns(BeerProviderQuantity.Create(10).Value);

            Mock <Provider> providerMock = new Mock <Provider>();

            providerMock.SetupGet(x => x.BeerProviders).Returns(new List <BeerProvider>());

            Result <BeerProvider> beerProviderResult =
                providerMock.Object.UpdateBeerQuantity(beerMock.Object, BeerProviderQuantity.Create(20).Value);

            beerProviderResult.IsFailure.ShouldBeTrue();

            beerProviderResult.Error.ShouldNotBeEmpty();
        }
Example #11
0
 public void Create_beer_provider_with_null_beer_should_raise_an_exception()
 {
     var provider     = new Provider(ProviderName.Create("GeneDrinks").Value);
     var beerProvider = new BeerProvider(BeerProviderQuantity.Create(10).Value, null, provider);
 }
Example #12
0
        public void Add_beer_that_already_exists_should_return_an_error()
        {
            Mock <Beer> beerMock = new Mock <Beer>();

            beerMock.SetupGet(x => x.Id).Returns(1);
            beerMock.SetupGet(x => x.Name).Returns(BeerName.Create("Leffe blonde").Value);

            Mock <BeerProvider> beerProviderMock = new Mock <BeerProvider>();

            beerProviderMock.SetupGet(x => x.Beer).Returns(beerMock.Object);

            Mock <Provider> providerMock = new Mock <Provider>();

            providerMock.SetupGet(x => x.BeerProviders).Returns(new List <BeerProvider>()
            {
                beerProviderMock.Object
            });

            Result <Provider> providerResult = providerMock.Object.ProvideBeer(beerMock.Object, BeerProviderQuantity.Create(10).Value);

            providerResult.IsFailure.ShouldBeTrue();
        }