public void Withdraw(Course course) { if (null == course) { throw new ArgumentNullException(nameof(course)); } var oldCourses = _courses.Where(c => c.CourseId == course.Id).ToArray(); var isEmpty = !oldCourses.Any(); var isCompleted = !isEmpty && oldCourses.OrderByDescending(c => c.CreatedAt).First().Status == StudentCourseStatus.Statuses.Completed; if (isCompleted) { throw new ArgumentException($"student {this.Id} has completed course {course.Id} already"); } var isEnrolled = !isEmpty && oldCourses.OrderByDescending(c => c.CreatedAt).First().Status == StudentCourseStatus.Statuses.Enrolled; if (!isEnrolled) { throw new ArgumentException($"student {this.Id} not enrolled in course {course.Id}"); } _courses.Add(StudentCourseStatus.Withdrawn(this, course)); this.AddEvent(new StudentWithdrawn(this, course)); }
public void Enroll(Course course) { if (null == course) { throw new ArgumentNullException(nameof(course)); } var oldCourses = _courses.Where(c => c.CourseId == course.Id).ToArray(); var isEmpty = !oldCourses.Any(); var hasWithdrawn = !isEmpty && oldCourses.OrderByDescending(c => c.CreatedAt).First().Status == StudentCourseStatus.Statuses.Withdrawn; if (isEmpty || hasWithdrawn) { _courses.Add(StudentCourseStatus.Enrolled(this, course)); this.AddEvent(new StudentEnrolled(this, course)); } }
public void Complete(Course course) { if (null == course) { throw new ArgumentNullException(nameof(course)); } var oldCourses = _courses.Where(c => c.CourseId == course.Id).ToArray(); if (!oldCourses.Any()) { throw new ArgumentException($"student {this.Id} not enrolled in course {course.Id}"); } if (oldCourses.Any(c => c.Status == StudentCourseStatus.Statuses.Withdrawn)) { throw new ArgumentException($"student {this.Id} has withdrawn from course {course.Id}"); } _courses.Add(StudentCourseStatus.Completed(this, course)); this.AddEvent(new CourseCompleted(this, course)); }