Пример #1
0
        public async Task <ActionResult <BeerDto> > PostWholesalerBeer(int id, PostAndPutWholesalerBeerForBeerDto postAndPutWholesalerBeerForBeerDto)
        {
            Beer beer = await _beerService.WholesalerBeersGetAsync(id);

            if (beer == null)
            {
                return(NotFound());
            }

            Wholesaler wholesaler = await _wholesalerService.GetAsync(postAndPutWholesalerBeerForBeerDto.WholesalerId);

            if (wholesaler == null)
            {
                return(NotFound());
            }

            WholesalerBeer wholesalerBeer = _mapper.Map <WholesalerBeer>(postAndPutWholesalerBeerForBeerDto);

            beer.WholesalerBeers.Add(wholesalerBeer); // TODO validate if it already exists

            _beerService.Update(beer);

            await _beerService.SaveAsync();

            return(CreatedAtAction(nameof(GetBeer), new { id = beer.Id }, _mapper.Map <BeerDto>(beer)));
        }
Пример #2
0
        public async Task <IActionResult> PutWholesalerBeer(int id, PostAndPutWholesalerBeerForBeerDto postAndPutWholesalerBeerForBeerDto)
        {
            Beer beer = await _beerService.WholesalerBeersGetAsync(id);

            if (beer == null)
            {
                return(NotFound());
            }

            Wholesaler wholesaler = await _wholesalerService.GetAsync(postAndPutWholesalerBeerForBeerDto.WholesalerId);

            if (wholesaler == null)
            {
                return(NotFound());
            }

            WholesalerBeer wholesalerBeer = beer.WholesalerBeers.AsEnumerable().FirstOrDefault(x => x.Wholesaler.Id == postAndPutWholesalerBeerForBeerDto.WholesalerId);

            if (wholesalerBeer == null)
            {
                return(NotFound());
            }

            wholesalerBeer.WholesalerId = postAndPutWholesalerBeerForBeerDto.WholesalerId; // TODO validate if it already exists
            wholesalerBeer.Stock        = postAndPutWholesalerBeerForBeerDto.Stock;

            _beerService.Update(beer);

            await _beerService.SaveAsync();

            return(NoContent());
        }
Пример #3
0
        public void UpdateStock(SellBeerOrUpdateStockCommand command)
        {
            var updateStock = new WholesalerBeer()
            {
                BeerId       = command.BeerId,
                WholesalerId = command.WholesalerId
            };

            _wholesalerRepository.UpdateStock(updateStock);
        }
Пример #4
0
        public void SellNewBeer(SellBeerOrUpdateStockCommand command)
        {
            var sellNewBeer = new WholesalerBeer()
            {
                BeerId       = command.BeerId,
                WholesalerId = command.WholesalerId,
                Stock        = command.Stock
            };

            _wholesalerRepository.SellNewBeer(sellNewBeer);
        }
        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 async Task <ActionResult <WholesalerBeerDto> > PostWholesalerBeer(PostWholesalerBeerDto postWholesalerBeerDto)
        {
            Wholesaler wholesaler = await _wholesalerService.GetAsync(postWholesalerBeerDto.WholesalerId);

            if (wholesaler == null)
            {
                return(NotFound());
            }

            Beer beer = await _beerService.GetAsync(postWholesalerBeerDto.BeerId);

            if (beer == null)
            {
                return(NotFound());
            }

            WholesalerBeer result = _mapper.Map <WholesalerBeer>(postWholesalerBeerDto);

            _wholesalerBeerService.Add(result);

            await _wholesalerBeerService.SaveAsync();

            return(CreatedAtAction(nameof(GetWholesalerBeer), new { wholesalerId = result.WholesalerId, beerId = result.BeerId }, _mapper.Map <WholesalerBeerDto>(result)));
        }
Пример #7
0
 public void UpdateStock(WholesalerBeer wholesalerBeer)
 {
     _brasserieContext.WholesalerBeers.Update(wholesalerBeer);
     _brasserieContext.SaveChanges();
 }
Пример #8
0
 public void SellNewBeer(WholesalerBeer wholesalerBeer)
 {
     _brasserieContext.WholesalerBeers.Add(wholesalerBeer);
     _brasserieContext.SaveChanges();
 }
Пример #9
0
 public void Update(WholesalerBeer entity)
 {
     _repo.Update(entity);
 }
Пример #10
0
 public void Add(WholesalerBeer entity)
 {
     _repo.Add(entity);
 }