Ejemplo n.º 1
0
        async Task <DeckData> IDeckRepository.UpdateDeckAsync(int id, DeckUpdateData update, CancellationToken cancellationToken)
        {
            var deck = await _context.Deck.SingleOrDefaultAsync(x => x.DeckPk == id, cancellationToken : cancellationToken);

            if (deck == null)
            {
                throw new CardHeroDataException($"Deck {id} does not exist.");
            }

            if (update.CardCollectionIds.IsSet && update.CardCollectionIds.Value != null)
            {
                var existingCards = await _context
                                    .DeckCardCollection
                                    .Where(x => x.DeckFk == id)
                                    .ToListAsync(cancellationToken: cancellationToken);

                foreach (var ec in existingCards)
                {
                    _context.DeckCardCollection.Remove(ec);
                }

                foreach (var d in update.CardCollectionIds.Value)
                {
                    _context.DeckCardCollection.Add(new DeckCardCollection
                    {
                        CardCollectionFk = d,
                        DeckFk           = id,
                    });
                }
            }

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            return(await GetDeckByIdInternalAsync(id, deck.UserFk, cancellationToken : cancellationToken));
        }
Ejemplo n.º 2
0
        async Task IDeckService.UpdateCollectionAsync(int id, int userId, IEnumerable <int> cardCollectionIds, CancellationToken cancellationToken)
        {
            var deck = await _deckRepository.GetDeckByIdAsync(id, userId, cancellationToken : cancellationToken);

            if (deck == null)
            {
                throw new InvalidDeckException("You do not have access to this deck.");
            }

            if (deck.UserId != userId)
            {
                throw new InvalidDeckException("You do not have access to this deck.");
            }

            var distincted = (cardCollectionIds ?? Array.Empty <int>()).Distinct().ToArray();

            if (distincted.Length > deck.MaxCards)
            {
                throw new InvalidDeckException("Over maximum number of cards added.");
            }

            var cardCollectionResults = await _cardCollectionRepository.FindCardCollectionsAsync(
                new Data.Abstractions.CardCollectionSearchFilter
            {
                Ids    = distincted,
                UserId = userId,
            },
                cancellationToken : cancellationToken
                );

            var ownedCount = cardCollectionResults.TotalCount;

            if (ownedCount != distincted.Length)
            {
                throw new InvalidCardException("You do not own some of the cards.");
            }

            var deckUpdate = new DeckUpdateData
            {
                CardCollectionIds = distincted,
            };

            await _deckRepository.UpdateDeckAsync(id, deckUpdate, cancellationToken : cancellationToken);
        }