public Player LoginPlayer(Player player)
        {
            Player player1 = players.FirstOrDefault(x => x.Fname == player.Fname && x.Lname == player.Lname); // check if the player is in the Db

            if (player1 == null)                                                                              // create new player if none exists
            {
                player1 = new Player()
                {
                    Fname = player.Fname,
                    Lname = player.Lname
                };
                players.Add(player1);                // ass new player
                _dbContext.SaveChanges();            // save new player to Db

                try
                {
                    Player player2 = players.FirstOrDefault(x => x.playerId == player1.playerId);                    // check if the player is in the Db
                    return(player2);
                }
                catch (ArgumentNullException ex)
                {
                    _logger.LogInformation($"Saving a player to the Db threw an error, {ex}");
                }
            }
            return(player1);
        }
        /// <summary>
        /// Takes a User and returns the edited version of the User after saving it to the Db.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public User EditUser(User user)
        {
            // search Db for the user
            User user1 = GetUserById(user.UserId);

            // transfer over all the new values
            user1.Fname          = user.Fname;
            user1.Lname          = user.Lname;
            user1.DefaultStoreId = user.DefaultStoreId;
            user1.Email          = user.Email;
            _dbContext.SaveChanges();

            // search the User again to verify that the new User is in the Db
            User user2 = GetUserById(user1.UserId);

            // return the edited User
            return(user2);
        }
예제 #3
0
        public Player LoginPlayer(Player player)
        {
            Player p1 = players.FirstOrDefault(x => x.Fname == player.Fname && x.Lname == player.Lname);

            if (p1 == null)
            {
                p1 = new Player()
                {
                    Fname = player.Fname,
                    Lname = player.Lname
                };
                players.Add(p1);
                _context.SaveChanges();
                Player p2 = players.FirstOrDefault(x => x.PlayerId == p1.PlayerId);
                return(p2);
            }

            return(p1);
        }