Exemple #1
0
        public ActionResult <StockLevel> Add(int productId, [FromBody] AddStocksCommand command)
        {
            var quantityInStock = _stockService.AddStock(productId, command.Amount);
            var stockLevel      = new StockLevel(quantityInStock);

            return(Ok(stockLevel));
        }
Exemple #2
0
        public async Task HandleAsync(AddStocksCommand command, CancellationToken cancellationToken)
        {
            using (var context = dbContextFactory.CreateDbContext())
            {
                var productIds = command.Items.Select(i => i.ProductId).ToList();

                var productLookup = await GetProductLookupAsync(context, productIds, cancellationToken).ConfigureAwait(false);

                requestValidator.ValidateAndThrow(command, productLookup);

                // This is for idempotence. We check only the TransactionId because we assume that if stocks for one product in a transaction is added then so are the others.
                var stocksAlreadyAdded = await context.ProductStockAddedEvents.AnyAsync(evt => evt.TransactionId == command.TransactionId, cancellationToken).ConfigureAwait(false);

                if (stocksAlreadyAdded)
                {
                    return;
                }

                foreach (var addedStock in command.Items)
                {
                    context.ProductStockAddedEvents.Add(new ProductStockAddedEvent
                    {
                        ProductId     = addedStock.ProductId,
                        Quantity      = addedStock.Quantity,
                        TransactionId = command.TransactionId
                    });
                }

                await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }
        }
        public ActionResult <StockLevel> Add(
            int productId,
            [FromBody] AddStocksCommand command,
            [FromServices] AddStocks useCase
            )
        {
            var product    = useCase.Handle(productId, command.Amount);
            var stockLevel = new StockLevel(product.QuantityInStock);

            return(Ok(stockLevel));
        }
Exemple #4
0
        public ActionResult <StockLevel> Add(
            int productId,
            [FromBody] AddStocksCommand command,
            [FromServices] AddStocks useCase
            )
        {
            var product    = useCase.Handle(productId, command.Amount);
            var stockLevel = _mappingService.Map <Product, StockLevel>(product);

            return(Ok(stockLevel));
        }