public void AddBeer_DuplicateBeer_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 1, WholesalerId = 1, Stock = 10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <DuplicateItemException>().WithMessage(ExceptionMessage.ALREADY_SELL);
            }
        }
        public void AddBeer_StockIsNegative_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 1, WholesalerId = 2, Stock = -10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.NEGATIVE_STOCK);
            }
        }
        public void AddBeer_WholesalerNotExist_ThrowException()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 2, WholesalerId = 7, Stock = 10
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                Action action = () => wholesalerService.AddNewBeerToWholesaler(command);
                action.Should().ThrowExactly <NotFindObjectException>().WithMessage(ExceptionMessage.WHOLESALER_NOT_EXIST);
            }
        }
        public WholesalerBeer AddNewBeerToWholesaler(SellBeerCommand command)
        {
            if (command == null)
            {
                throw new HttpBodyException(ExceptionMessage.COMMAND_IS_NULL);
            }
            if (command.Stock < 0)
            {
                throw new HttpBodyException(ExceptionMessage.NEGATIVE_STOCK);
            }

            var beer = _brasserieContext.Beers
                       .FirstOrDefault(e => e.Id == command.BeerId);

            if (beer == null)
            {
                throw new NotFindObjectException(ExceptionMessage.BEER_NOT_EXIST);
            }

            var wholesaler = _brasserieContext.Wholesalers
                             .Include(e => e.WholesalerBeers)
                             .FirstOrDefault(w => w.Id == command.WholesalerId);

            if (wholesaler == null)
            {
                throw new NotFindObjectException(ExceptionMessage.WHOLESALER_NOT_EXIST);
            }

            if (wholesaler.WholesalerBeers.Any(wb => wb.WholesalerId == command.WholesalerId && wb.BeerId == command.BeerId))
            {
                throw new DuplicateItemException(ExceptionMessage.ALREADY_SELL);
            }

            var addBeer = new WholesalerBeer()
            {
                BeerId       = command.BeerId,
                WholesalerId = command.WholesalerId,
                Stock        = command.Stock
            };

            _brasserieContext.WholesalerBeers.Add(addBeer);
            _brasserieContext.SaveChanges();

            return(addBeer);
        }
        public void AddBeerToWholesaler_OK()
        {
            SellBeerCommand command = new SellBeerCommand()
            {
                BeerId = 5, WholesalerId = 2, Stock = 1000
            };

            using (var context = new BrasserieContext(ContextOptions))
            {
                var wholesalerService = new WholesalerService(context);

                var actual = wholesalerService.AddNewBeerToWholesaler(command);

                actual.BeerId.Should().Be(command.BeerId);
                actual.WholesalerId.Should().Be(command.WholesalerId);
                actual.Stock.Should().Be(command.Stock);
            }
        }
        public ActionResult <WholesalerBeer> SellNewBeer(SellBeerCommand command)
        {
            _wholesalerService.AddNewBeerToWholesaler(command);

            return(Ok());
        }