Exemplo n.º 1
0
        public async Task <ActionResult <Wholesaler> > AddBeer(AddBeerToWholesalerCommand command)
        {
            await _wholesalerService.AddBeer(command);

            // TODO : Check to return a 201
            return(Ok());
        }
Exemplo n.º 2
0
        public Task AddBeer(AddBeerToWholesalerCommand command)
        {
            var wholesaler = _beerContext.Wholesalers
                             .Include(w => w.WholesalerBeers)
                             .FirstOrDefault(w => w.Id == command.WholesalerId);

            if (wholesaler == null)
            {
                throw new CustomBadRequestException($"Wholesaler with id {command.WholesalerId} does not exist");
            }

            var beer = _beerContext.Beers.FirstOrDefault(b => b.Id == command.BeerId);

            if (beer == null)
            {
                throw new CustomBadRequestException($"Beer with id {command.BeerId} does not exist");
            }

            if (wholesaler.WholesalerBeers.Any(wb => wb.BeerId == command.BeerId))
            {
                throw new CustomBadRequestException("Wholesaler already sell this beer");
            }

            wholesaler.AddBeer(beer, command.Stock);
            _beerContext.Wholesalers.Update(wholesaler);
            return(_beerContext.SaveChangesAsync());
        }
Exemplo n.º 3
0
        public async Task AddBeerWholesaler_BeerNotExist()
        {
            var command = new AddBeerToWholesalerCommand {
                BeerId = 100, WholesalerId = 1, Stock = 20
            };

            using (var context = new BeerContext(ContextOptions))
            {
                var service = new WholesalerService(context);

                await Assert.ThrowsAsync <CustomBadRequestException>(() => service.AddBeer(command));
            }
        }
Exemplo n.º 4
0
        public async Task AddBeerWholesaler_ReturnOk()
        {
            var command = new AddBeerToWholesalerCommand {
                BeerId = 5, WholesalerId = 2, Stock = 20
            };

            using (var context = new BeerContext(ContextOptions))
            {
                var service = new WholesalerService(context);

                await service.AddBeer(command);

                var wholesaler = await context.Wholesalers
                                 .Include(w => w.WholesalerBeers)
                                 .SingleOrDefaultAsync(w => w.Id == command.WholesalerId);

                Assert.Contains(wholesaler.WholesalerBeers, wb => wb.BeerId == 5);
            }
        }