public static async Task UpdateTeamsWhoSentResponse(PuzzleServerContext context, Response response) { using (IDbContextTransaction transaction = context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { var submissionsThatMatchResponse = await(from PuzzleStatePerTeam pspt in context.PuzzleStatePerTeam join Submission sub in context.Submissions on pspt.Team equals sub.Team where pspt.PuzzleID == response.PuzzleID && sub.SubmissionText == response.SubmittedText select new { State = pspt, Submission = sub }).ToListAsync(); if (submissionsThatMatchResponse.Count > 0) { Puzzle puzzle = await context.Puzzles.Where((p) => p.ID == response.PuzzleID).FirstOrDefaultAsync(); foreach (var s in submissionsThatMatchResponse) { s.Submission.Response = response; context.Attach(s.Submission).State = EntityState.Modified; if (response.IsSolution && s.State.SolvedTime == null) { await SetSolveStateAsync(context, puzzle.Event, puzzle, s.State.Team, s.Submission.TimeSubmitted); } } await context.SaveChangesAsync(); transaction.Commit(); } } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var thisPuzzleUser = await PuzzleUser.GetPuzzleUserForCurrentUser(_context, User, _userManager); // enforce access rights, do not let these change! Input.ID = thisPuzzleUser.ID; Input.IsGlobalAdmin = thisPuzzleUser.IsGlobalAdmin; Input.IdentityUserId = thisPuzzleUser.IdentityUserId; _context.Entry(thisPuzzleUser).State = EntityState.Detached; _context.Attach(Input).State = EntityState.Modified; await _context.SaveChangesAsync(true); await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { if (string.IsNullOrEmpty(PuzzleUser.Email)) { ModelState.AddModelError("PuzzleUser.Email", "An email is required."); } else if (!MailHelper.IsValidEmail(PuzzleUser.Email)) { ModelState.AddModelError("PuzzleUser.Email", "This email address is not valid."); } if (!ModelState.IsValid) { return(Page()); } var thisPuzzleUser = await PuzzleUser.GetPuzzleUserForCurrentUser(_context, User, _userManager); if (thisPuzzleUser != null) { _context.Entry(thisPuzzleUser).State = EntityState.Detached; } _context.Attach(PuzzleUser).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Redirect(returnUrl)); }
/// <summary> /// Deletes all duplicate submissions from the database /// </summary> public async Task <IActionResult> OnPostDeleteDuplicateSubmissionsAsync() { using (var transaction = await _context.Database.BeginTransactionAsync(IsolationLevel.Serializable)) { var duplicateSubGroups = await(from submission in _context.Submissions group submission by new { submission.PuzzleID, submission.TeamID, submission.SubmissionText } into g where g.Count() > 1 select(from sub in g select sub.ID).ToArray()).ToListAsync(); List <Submission> submissionsToDelete = new List <Submission>(); foreach (int[] dupeGroup in duplicateSubGroups) { for (int i = 0; i < dupeGroup.Length - 1; i++) { int subId = dupeGroup[i]; Submission toDelete = new Submission() { ID = subId }; _context.Attach(toDelete); submissionsToDelete.Add(toDelete); } } _context.Submissions.RemoveRange(submissionsToDelete); await _context.SaveChangesAsync(); transaction.Commit(); Status = $"{submissionsToDelete.Count} duplicates removed"; } return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { if (!ModelState.IsValid) { return(Page()); } var thisPuzzleUser = await PuzzleUser.GetPuzzleUserForCurrentUser(_context, User, _userManager); if (thisPuzzleUser != null) { _context.Entry(thisPuzzleUser).State = EntityState.Detached; } _context.Attach(PuzzleUser).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Redirect(returnUrl)); }
public static async Task UpdateTeamsWhoSentResponse(PuzzleServerContext context, Response response) { using (IDbContextTransaction transaction = context.Database.BeginTransaction(System.Data.IsolationLevel.Serializable)) { var submissionsThatMatchResponse = await(from PuzzleStatePerTeam pspt in context.PuzzleStatePerTeam join Submission sub in context.Submissions on pspt.Team equals sub.Team where pspt.PuzzleID == response.PuzzleID && sub.PuzzleID == response.PuzzleID && sub.SubmissionText == response.SubmittedText select new { State = pspt, Submission = sub }).ToListAsync(); if (submissionsThatMatchResponse.Count > 0) { Puzzle puzzle = await context.Puzzles.Where((p) => p.ID == response.PuzzleID).FirstOrDefaultAsync(); foreach (var s in submissionsThatMatchResponse) { s.Submission.Response = response; context.Attach(s.Submission).State = EntityState.Modified; if (response.IsSolution && s.State.SolvedTime == null) { await SetSolveStateAsync(context, puzzle.Event, puzzle, s.State.Team, s.Submission.TimeSubmitted); } } await context.SaveChangesAsync(); transaction.Commit(); var teamMembers = await(from TeamMembers tm in context.TeamMembers join Submission sub in context.Submissions on tm.Team equals sub.Team where sub.PuzzleID == response.PuzzleID && sub.SubmissionText == response.SubmittedText select tm.Member.Email).ToListAsync(); MailHelper.Singleton.SendPlaintextBcc(teamMembers, $"{puzzle.Event.Name}: {response.Puzzle.Name} Response updated for '{response.SubmittedText}'", $"The new response for this submission is: '{response.ResponseText}'."); } } }