public async Task DeleteApplication(string applicationId, string userId) { _logger.LogInformation($"Executing DeleteApplication with applicationId={applicationId}. (UserID: {userId})"); var application = await _context.Applications.SingleOrDefaultAsync(x => x.Id == applicationId); if (application == null) { _logger.LogError($"Application with ID:{applicationId} not found. (UserID: {userId})"); throw new NotFoundException(_stringLocalizer["Application with ID:{0} not found.", applicationId]); } await _cvStorageService.DeleteCvAsync(application.CvFileName); _context.Applications.Remove(application); await _context.SaveChangesAsync(); }
public async Task <IActionResult> DeleteUser(string id) { var user = await _userManager.FindByIdAsync(id); if (user != null) { var usersApplications = _context.Applications.Where(x => x.UserId == user.Id); foreach (var application in usersApplications) { var deleteResult = await _cvStorageService.DeleteCvAsync(application.CvFileName); if (!deleteResult) { throw new Exception($"Something went wrong while deleting cv in Blob: {application.CvFileName}."); } } var usersApplicationsViewHistory = _context.ApplicationsViewHistories.Where(x => x.UserId == user.Id); _context.ApplicationsViewHistories.RemoveRange(usersApplicationsViewHistory); await _context.SaveChangesAsync(); var historyCount = _context.ApplicationsViewHistories.Where(x => x.UserId == user.Id).Count(); if (historyCount != 0) { throw new Exception($"Something went wrong while deleting users application view history. UserId: {user.Id}, HistoryCount: {historyCount}"); } var applicatioApprovalResponsibilities = _context.ApplicationStagesRequirements.Where(x => x.DefaultResponsibleForApplicatioApprovalId == user.Id); foreach (var x in applicatioApprovalResponsibilities) { x.DefaultResponsibleForApplicatioApprovalId = null; } var phoneCallResponsibilities = _context.ApplicationStagesRequirements.Where(x => x.DefaultResponsibleForPhoneCallId == user.Id); foreach (var x in phoneCallResponsibilities) { x.DefaultResponsibleForApplicatioApprovalId = null; } var homeworkResponsibilities = _context.ApplicationStagesRequirements.Where(x => x.DefaultResponsibleForHomeworkId == user.Id); foreach (var x in homeworkResponsibilities) { x.DefaultResponsibleForApplicatioApprovalId = null; } var interviewResponsibilities = _context.ApplicationStagesRequirements.Where(x => x.DefaultResponsibleForInterviewId == user.Id); foreach (var x in interviewResponsibilities) { x.DefaultResponsibleForApplicatioApprovalId = null; } var interwievAppointments = _context.InterviewAppointments .Include(x => x.Interview) .Where(x => x.Interview.ResponsibleUserId == user.Id && x.Interview.State != ApplicationStageState.Finished); if (interwievAppointments != null) { _context.InterviewAppointments.RemoveRange(interwievAppointments); } var applicationStagesResponsibilities = _context.ApplicationStages.Where(x => x.ResponsibleUserId == user.Id); foreach (var x in applicationStagesResponsibilities) { x.ResponsibleUserId = null; } await _context.SaveChangesAsync(); IdentityResult result = await _userManager.DeleteAsync(user); if (result.Succeeded) { _logger.LogInformation("User deleted successfully."); return(RedirectToAction(nameof(AdminController.UserManagement))); } else { ModelState.AddModelError("", _stringLocalizer["Something went wrong while deleting this user."]); } } else { ModelState.AddModelError("", _stringLocalizer["This user can't be found."]); } return(View(nameof(AdminController.UserManagement), _userManager.Users)); }