Esempio n. 1
0
        /// <summary>
        /// Searches the database for teams matching the given search term.
        /// </summary>
        /// <param name="teamName">Team name or partial name to search for.</param>
        /// <returns>A list of matching teams.</returns>
        public ActionResult AJAX_SearchTeams(string teamName)
        {
            // Make sure the user is authenticated
            string result = "Request not authenticated.";
            if (Request.IsAuthenticated) {

                // Make sure a value was bound for team name
                if (teamName != null && !teamName.Equals("")) {

                    // Search the DB using the DBA
                    DBAccessor dba = new DBAccessor();
                    List<Team> matchedTeams = dba.SearchTeams(teamName);

                    if (matchedTeams != null) {
                        if (matchedTeams.Count >= 1) {
                            // Form an html table with the results
                            result = "<h4>Team Matches</h4>\n";
                            result += "<table>\n";
                            result += "<tr><th>Team Name</th><th>Coach's Name</th><th>Request to Join</th></tr>\n";
                            foreach (Team team in matchedTeams) {
                                // Get the players of the team
                                List<Person> teamMembers = dba.GetTeamPlayers(team.ID);
                                Person user = new Person();
                                user.email = User.Identity.Name;

                                string coachName = team.CoachesDisplay;
                                string joinCell = "<div id='RQ_" + team.ID + "' class='clickable-text' onClick='action_requestjoin(" + team.ID + ")'>Send Request</div>";
                                if (team.coaches.Contains(user, new PersonComparer())) {
                                    coachName = "You";
                                    joinCell = "N/A";
                                }
                                else if (teamMembers.Contains(user, new PersonComparer())) {
                                    joinCell = "You're on the Team";
                                }
                                result += "<tr><td>" + team.name + "</td><td>" + coachName + "</td><td>" + joinCell + "</td></tr>\n";
                            }
                            result += "</table>\n";
                        }
                        else {
                            result = "No matches were found.";
                        }
                    }
                    else {
                        result = "The query to the database failed.";
                    }
                }
                else {
                    result = "Please enter a name or part of a name to search.";
                }
            }

            // Return the success message of the addition
            return Json(
                new { message = result },
                JsonRequestBehavior.AllowGet
            );
        }