Example #1
0
        private void HandleNullDeckCardStoreModification(Deck deck, Card card, DeckCardStoreAction action)
        {
            if (action == DeckCardStoreAction.RemoveCard)
            {
                _logger.LogWarning("The card {cardId} could not be removed as it did not exist within deck {deckId}.", card.Id, deck.Id);
                return;
            }

            _logger.LogInformation("Attempting to add card {cardId} to deck {deckId} with a quantity of 1.", card.Id, deck.Id);
            var deckCards = new DeckCards {
                Deck = deck, Card = card, Quantity = 1
            };

            deck.DeckCards.Add(deckCards);
        }
Example #2
0
        private void HandleDeckCardStoreModification(Deck deck, Card card, DeckCardStoreAction action)
        {
            // A service/repo for the DeckCards might be overkill
            var deckCard = deck.DeckCards.SingleOrDefault(m => m.CardId == card.Id);

            if (deckCard == null)
            {
                HandleNullDeckCardStoreModification(deck, card, action);
                return;
            }

            switch (action)
            {
            case DeckCardStoreAction.AddCard:
                deckCard.IncrementQuantity();
                _logger.LogInformation("Received action {action}. Adding card {cardId} to deck {deckId} resulting in a matching card quantity of {quantity}.", action, card.Id, deck.Id, deckCard.Quantity);
                break;

            case DeckCardStoreAction.RemoveCard:
                deckCard.DecrementQuantity();
                _logger.LogInformation("Received action {action}. Removing card {cardId} from deck {deckId} with a matching card quantity of {quantity} remaining.", action, card.Id, deck.Id, deckCard.Quantity);
                break;

            default:
                throw new InvalidOperationException($"Unknown action: {action}");
            }

            if (deckCard.Quantity == 0)
            {
                _logger.LogInformation("The card {cardId} was completely removed from deck {deckId} due to having a quantity of 0.", card.Id, deck.Id);
                _context.DeckCards.Remove(deckCard);
                return;
            }

            _context.DeckCards.Update(deckCard);
        }