예제 #1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            string username = HttpContext.Session.GetString("username");

            if (id == null)
            {
                return(RedirectToPage("JoinTeam"));
            }

            var joinCode = Request.Form["joinCode"];

            AppCondition app = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1));

            Team = await _context.Team.FirstOrDefaultAsync(t => t.TeamId.Equals(id) && t.EventID.Equals(app.EventID));

            User loginUser = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username));

            if (Team != null)
            {
                //We have to make sure that the code is correct
                if (Team.JoinCode != joinCode)
                {
                    Message = "The join Code is Incorrect!! Try Again!!";
                    return(Page());
                }
                //else just add the user to the respective team
                Member newMember = new Member()
                {
                    UserID   = loginUser.UserId,
                    TeamID   = Team.TeamId,
                    Email    = loginUser.Email,
                    EventID  = Team.EventID,
                    FullName = loginUser.FullName,
                    UserName = loginUser.UserName
                };

                _context.Member.Add(newMember);
                await _context.SaveChangesAsync();

                return(RedirectToPage("MyTeam"));
            }

            return(RedirectToPage("./JoinTeam"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            string       username = HttpContext.Session.GetString("username");
            AppCondition app      = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1));

            //Now we have to check wether to vote or unvote
            //We will vote if a user hasn't vote for that team yet

            //IList<Vote> allUserVotes = await _context.Vote.Where(v => v.UserName.Equals(username) && v.EventID.Equals(app.EventID)).ToListAsync();

            //foreach (var vote in allUserVotes)
            //{
            //    //Now we run through the entire loop if
            //    //The team with teamid = id is found in the list
            //    //Then we unvote it

            //    if (vote.TeamID.Equals(id))
            //    {
            //        //WE have found the vote
            //        _context.Vote.Remove(vote);
            //        await _context.SaveChangesAsync();
            //    }

            //}

            Vote vote = await _context.Vote.FirstOrDefaultAsync(v => v.UserName.Equals(username) && v.TeamID.Equals(id) && v.EventID.Equals(app.EventID));

            Team team = await _context.Team.FirstOrDefaultAsync(t => t.TeamId.Equals(id));

            User user = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username));

            if (vote == null)
            {
                //That means user wish to vote for this team
                Vote newVote = new Vote()
                {
                    EventID  = app.EventID,
                    TeamID   = team.TeamId,
                    TeamName = team.TeamName,
                    UserID   = user.UserId,
                    UserName = user.UserName
                };
                _context.Vote.Add(newVote);
                await _context.SaveChangesAsync();
            }
            else
            {
                //Then we will have to unvote this team as user has already votes fo4r this team
                _context.Vote.Remove(vote);
                await _context.SaveChangesAsync();
            }


            IList <Judge> allJudges = await _context.Judge.Where(j => j.EventID.Equals(app.EventID)).ToListAsync();

            //Before we send the user back we need to the voting
            //We need to perform certain checks for that
            foreach (var judge in allJudges)
            {
                if (judge.UserID.Equals(user.UserId))
                {
                    return(RedirectToPage("JudgeVoting"));
                }
            }

            //Other wise the user is just a normal user
            //So we just send him/her back to confirm voting
            return(RedirectToPage("Voting"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(RedirectToPage("AllUsers"));
            }

            User = await _context.User.FirstOrDefaultAsync(u => u.UserId.Equals(id));

            if (User == null)
            {
                return(RedirectToPage("AllUsers"));
            }

            //WE need to start removing the traces of the users throughout the application
            //1. Let begin by the votes
            allVotes = await _context.Vote.Where(v => v.UserID.Equals(User.UserId)).ToListAsync();

            foreach (var vote in allVotes)
            {
                _context.Vote.Remove(vote);
                await _context.SaveChangesAsync();
            }

            //2. Let remove his membership from various teams if he has any
            IList <Member> allMembersShips = await _context.Member.Where(m => m.UserID.Equals(User.UserId)).ToListAsync();

            foreach (var member in allMembersShips)
            {
                _context.Member.Remove(member);
                await _context.SaveChangesAsync();
            }

            //3. Judges position
            IList <Judge> AsJudge = await _context.Judge.Where(j => j.UserID.Equals(User.UserId)).ToListAsync();

            foreach (var judge in AsJudge)
            {
                _context.Judge.Remove(judge);
                await _context.SaveChangesAsync();
            }

            //4. All Team Leader postion
            IList <Team> allTeams = await _context.Team.Where(t => t.UserID.Equals(User.UserId)).ToListAsync();

            //The code inside the foreach loop is a refrence from the deleteTeamByAdmin Page
            foreach (var team in allTeams)
            {
                Team Team = await _context.Team.FindAsync(id);

                if (Team == null)
                {
                    return(RedirectToPage("Privacy"));
                }

                //Now that we are removing the team from the application database
                //we need to remove the traces of it as well otherwise they are likely to mess up with the
                //user experience and data management

                //1. We remove all the members of this team
                IList <Member> allMembers = await _context.Member.Where(m => m.TeamID.Equals(Team.TeamId)).ToListAsync();

                foreach (var mem in allMembers)
                {
                    _context.Member.Remove(mem);
                    await _context.SaveChangesAsync();
                }

                //2.We need to get rid of all the votes for this team
                IList <Vote> allVotes = await _context.Vote.Where(v => v.TeamID.Equals(Team.TeamId)).ToListAsync();

                foreach (var vote in allVotes)
                {
                    _context.Vote.Remove(vote);
                    await _context.SaveChangesAsync();
                }

                //3. We need to get rid of the presentation
                TeamPresentation teamPresentation = await _context.TeamPresentation.FirstOrDefaultAsync(tp => tp.TeamID.Equals(Team.TeamId));

                if (teamPresentation != null)
                {
                    //Before we remove from the table
                    //We need to remove it from the Files folder

                    string storedFile = teamPresentation.FileName;
                    var    oldPath    = Path.Combine(Directory.GetCurrentDirectory(), "Files", storedFile);
                    if (System.IO.File.Exists(oldPath))
                    {
                        System.IO.File.Delete(oldPath);
                        _context.TeamPresentation.Remove(teamPresentation);
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        _context.TeamPresentation.Remove(teamPresentation);
                        await _context.SaveChangesAsync();
                    }
                }

                //Now we just remove the team as a final step

                _context.Team.Remove(Team);
                await _context.SaveChangesAsync();
            }

            //Now in the end we are just gonna remove the users

            _context.User.Remove(User);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./AllUsers"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            string username = HttpContext.Session.GetString("username");
            User   user     = await _context.User.FirstOrDefaultAsync(u => u.UserName.Equals(username));

            AppCondition app = await _context.AppCondition.FirstOrDefaultAsync(app => app.AppConditionId.Equals(1));

            Team = await _context.Team.FirstOrDefaultAsync(t => t.UserID.Equals(user.UserId) && t.EventID.Equals(app.EventID));

            //The team won't be null because of test performed earlier
            if ((UploadFile == null) || (UploadFile.Length == 0))
            {
                Message = "Please select a file first to upload!!";
                return(Page());
            }

            //Before we store the file we need to get rid of the previous file if any
            TeamPresentation teamPresentation1 = await _context.TeamPresentation.FirstOrDefaultAsync(tp => tp.TeamID.Equals(Team.TeamId) && tp.EventID.Equals(app.EventID));

            if (teamPresentation1 != null)
            {
                //That means team leader has already upload a file before this
                //So we need to start by deleting that file first
                string storedFile = teamPresentation1.FileName;
                var    oldPath    = Path.Combine(Directory.GetCurrentDirectory(), "Files", storedFile);
                if (System.IO.File.Exists(oldPath))
                {
                    System.IO.File.Delete(oldPath);
                    _context.TeamPresentation.Remove(teamPresentation1);
                    await _context.SaveChangesAsync();

                    Message = "Your previous file has been deleted!!";
                }
                else
                {
                }
            }

            string extension = Path.GetExtension(UploadFile.FileName);
            //Since team name is unique for an event and username is unique always
            string fileName = Team.JoinCode + username + Team.TeamId + extension;

            var path = Path.Combine(
                Directory.GetCurrentDirectory(), "Files", fileName);

            using (var stream = new FileStream(path, FileMode.Create))
            {
                await UploadFile.CopyToAsync(stream);
            }

            Message = "You file has been uploaded!!";

            TeamPresentation teamPresentation = new TeamPresentation()
            {
                TeamID   = Team.TeamId,
                EventID  = Team.EventID,
                FileName = fileName,
                TeamName = Team.TeamName,
            };

            _context.TeamPresentation.Add(teamPresentation);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./MyTeam"));
        }