コード例 #1
0
        public async Task <Inventory> IncrementInventoryAsync(Guid itemId, decimal quantity, int index, CancellationToken cancellationToken)
        {
            _logger.LogDebug($"Increment inventory index {index}");

            var item = await _itemRepository.GetByKeyAsync(itemId, cancellationToken);

            if (item == null)
            {
                throw new Exception("Could not find item");
            }

            var inventories = await _inventoryRepository
                              .GetByItemId(itemId, cancellationToken);

            var inventory = inventories?.FirstOrDefault();

            if (inventory == null)
            {
                inventory = new Inventory
                {
                    Count  = quantity,
                    Id     = Guid.NewGuid(),
                    ItemId = itemId
                };

                await _inventoryRepository.CreateAsync(inventory, cancellationToken);
            }
            else
            {
                inventory.Count += quantity;
                await _inventoryRepository.UpdateAsync(inventory, cancellationToken);
            }

            return(inventory);
        }