/// <summary> /// Accepts the given invite to join a team. The player invited to join /// the team is linked as a player of that team. The entry in the invites /// table is then deleted. /// </summary> /// <param name="inviteID">The ID of the invite which was accepted.</param> /// <returns>The result of the accept.</returns> public ActionResult AJAX_AcceptInvite(long inviteID) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { DBAccessor dba = new DBAccessor(); // Get the invite from the database Invitation invite = dba.GetInvite(inviteID); // Get the current user's ID from the database long userID = dba.GetPersonID(User.Identity.Name); // Ensure the get invite call worked if (invite == null) { result = "Error finding the invite in the database."; } else { if (invite.invitee.Equals(User.Identity.Name)) { // Link the player to the team if (dba.AddPlayerToTeam(userID, invite.team.ID)) { // Remove the invite entry from the database if (dba.RemoveInvite(inviteID)) { result = "You've been added to " + invite.team.name + " successfully."; // Indicate the accept went through but the request wasn't removed } else { result = "You've been added to " + invite.team.name + " but the invite wasn't removed."; } // If the link failed set an appropriate message } else { result = "An error occured adding you to " + invite.team.name + "."; } } else { result = "Invalid attempt to accept an invite."; LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.ACCEPT_INVITE, LogAction.NA); entry.User = new Person("NA", "NA", User.Identity.Name, "NA"); entry.Message = "Attempt to accept invite (ID " + invite.ID + ")."; dba.LogMessage(entry); } } } // Return the success message of the accept return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Removes the authenticated user from the team specified by the given ID. /// </summary> /// <param name="teamID">ID of the team to remove the player from.</param> /// <returns>Success message of the removal.</returns> public ActionResult AJAX_LeaveTeam(long teamID) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { // Get the person id for the user currently logged in DBAccessor dba = new DBAccessor(); long userID = dba.GetPersonID(User.Identity.Name); // Remove the player from the team if (dba.RemovePlayerFromTeam(userID, teamID)) { result = "Succesfully removed player from the team."; } else { result = "Failure to remove player from team."; } } // Return the success message of the removal return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Accepts the given request to join a team. The player that requested to /// join the team is linked as a player of that team. The entry in the /// request table is then deleted. /// </summary> /// <param name="requestID">The ID of the request which was accepted.</param> /// <returns>The result of the accept.</returns> public ActionResult AJAX_AcceptRequest(long requestID) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { DBAccessor dba = new DBAccessor(); // Get the player and team IDs from the database Request request = dba.GetRequest(requestID, RequestType.JOIN_TEAM); // Ensure the get request call worked if (request == null) { result = "Error finding the request in the database."; } else { Person user = new Person(); user.email = User.Identity.Name; if (request.team.coaches.Contains(user, new PersonComparer())) { // Link the player to the team if (dba.AddPlayerToTeam(request.requestee.ID, request.team.ID)) { // Remove the request entry from the database long requesteeID = dba.GetPersonID(User.Identity.Name); if (dba.RemoveRequest(requestID)) { result = request.requestee.firstName + " " + request.requestee.lastName + " added to " + request.team.name + " successfully."; // Indicate the accept went through but the request wasn't removed } else { result = request.requestee.firstName + " " + request.requestee.lastName + " added to " + request.team.name + " but the request wasn't removed."; } // If the link failed set an appropriate message } else { result = "Error adding " + request.requestee.firstName + " " + request.requestee.lastName + " to " + request.team.name; } } else { result = "Invalid attempt to accept request."; String message = "Attempt to accept request from " + request.requestee.firstName + " " + request.requestee.lastName + " (ID " + request.requestee.ID + ") "; message += "to join " + request.team.name + " (ID " + request.team.ID + ")."; LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.ACCEPT_REQUEST_JOIN, LogAction.NA); entry.User = user; entry.Message = message; dba.LogMessage(entry); } } } // Return the success message of the accept return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Adds a team to the database with the given name and the current logged /// in user as the coach. /// </summary> /// <returns>Success of the addition.</returns> public ActionResult AJAX_AddTeam(string teamName) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { if (teamName != null && !teamName.Equals("")) { // Get the person id for the user currently logged in DBAccessor dba = new DBAccessor(); long coachID = dba.GetPersonID(User.Identity.Name); // Add the team to the database result = "Error adding the team."; Team newTeam = new Team(); newTeam.name = teamName; if (dba.AddNewTeam(newTeam, coachID)) { result = teamName + " successfully added."; } } else { result = "Please enter a name for the new team."; } } // Return the success message of the addition return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }
/// <summary> /// Adds a request entry to the database. /// </summary> /// <param name="teamID">The ID of the team being requested to join.</param> /// <returns>Success message of the request.</returns> public ActionResult AJAX_AddRequest(long teamID) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { // Get the person id for the user currently logged in DBAccessor dba = new DBAccessor(); long requesteeID = dba.GetPersonID(User.Identity.Name); // Get any previous requests to join the team List<Request> requests = dba.GetRequests(teamID, requesteeID); if (requests.Any()) { result = "A request has already been sent."; } else { // Add the request to the database result = "Error making the request."; if (dba.AddNewRequest(requesteeID, teamID)) { result = "Request sent."; } } } // Return the success message of the addition return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }