public Beer CreateBeer(CreateBeerCommand command) { if (command == null) { throw new HttpBodyException(ExceptionMessage.COMMAND_IS_NULL); } if (command.Name == null) { throw new HttpBodyException(ExceptionMessage.NAME_BEER_NOT_EXIST); } if (command.Price <= 0) { throw new HttpBodyException(ExceptionMessage.PRICE_NULL_OR_NEGATIVE); } var brewer = _brasserieContext.Brewers.SingleOrDefault(b => b.Id == command.BrewerId); if (brewer == null) { throw new NotFindObjectException(ExceptionMessage.BREWER_NOT_EXIST); } var beer = new Beer { Name = command.Name, AlcoholPercentage = command.AlcoholPercentage, Price = command.Price, Brewer = brewer }; _brasserieContext.Beers.Add(beer); _brasserieContext.SaveChanges(); return(beer); }
public async Task GivenValidRequest_WithInvalidBeerStyle_ReturnsMappedAndCreatedBeerWithOtherAsStyle() { // Arrange using var unitOfWork = UnitOfWork; var beerCommand = new CreateBeerCommand(new CreateBeerDto { Name = "Test Beer", Style = "Not defined!", BreweryId = 1 }); var handler = new CreateBeerCommandHandler(unitOfWork); // Act var result = await handler.Handle(beerCommand, CancellationTestToken); // Assert result.ShouldNotBeNull(); result.ShouldBeOfType <BeerResource>(); result.Self.ShouldNotBeNull(); result.Self.Brewery.ShouldNotBeNull(); result.Self.Brewery?.Address.ShouldNotBeNull(); result.Self.Brewery?.Address?.StreetAddress.ShouldBe("1030 E Cypress Ave Ste D"); result.Self.Brewery?.Address?.City.ShouldBe("Redding"); result.Self.Brewery?.Address?.State.ShouldBe("CA"); result.Self.Brewery?.Address?.ZipCode.ShouldBe("96002"); result.Self.Brewery?.Beers.ShouldBeNull(); result.Self.Brewery?.Id.ShouldBe(1); result.Self.Brewery?.Name.ShouldBe("Fall River Brewery"); result.Self.Id.ShouldNotBeNull(); result.Self.Name.ShouldBe(beerCommand.Dto.Name); result.Self.Style.ShouldBe(BeerStyle.Other.ToString()); }
private async Task InvokeCreateBeerCommandAsync(IContext context, CreateBeerCommand command) { var triggeredEvent = new CreateBeerEvent(command.Beer); bool isSuccess = await actorManager.RequestAsync <BeerEventActor, bool>(triggeredEvent); context.Respond(isSuccess); }
public ActionResult <Beer> CreateBeer([FromRoute] int id, [FromBody] CreateBeerCommand command) { command.BrewerId = id; var createdBeer = _beerService.CreateBeer(command); return(Ok(createdBeer)); }
public async Task <ActionResult> CreateBeer(Beer beer) { try { var command = new CreateBeerCommand(beer); bool isSuccess = await _actorManager.RequestAsync <BeerCommandActor, bool>(command); return(StatusCode(201)); } catch (Exception ex) { Console.WriteLine(ex.StackTrace); return(StatusCode(500)); } }
public void AddBeer_CommandAmountIsNegative_ThrowException() { using (var context = new BrasserieContext(ContextOptions)) { var service = new BeerService(context); CreateBeerCommand createBeer = new CreateBeerCommand() { Name = "Jaja", Price = -10.00, AlcoholPercentage = 17.00, BrewerId = 1 }; Action action = () => service.CreateBeer(createBeer); action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.PRICE_NULL_OR_NEGATIVE); } }
public void AddBeer_CommandNameIsNull_ThrowException() { using (var context = new BrasserieContext(ContextOptions)) { var service = new BeerService(context); CreateBeerCommand createBeer = new CreateBeerCommand() { Price = 10, AlcoholPercentage = 10, BrewerId = 1 }; Action action = () => service.CreateBeer(createBeer); action.Should().ThrowExactly <HttpBodyException>().WithMessage(ExceptionMessage.NAME_BEER_NOT_EXIST); } }
public void AddBeer_BrewerNotExist_ThrowException() { using (var context = new BrasserieContext(ContextOptions)) { var service = new BeerService(context); CreateBeerCommand createBeer = new CreateBeerCommand() { Name = "Xavier", Price = 10.00, AlcoholPercentage = 1.00, BrewerId = 40 }; Action action = () => service.CreateBeer(createBeer); action.Should().ThrowExactly <NotFindObjectException>().WithMessage(ExceptionMessage.BREWER_NOT_EXIST); } }
public Beer CreateBeer(CreateBeerCommand command) { var brewer = new Brewer { Id = command.BrewerId }; var beer = new Beer { Name = command.Name, Description = command.Description, AlcoolPercentage = command.AlcoolPercentage, }; _beerRepository.CreateBeer(brewer, beer); _unitOfWork.SaveChanges(); return(beer); }
public async Task GivenValidRequest_WhenBreweryDoesNotExist_ThrowsApiExceptionForBadRequest() { // Arrange using var unitOfWork = UnitOfWork; var beerCommand = new CreateBeerCommand(new CreateBeerDto { Name = "Test Beer", Style = "Lager", BreweryId = 11 }); var handler = new CreateBeerCommandHandler(unitOfWork); // Act var result = await Should.ThrowAsync <DapperyApiException>(async() => await handler.Handle(beerCommand, CancellationTestToken)); // Assert result.ShouldNotBeNull(); result.StatusCode.ShouldBe(HttpStatusCode.BadRequest); }
public void AddBeer_Ok() { using (var context = new BrasserieContext(ContextOptions)) { var service = new BeerService(context); CreateBeerCommand createBeer = new CreateBeerCommand() { Name = "Xavier", Price = 10.00, AlcoholPercentage = 1.00, BrewerId = 1 }; var beer = service.CreateBeer(createBeer); beer.Name.Should().Be("Xavier"); beer.Price.Should().Be(10); beer.Brewer.Name.Should().Be("Abbaye de Leffe"); } }
public Beer CreateBeer(CreateBeerCommand command) { var brewer = _brewerRepository.FindBrewerById(command.BrewerId); if (brewer == null) { return(null); } var beer = new Beer { Name = command.Name, AlcoholPercentage = command.AlcoholPercentage, Price = command.Price, Brewer = brewer }; _beerRepository.CreateBeer(beer); return(beer); }
public ActionResult <Beer> CreateBeer(CreateBeerCommand command) { var beer = _beerService.CreateBeer(command); return(Ok(beer)); }