/// <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 invite matching the given invite ID from the database. /// </summary> /// <param name="inviteID">The ID of the invite to remove.</param> /// <returns>Success message of the invite removal.</returns> public ActionResult AJAX_RemoveInvite(long inviteID) { // Make sure the user is authenticated string result = "Request not authenticated."; if (Request.IsAuthenticated) { DBAccessor dba = new DBAccessor(); Invitation invite = dba.GetInvite(inviteID); if (invite.invitor.email.Equals(User.Identity.Name) || invite.invitee == User.Identity.Name) { // Remove the request to the database result = "Error making the request."; if (dba.RemoveInvite(inviteID)) { result = "Invitation removed."; } } else { result = "Invalid request to remove invite."; LogEntry entry = new LogEntry(LogType.INVALID_REQUEST, LogFunction.REMOVE_INVITE, LogAction.NA); entry.User = new Person("NA", "NA", User.Identity.Name, "NA"); entry.Message = "Attempt to remove invite (ID " + invite.ID + ")."; dba.LogMessage(entry); } } // Return the success message of the removal return Json( new { message = result }, JsonRequestBehavior.AllowGet ); }