Beispiel #1
0
        async Task ICardRepository.FavouriteCardAsync(int id, int userId, bool favourite, CancellationToken cancellationToken)
        {
            var existingFavourite = await _context
                                    .CardFavourite
                                    .SingleOrDefaultAsync(x => x.CardFk == id && x.UserFk == userId, cancellationToken : cancellationToken);

            if (favourite && existingFavourite == null)
            {
                var newCardFavourite = new CardFavourite
                {
                    CardFk = id,
                    UserFk = userId,
                };

                _context.CardFavourite.Add(newCardFavourite);

                await _context.SaveChangesAsync(cancellationToken : cancellationToken);
            }
            else if (!favourite && existingFavourite != null)
            {
                _context.CardFavourite.Remove(existingFavourite);

                await _context.SaveChangesAsync(cancellationToken : cancellationToken);
            }
        }
Beispiel #2
0
        async Task <DeckData> IDeckRepository.CreateDeckAsync(DeckCreateData deck, CancellationToken cancellationToken)
        {
            var entity = new Deck
            {
                Description = deck.Description,
                MaxCards    = 5,
                Name        = deck.Name,
                UserFk      = deck.UserId,
            };

            _context.Deck.Add(entity);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            return(_deckMapper.Map(entity));
        }
        async Task <CardCollectionData[]> ICardCollectionRepository.AddCardsAsync(IEnumerable <int> cardIds, int userId, CancellationToken cancellationToken)
        {
            if (cardIds == null)
            {
                return(null);
            }

            if (!cardIds.Any())
            {
                return(Array.Empty <CardCollectionData>());
            }

            var cardCollections = cardIds
                                  .Select(x => new CardCollection
            {
                CardFk      = x,
                CreatedTime = DateTime.UtcNow,
                UserFk      = userId,
            })
                                  .ToArray();

            await _context.CardCollection.AddRangeAsync(cardCollections);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            var results = cardCollections.Select(x => _cardCollectionMapper.Map(x)).ToArray();

            return(results);
        }
Beispiel #4
0
        async Task <GameDeckData> IGameDeckRepository.AddGameDeckAsync(int gameUserId, string name, string description, int[] cardIds, CancellationToken cancellationToken)
        {
            var gameDeck = new GameDeck
            {
                CreatedTime = DateTime.UtcNow,
                Description = description,
                GameUserFk  = gameUserId,
                Name        = name,
            };

            if (cardIds != null)
            {
                gameDeck.GameDeckCardCollection = cardIds.Select(x => new GameDeckCardCollection
                {
                    CardFk = x,
                }).ToArray();
            }

            _context.GameDeck.Add(gameDeck);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            var data = new GameDeckData
            {
                CreatedTime = gameDeck.CreatedTime,
                Description = description,
                GameUserId  = gameUserId,
                Id          = gameDeck.GameDeckPk,
                Name        = name,
            };

            return(data);
        }
Beispiel #5
0
        async Task <GameData> IGameRepository.AddGameAsync(GameCreateData game, CancellationToken cancellationToken)
        {
            var data = new Game
            {
                Columns    = game.Columns,
                GameTypeFk = (int)game.Type,
                Rows       = game.Rows,
                StartTime  = DateTime.UtcNow,
            };

            _context.Game.Add(data);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            return(await GetGameByIdInternalAsync(data.GamePk, cancellationToken : cancellationToken));
        }
Beispiel #6
0
        async Task <UserData> IUserRepository.CreateUserAsync(string identifier, string idp, string name, long coins, CancellationToken cancellationToken)
        {
            var efUser = new User
            {
                Identifier = identifier,
                IdPsource  = idp,
                FullName   = name,

                Coins = coins,
            };

            await _context.User.AddAsync(efUser, cancellationToken : cancellationToken);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            return(_userMapper.Map(efUser));
        }
Beispiel #7
0
        async Task <TurnData> ITurnRepository.AddTurnAsync(TurnData turn, CancellationToken cancellationToken)
        {
            var newTurn = new Turn
            {
                CurrentGameUserFk = turn.CurrentGameUserId,
                GameFk            = turn.GameId,
                StartTime         = DateTime.UtcNow,
            };

            _context.Turn.Add(newTurn);
            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            turn.Id        = newTurn.TurnPk;
            turn.StartTime = newTurn.StartTime;

            return(turn);
        }
Beispiel #8
0
        async Task <MoveData> IMoveRepository.AddMoveAsync(MoveData move, CancellationToken cancellationToken)
        {
            var newMove = new Move
            {
                GameDeckCardCollectionFk = move.GameDeckCardCollectionId,
                Column      = move.Column,
                CreatedTime = DateTime.UtcNow,
                Row         = move.Row,
                TurnFk      = move.TurnId,
            };

            _context.Move.Add(newMove);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            return(move);
        }
Beispiel #9
0
        async Task <GameUserData> IGameUserRepository.AddGameUserAsync(int gameId, int userId, CancellationToken cancellationToken)
        {
            var gameUser = new GameUser
            {
                GameFk     = gameId,
                JoinedTime = DateTime.UtcNow,
                UserFk     = userId,
            };

            _context.GameUser.Add(gameUser);

            await _context.SaveChangesAsync(cancellationToken : cancellationToken);

            var newData = new GameUserData
            {
                GameId     = gameId,
                Id         = gameUser.GameUserPk,
                JoinedTime = gameUser.JoinedTime,
                Order      = gameUser.Order,
                UserId     = userId,
            };

            return(newData);
        }