Ejemplo n.º 1
0
        public LeagueUser JoinLeague(LeagueUser league)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand("INSERT INTO league_users (league_id, user_id) VALUES (@league_id, @user_id)", conn);
                    cmd.Parameters.AddWithValue("@league_id", league.LeagueId);
                    cmd.Parameters.AddWithValue("@user_id", league.UserId);
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows && reader.Read())
                    {
                        league = GetLeagueFromReader(reader);
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }

            return(league);
        }
Ejemplo n.º 2
0
 private LeagueUserViewModel(LeagueUser leagueUser, string title, string titleLink, string picture)
 {
     LeagueUser = leagueUser;
     Picture    = picture;
     TitleLink  = titleLink;
     Title      = title;
 }
Ejemplo n.º 3
0
        public List <LeagueUser> GetLeagueUsers()
        {
            List <LeagueUser> returnLeagueUsers = new List <LeagueUser>();

            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT * FROM league_users", conn);
                    SqlDataReader reader = cmd.ExecuteReader();

                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            LeagueUser l = GetLeagueFromReader(reader);
                            returnLeagueUsers.Add(l);
                        }
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }

            return(returnLeagueUsers);
        }
Ejemplo n.º 4
0
        public async Task <bool> kickFromLeague(string leagueID, int userID, int kickedUserID)
        {
            bool response = false;

            // Find user to be removed, and makes sure that the user doing the removal is the LeagueHost.
            LeagueUser result = (from leagueUser in _context.LeagueUsers
                                 join league in _context.Leagues
                                 on leagueUser.LeagueId equals league.LeagueId
                                 where leagueUser.LeagueId == leagueID && leagueUser.UserId == kickedUserID && league.LeagueHost == userID
                                 select new LeagueUser
            {
                UserId = leagueUser.UserId,
                LeagueId = leagueUser.LeagueId
            }).SingleOrDefault();

            // Remove the LeagueUser from the database if it were found.
            try
            {
                _context.LeagueUsers.Remove(result);
                await _context.SaveChangesAsync();

                response = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(response);
        }
Ejemplo n.º 5
0
        public async Task <bool> leaveLeague(string leagueID, int userID)
        {
            bool response = false;

            // Find LeagueUser, and making sure it is not the Host who is attempting to leave.
            LeagueUser result = (from leagueUser in _context.LeagueUsers
                                 join league in _context.Leagues
                                 on leagueUser.LeagueId equals league.LeagueId
                                 where leagueUser.LeagueId == leagueID && leagueUser.UserId == userID && league.LeagueHost != leagueUser.UserId
                                 select new LeagueUser
            {
                UserId = leagueUser.UserId,
                LeagueId = leagueUser.LeagueId
            }).SingleOrDefault();

            // Remove the LeagueUser from the database if it were found.
            try
            {
                _context.LeagueUsers.Remove(result);
                await _context.SaveChangesAsync();

                response = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }

            return(response);
        }
Ejemplo n.º 6
0
        private LeagueUser CreateLeagueUser(int leagueId, string userId)
        {
            var leagueUser = new LeagueUser
            {
                LeagueId = leagueId,
                UserId   = userId
            };

            return(leagueUser);
        }
Ejemplo n.º 7
0
 public LeagueUserDto(LeagueUser leagueUser)
 {
     ID       = leagueUser.ID;
     LeagueID = leagueUser.ID;
     UserID   = leagueUser.UserID;
     HasLeft  = leagueUser.HasLeft;
     Username = leagueUser.User.UserName;
     Points   = leagueUser.Points;
     Rank     = leagueUser.Rank;
 }
Ejemplo n.º 8
0
        public async Task <string> joinLeague(string leagueID)
        {
            // Obtain LeagueID and UserID
            var userID = Int32.Parse(HttpContext.User.FindFirst(ClaimTypes.Name).Value);

            // Return LeagueUser Object.
            LeagueUser result = await _leagueService.joinLeague(leagueID, userID);

            return(Newtonsoft.Json.JsonConvert.SerializeObject(result));
        }
Ejemplo n.º 9
0
        private LeagueUser GetLeagueFromReader(SqlDataReader reader)
        {
            LeagueUser l = new LeagueUser()
            {
                LeagueId = Convert.ToInt32(reader["league_id"]),
                UserId   = Convert.ToInt32(reader["user_id"]),
            };

            return(l);
        }
Ejemplo n.º 10
0
        public async Task Join_OldUser_HasLeftFalse()
        {
            var        league        = LeagueUtility.CreateLeague(dbContext);
            var        user          = DbContextUtility.AddNew <ApplicationUser>(dbContext);
            LeagueUser oldLeagueUser = CreateOldLeagueUser(league, user);

            var leagueUser = await testObj.Join(league.ID, user.Id);

            Assert.IsFalse(leagueUser.HasLeft);
            Assert.AreEqual(oldLeagueUser.ID, leagueUser.ID);
        }
Ejemplo n.º 11
0
        private LeagueUser CreateOldLeagueUser(League league, ApplicationUser user)
        {
            var oldLeagueUser = new LeagueUser(league.ID, user.Id)
            {
                HasLeft = true
            };

            dbContext.LeagueUsers.Add(oldLeagueUser);
            dbContext.SaveChanges();
            return(oldLeagueUser);
        }
Ejemplo n.º 12
0
        public async Task <League> createLeague(int leagueHost, string leagueName, bool openEnrollment)
        {
            // Auto generate leagueID as a string, and return the entire league instead of a booleon.
            // O0IL and 1 have been removed for their confusing similarities.
            var chars    = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
            var leagueID = new char[8];
            var random   = new Random();

            for (int i = 0; i < leagueID.Length; i++)
            {
                leagueID[i] = chars[random.Next(chars.Length)];
            }


            // Create New League using passed in information.
            League league = new League();

            league.LeagueId           = new String(leagueID);
            league.LeagueHost         = leagueHost;
            league.LeagueName         = leagueName;
            league.LeagueCreationDate = DateTime.Now.ToString();
            league.OpenEnrollment     = openEnrollment;

            // Add league to the entity model
            var dbResponse  = _context.Leagues.Add(league);
            var addedLeague = dbResponse.Entity;

            // Add league to the database
            try
            {
                await _context.SaveChangesAsync();

                LeagueUser hosting = await joinLeague(league.LeagueId, leagueHost);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                league = null;
            }

            return(league);
        }
Ejemplo n.º 13
0
        public async Task <LeagueUser> joinLeague(string leagueID, int userID)
        {
            // Find league using its ID.
            var leagues = (from League in _context.Leagues
                           where League.LeagueId == leagueID
                           select new League
            {
                LeagueId = League.LeagueId,
                LeagueName = League.LeagueName,
                LeagueHost = League.LeagueHost,
                LeagueCreationDate = League.LeagueCreationDate
            }).SingleOrDefault();


            if (leagues.LeagueId == leagueID)
            {
                // Create new LeagueUser Entry
                LeagueUser leagueUser = new LeagueUser();
                leagueUser.LeagueId       = leagueID;
                leagueUser.UserId         = userID;
                leagueUser.PrivilegeLevel = 0;

                // Add league to the entity model
                var dbResponse  = _context.LeagueUsers.Add(leagueUser);
                var addedLeague = dbResponse.Entity;

                // Add LeagueUser to the database
                try
                {
                    await _context.SaveChangesAsync();

                    return(leagueUser);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }
            }

            return(null);
        }
Ejemplo n.º 14
0
        public static LeagueUserViewModel Create(LeagueUser leagueUser, ICdnService cdnService, bool showUser, IUrlHelper urlHelper)
        {
            string title;
            string titleLink;
            string picture;

            if (showUser)
            {
                title     = leagueUser.DisplayName;
                titleLink = urlHelper.Action("Home", "User", new { leagueUser.UserID });
                picture   = cdnService.GetUserProfilePicUrl(leagueUser.User.Id, leagueUser.User.ProfilePicKey, ClimbImageRules.ProfilePic);
            }
            else
            {
                title     = leagueUser.League.Name;
                titleLink = urlHelper.Action("Home", "League", new { leagueUser.LeagueID });
                picture   = cdnService.GetImageUrl(leagueUser.League.Game.LogoImageKey, ClimbImageRules.GameLogo);
            }

            return(new LeagueUserViewModel(leagueUser, title, titleLink, picture));
        }
Ejemplo n.º 15
0
        public async Task <LeagueUser> Join(int leagueID, string userID)
        {
            if (!await dbContext.Leagues.AnyAsync(l => l.ID == leagueID))
            {
                throw new NotFoundException(typeof(League), leagueID);
            }

            var user = await dbContext.Users
                       .AsNoTracking()
                       .FirstOrDefaultAsync(u => u.Id == userID);

            if (user == null)
            {
                throw new NotFoundException(typeof(ApplicationUser), userID);
            }

            var leagueUser = await dbContext.LeagueUsers
                             .IgnoreQueryFilters()
                             .FirstOrDefaultAsync(lu => lu.LeagueID == leagueID && lu.UserID == userID);

            if (leagueUser != null)
            {
                leagueUser.HasLeft = false;
            }
            else
            {
                leagueUser = new LeagueUser(leagueID, userID)
                {
                    JoinDate = DateTime.UtcNow
                };
                dbContext.Add(leagueUser);
            }

            leagueUser.DisplayName = user.UserName;

            await dbContext.SaveChangesAsync();

            return(leagueUser);
        }
Ejemplo n.º 16
0
 public SharedLeagueUsers(LeagueUser requester, LeagueUser challenged)
 {
     Requester  = requester;
     Challenged = challenged;
 }
Ejemplo n.º 17
0
 public void AddUserToLeague(LeagueUser leagueUser)
 {
     _repository.Add(leagueUser);
     _repository.Save();
 }
Ejemplo n.º 18
0
        public ActionResult <LeagueUser> JoinLeague(LeagueUser league)
        {
            LeagueUser userJoined = leagueUserDAO.JoinLeague(league);

            return(Ok());
        }
Ejemplo n.º 19
0
 public bool UserIsAlreadyInLeague(LeagueUser leagueUser)
 {
     return(_repository.FindBy(x => x.LeagueId == leagueUser.LeagueId && x.UserId == leagueUser.UserId).Any());
 }