Beispiel #1
0
 public Deck(IDeckBuilder deckBuilder)
 {
     if (deckBuilder != null)
     {
         _deckOfCards = deckBuilder.CreateDeck();
     }
     else
     {
         throw new ArgumentNullException(nameof(deckBuilder));
     }
 }
        public async Task <ActionResult> CreateDeck(string name)
        {
            var deck = new Deck(name);

            try
            {
                var deckInMemory = _deckBuilder.CreateDeck();
                await _deckRepository.AddAsync(deck);

                await _deckRepository.Context.SaveChangesAsync();

                var i     = 1;
                var toAdd = new List <CardInDeck>();
                foreach (var cardInMemory in deckInMemory)
                {
                    var card = await _cardRepository.FirstAsync(x =>
                                                                x.Suit == cardInMemory.Suit && x.Rank == cardInMemory.Rank);

                    toAdd.Add(new CardInDeck {
                        CardId = card.Key, DeckId = deck.Key, NumberInDeck = i
                    });
                    i++;
                }

                await _cardInDeckRepository.AddRangeAsync(toAdd);

                await _cardInDeckRepository.Context.SaveChangesAsync();

                return(Ok());
            }
            catch (DbUpdateException)
            {
                return(base.BadRequest($"Deck {name} already exist"));
            }
            catch (Exception)
            {
                await _deckRepository.RemoveAsync(deck);

                return(StatusCode(StatusCodes.Status500InternalServerError, "Sorry"));
            }
        }