public async Task <IActionResult> DeleteOffering(int id) { var offering = await repository.GetOffering(id, includeRelated : false); if (offering == null) { return(NotFound()); } repository.Remove(offering); await unitOfWork.CompleteAsync(); return(Ok(id)); }
public void Remove(Offering offering, bool forceToRemove) { if (offering.Enrollments.Count > 0 && !offering.Term.IsCurrentTerm) { throw new ForeignKeyEntityException("The offering could not be removed because it has students enrolled in it, and it was offered in another term."); } if (offering.Enrollments.Count > 0 && offering.Term.IsCurrentTerm && !forceToRemove) { throw new ForeignKeyEntityException("The offering could not be removed because it has students enrolled in it, and it was offered in another term."); } // Send an email to students currently enrolled in the offering that this will be removed var emailMessage = new EmailMesageService(); foreach (var enrollment in offering.Enrollments) { var student = StudentService.Get(enrollment.StudentId); string subject = "Offering removed!!!"; string body = $"{student.LastName}, {student.FirstName},\n" + $"{offering.Title} is not longer available.\n" + "You might want to enroll in a new one if you wish." + "Sorry for any incoveninence that this causes you."; emailMessage.Sent(student.Email, subject, body); } // Remove enrollment and evaluations EnrollmentService.RemoveRange(offering.Enrollments); // Remove schedules for the offering ScheduleService.RemoveRange(offering.Schedules); // Remove offering _offeringRepository.Remove(offering); }