Example #1
0
        public async Task <IActionResult> AddToStock([FromRoute] long productId, [FromBody] AddToStockHttpRequest addToStockHttpRequest)
        {
            var queryProductServiceRequest = new QueryProductCommand(0, 1)
            {
                ProductId = productId
            };

            StockActionHttpResponse stockActionHttpResponse = null;
            await _distributedLockManager.LockAsync(StockOperationLockKey(productId),
                                                    async() =>
            {
                await _mediator.Send(queryProductServiceRequest);

                AddToStockCommand addToStockCommand = addToStockHttpRequest != null
                                                                                                  ? new AddToStockCommand(productId, addToStockHttpRequest.Count, addToStockHttpRequest.CorrelationId)
                                                                                                  : null;

                StockActionResponse stockActionResponse = await _mediator.Send(addToStockCommand);

                stockActionHttpResponse = stockActionResponse.ToStockActionHttpResponse();
            });


            return(StatusCode((int)HttpStatusCode.OK, stockActionHttpResponse));
        }
        public async Task <IActionResult> AddToStock([FromBody] AddToStockHttpRequest?addToStockHttpRequest)
        {
            AddToStockCommand addToStockCommand = new AddToStockCommand(addToStockHttpRequest?.ProductId ?? string.Empty,
                                                                        addToStockHttpRequest?.Count ?? 0,
                                                                        addToStockHttpRequest?.CorrelationId ?? string.Empty);
            await _executionContext.ExecuteAsync(addToStockCommand, CancellationToken.None);

            return(StatusCode((int)HttpStatusCode.OK));
        }
        public async Task <bool> Handle(RollbackDecreaseFromStockCommand request, CancellationToken cancellationToken)
        {
            IStockActionRepository stockActionRepository = _stockDbContext.StockActionRepository;
            StockAction            stockAction           = await stockActionRepository.GetByCorrelationIdAsync(request.CorrelationId, cancellationToken);

            AddToStockCommand addToStockCommand = new AddToStockCommand(stockAction.ProductId, stockAction.Count, $"{stockAction.CorrelationId}-rollback");
            await _domainMessageBroker.SendAsync(addToStockCommand, cancellationToken);

            return(true);
        }
Example #4
0
        public async Task When_adding_stock_Aggregate_quantity_should_be_increased_by_command_amount()
        {
            AddToStockCommand command;
            SkuStockCreated   created;
            StockAdded        added;
            var id       = Guid.NewGuid();
            var scenario = await AggregateScenario.New <SkuStock, SkuStockCommandsHandler>()
                           .Given(created = new SkuStockCreated(id, _skuId, 50, TimeSpan.FromMilliseconds(100)),
                                  added   = new StockAdded(id, 10, "test batch 2"))
                           .When(command  = new AddToStockCommand(id, _skuId, 10, "test batch"))
                           .Then(new StockAdded(id, command.Quantity, command.BatchArticle))
                           .Run();

            Assert.Equal(command.Quantity + created.Quantity + added.Quantity, scenario.Aggregate.Quantity);
        }