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))); }
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()); }