public async Task <IActionResult> TryPurchaseItem(ItemPurchaseRequestDTO purchaseRequest)
        {
            var result = await _characterService.TryPurchaseItem(purchaseRequest);

            switch (result.Result)
            {
            case GoldExpenseResult.Authorized:        return(Ok(result));

            case GoldExpenseResult.ItemNotFound:
            case GoldExpenseResult.CharacterNotFound: return(NotFound(result));

            default:                                  return(BadRequest(result));
            }
        }
Beispiel #2
0
 public ItemPurchase(ItemPurchaseRequestDTO purchaseRequest) : base(purchaseRequest)
 {
     RequestedQuantity = purchaseRequest.Quantity;
 }
Beispiel #3
0
        public async Task <ItemPurchaseResponseDTO> TryPurchaseItem(ItemPurchaseRequestDTO purchaseRequest)
        {
            var purchase = new ItemPurchase(purchaseRequest);

            do
            {
                if (purchaseRequest.Quantity <= 0)
                {
                    purchase.Result = GoldExpenseResult.BadRequest;
                    break;
                }

                var item = await _itemsRepo.GetItemAsync(purchaseRequest.ItemName);

                if (item == null || item.MaxQuantity == 0)
                {
                    purchase.Result = GoldExpenseResult.ItemNotFound;
                    break;
                }

                purchase.Item             = item;
                purchase.ExpenseRequested = item.Cost * purchase.RequestedQuantity;

                var character = await _characterRepo.GetCharacterItemsAsync(purchase.CharacterId);

                if (character == null)
                {
                    purchase.Result = GoldExpenseResult.CharacterNotFound;
                    break;
                }

                var characterItem = character.Items.FirstOrDefault(i => i.Item.Name == item.Name);

                if (item.MaxQuantity > 0 && purchase.RequestedQuantity + characterItem?.Quantity > item.MaxQuantity)
                {
                    purchase.Result = GoldExpenseResult.ReachedItemMaxQuantity;
                    break;
                }

                purchase = (ItemPurchase) await TryExpendGoldInternal(purchase, saveChanges : false);

                if (purchase.Result == GoldExpenseResult.Authorized)
                {
                    purchase.Item = item;
                    purchase.PurchasedQuantity = purchase.RequestedQuantity;

                    if (characterItem == null)
                    {
                        character.Items.Add(new InventoryItem(purchase.Item, purchase.PurchasedQuantity, false));
                    }
                    else
                    {
                        characterItem.Quantity += purchase.PurchasedQuantity;
                    }

                    await _characterRepo.SaveChangesAsync();
                }
            } while (false);

            return(_mapper.Map <ItemPurchaseResponseDTO>(purchase));
        }