public ActionResult UnusedCoursesReport(SearchQueueResultViewModel viewModel) { var courseSearchQueue = courseSearchQueueBll.Get(viewModel.CourseSearchQueueId); courseSearchQueue.DeleteAllRequested = true; courseSearchQueueBll.Update(courseSearchQueue); var unusedCourses = unusedCourseBll.GetAll() .Where(x => x.CourseSearchQueueId == viewModel.CourseSearchQueueId).ToList(); unusedCourseBll.UpdateStatusRange(unusedCourses, CourseStatus.PendingDeletion); viewModel.UnusedCourses = unusedCourses; return(View()); }
static void ProcessCoursesPendingDeletion(IUnusedCourseBLL unusedCourseBll, ICourseSearchQueueBLL courseSearchQueueBll, ISendEmailBLL sendEmailBll) { DateTime deleteStartTimeStamp = DateTime.Now; var coursesPendingDeletion = unusedCourseBll.GetAll().Where(x => x.Status == CourseStatus.PendingDeletion).ToList(); if (coursesPendingDeletion.Any()) { var coursesByAccount = coursesPendingDeletion.GroupBy(x => x.AccountId); var coursesDeleted = 0; var errors = new List <string>(); foreach (var courseList in coursesByAccount) { var courseChunks = courseList.ToList().ChunkBy(MAXCOURSESTHATCANBEMODIFIED); foreach (var courseChunk in courseChunks) { var courseIds = courseChunk.Select(x => x.CourseId); var form = new MultipartFormDataContent(); foreach (var courseId in courseIds) { form.Add(new StringContent(courseId), "course_ids[]"); } form.Add(new StringContent("delete"), "event"); var result = client.PutAsync($"accounts/{courseList.Key}/courses", form).GetAwaiter().GetResult(); if (result.IsSuccessStatusCode) { coursesDeleted += courseChunk.Count; foreach (var unusedCourse in courseChunk) { unusedCourse.Status = CourseStatus.Deleted; } unusedCourseBll.UpdateRange(courseChunk); } else { string error = $"{(int)result.StatusCode}: {result.ReasonPhrase}. {result.RequestMessage.RequestUri}"; if (!errors.Contains(error)) { errors.Add(error); } } } } var deleteEndTimeStamp = DateTime.Now; var courseSearchQueueIds = coursesPendingDeletion.Select(x => x.CourseSearchQueueId).Distinct().ToList(); foreach (var courseSearchQueueId in courseSearchQueueIds) { // Send email if DELETE ALL was requested var courseSearchQueue = courseSearchQueueBll.Get(courseSearchQueueId); if (courseSearchQueue.DeleteAllRequested) { sendEmailBll.SendBatchDeleteCoursesCompletedEmailAsync(deleteStartTimeStamp, deleteEndTimeStamp, coursesDeleted, errors, courseSearchQueue.SubmittedByEmail).GetAwaiter().GetResult(); courseSearchQueue.DeleteAllRequested = false; courseSearchQueueBll.Update(courseSearchQueue); } } } }