コード例 #1
0
        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()}");
            }
        }
コード例 #2
0
        public void Handle(AddItemToBasket command)
        {
            var basket  = this.basketRepository.GetById(command.Id);
            var product = this.productRepository.GetById(command.ProductId);

            basket.AddItem(product, command.Quantity);
        }
コード例 #3
0
        public async Task <IActionResult> AddItemToBasket([FromBody] AddItemToBasket command)
        {
            command.UserId = UserId;
            var result = await _mediator.Send(command);

            return(JsonResult(result));
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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));
        }
コード例 #6
0
ファイル: BasketsController.cs プロジェクト: 7c78/eventshop
        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)));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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)));
        }
コード例 #9
0
        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()}");
            }
        }
コード例 #10
0
ファイル: BasketAdapter.cs プロジェクト: vaskiv99/shop-api
 public static Basket ToModel(this AddItemToBasket item) => new Basket
 {
     GoodsId = item.GoodsId,
     UserId  = item.UserId,
     Count   = item.Count
 };
コード例 #11
0
 public async Task <ActionResult> AddItem(AddItemToBasket command)
 {
     return(await _commandProcessor(command).ToActionResult());
 }