public async Task <BasketEvent> AddItemToBasketAction(AddItemToBasket message) { var productActorResult = await this.ProductsActorRef.Ask <ProductsActor.ProductEvent>( new ProductsActor.UpdateStock( productId: message.ProductId, amountChanged: -message.Amount ) ); if (productActorResult is ProductsActor.StockUpdated) { var product = ((ProductsActor.StockUpdated)productActorResult).Product; return(AddToBasket(product, message.Amount) as ItemAdded); } else if (productActorResult is ProductsActor.ProductNotFound) { return(new ProductNotFound()); } else if (productActorResult is ProductsActor.InsuffientStock) { return(new NotInStock()); } else { throw new NotImplementedException($"Unknown response: {productActorResult.GetType().ToString()}"); } }
public void Handle(AddItemToBasket command) { var basket = this.basketRepository.GetById(command.Id); var product = this.productRepository.GetById(command.ProductId); basket.AddItem(product, command.Quantity); }
public async Task <IActionResult> AddItemToBasket([FromBody] AddItemToBasket command) { command.UserId = UserId; var result = await _mediator.Send(command); return(JsonResult(result)); }
public IAggregate Handle(AddItemToBasket command) { var basket = _domainRepository.GetById <Basket>(command.Id); var product = _domainRepository.GetById <Product>(command.ProductId); basket.AddItem(product, command.Quantity); return(basket); }
internal void AddItemToBasket(AddItemToBasket cmd) { if (State != BasketState.Pending) { throw new InvalidStateException(AggregateId, $"Cannot add item. Basket is {State}"); } RaiseEvent(new BasketItemAdded(cmd.AggregateId, cmd.ProductId, cmd.ProductName, cmd.Price, cmd.Quantity)); }
public async Task <IActionResult> AddItem([FromBody] AddItemModel model) { // TODO: get product price and name from api var useCase = new AddItemToBasket(model.BasketId, model.ProductId, model.ProductName, model.Price, model.Quantity); var cmd = new Command <AddItemToBasket>(useCase); var basket = await _commandHandler.Execute <PendingBasket>(cmd); return(Ok(BasketModel.FromAggregateRoot(basket))); }
public void When_AddItem_BasketAddItemAdded() { Given(InitialEvents.ToArray()); var command = new AddItemToBasket(id, productId, "Test Item", 2, 10); command.Metadata.CausationId = command.Metadata.CommandId; command.Metadata.CorrelationId = causationAndCorrelationId; When(command); var expectedEvent = new BasketItemAdded(id, productId, "Test Item", 2, 10); expectedEvent.Metadata.CausationId = command.Metadata.CommandId; expectedEvent.Metadata.CorrelationId = causationAndCorrelationId; expectedEvent.Metadata.ProcessId = command.Metadata.ProcessId; Then(expectedEvent); }
public IHttpActionResult AddItem([FromBody] AddItemModel model) { var basketId = model.BasketId; if (model.BasketId == Guid.Empty) { basketId = Guid.NewGuid(); _commandDispatcher.ExecuteCommand <BasketAggregate>(new CreateBasket(basketId)); } // TODO: get model price and name from api by id var cmd = new AddItemToBasket( basketId, model.ProductId, model.Name, model.Price, model.Quantity); var basket = _commandDispatcher.ExecuteCommand <BasketAggregate>(cmd); return(Ok(BasketModel.FromAggregate(basket))); }
private async Task <BasketEvent> AddItemToBasketAction(AddItemToBasket message) { var productActorResult = await this.ProductsActorRef.Ask <ProductsActor.ProductEvent>( new ProductsActor.UpdateStock(message.ProductId, -message.Amount) ); switch (productActorResult) { case ProductsActor.StockUpdated _: { var product = ((ProductsActor.StockUpdated)productActorResult).Product; return(AddToBasket(product, message.Amount) as ItemAdded); } case ProductsActor.ProductNotFound _: return(new ProductNotFound()); case ProductsActor.InsufficientStock _: return(new NotInStock()); default: throw new NotImplementedException($"Unknown response: {productActorResult.GetType().ToString()}"); } }
public static Basket ToModel(this AddItemToBasket item) => new Basket { GoodsId = item.GoodsId, UserId = item.UserId, Count = item.Count };
public async Task <ActionResult> AddItem(AddItemToBasket command) { return(await _commandProcessor(command).ToActionResult()); }