async Task EnrollStudent(StudentEnrollCommand command) { Student student = await _students.GetStudentAsync(command.StudentEmail); if (student == null) { throw new DomainException($"Student email '{command.StudentEmail}' doens't exist"); } bool suceed; do { CourseEnrollment enrollment = await _courses.GetCourseEnrollmentAsync(command.CourseTitle); if (enrollment == null) { throw new DomainException($"Course {command.CourseTitle} doesn't exist"); } int version = enrollment.CourseVersion; enrollment.AddStudent(student); suceed = await _courses.SetCourseEnrollmentAsync(version, enrollment); } while(!suceed); }
public void AddStudent_Test() { var enrollment = new CourseEnrollment( courseId: "Course_1", courseVersion: 0, courseTitle: "Course 1", capacity: 2, students: new string[0], summary: new CourseSummary() ); var student = TestHelpers.CreateStudent(id: 1, age: 30); enrollment.AddStudent(student); enrollment.CourseId.Should().Be("Course_1"); enrollment.CourseVersion.Should().Be(1); enrollment.CourseTitle.Should().Be("Course 1"); enrollment.Capacity.Should().Be(2); enrollment.Students.Should().BeEquivalentTo(new[] { "Student_1" }); enrollment.Summary.StudentCount.Should().Be(1); }