/// <summary>
        /// Given a Course.Id and a student's username, enroll the student in that course.  If
        /// a student record doesn't exist (e.g. the student was never enrolled in a course),
        /// a record is created internally and the course is added to the StudentRecord.StudentCourseEnrollments.
        /// If the student already has a record, we udpate the student's enrollments with the new course
        /// enrollment.
        /// </summary>
        /// <param name="username">The username is how we uniquely track/identify a StudentRecord</param>
        /// <param name="courseId">The Course.Id (unique id of the course)</param>
        /// <exception cref="System.InvalidOperationException">
        ///     <para>
        ///     1) If the given course (by Course.Id) does not exist in the system
        ///     2) If the student is already enrolled in that course
        ///     </para>
        /// </exception>
        /// <returns>A copy of the persisted/updated StudentRecord</returns>
        public StudentRecord AddEnrollment(string username, string courseId)
        {
            IRepository<Course, string> coursesRepo = new BizCollegeRepository<Course, string>();
            var course = coursesRepo.Get(courseId);
            if (course == null)
                throw new InvalidOperationException("That course does not exist in the system");

            // Try and retrive the student's record (by username).  If they don't
            // have an existing record, create a record for them
            var student = m_enrollmentsRepo.Get(username);
            if (student == null)
            {
                student = new StudentRecord() { Username = username, StudentCourseEnrollments = new List<Enrollment>() };
            }

            // Throw an error if the student is already enrolled in this course
            foreach (var currentEnrollment in student.StudentCourseEnrollments)
            {
                if (currentEnrollment.CourseId == courseId)
                {
                    throw new InvalidOperationException("The student is already enrolled in course:  " + courseId);
                }
            }

            // Add the new enrollment and save it the updated student record
            student.StudentCourseEnrollments.Add(new Enrollment()
            {
                CourseId = courseId,
                DateStarted = DateTime.Now,
                DateCompleted = SqlServerHelper.GetSqlServerMinimumDateTimeValue()
            });

            return m_enrollmentsRepo.AddOrUpdate(student);
        }
        public void CanGetStudentEnrollment()
        {
            // dummy username and course id
            string username = "******";
            string courseId = Guid.NewGuid().ToString();

            // Add the dummy student enrollment via the internal repository interfaces
            var dummyEnrollment = new StudentRecord(){ Username = username};
            dummyEnrollment.StudentCourseEnrollments = new List<Enrollment>();
            dummyEnrollment.StudentCourseEnrollments.Add(
                new Enrollment()
                {
                    CourseId = courseId,
                    DateStarted = DateTime.Now,
                    DateCompleted = SqlServerHelper.GetSqlServerMinimumDateTimeValue()
                }
            );
            var enrollmentsRepo = new BizCollegeRepository<StudentRecord, string>();
            dummyEnrollment = enrollmentsRepo.AddOrUpdate(dummyEnrollment);

            // Get the student record via the enrollments model interface that the web
            // application will use to retrieve student enrollment records by username
            IStudentEnrollmentsModel enrollmentsModel = new StudentEnrollmentsModel();
            var fromDb = enrollmentsModel.GetStudentRecord(username);

            Assert.NotNull(fromDb);
            Assert.AreEqual(fromDb.Username, dummyEnrollment.Username);
            Assert.NotNull(fromDb.StudentCourseEnrollments);
            Assert.AreEqual(fromDb.StudentCourseEnrollments.Count, dummyEnrollment.StudentCourseEnrollments.Count);
            Assert.AreEqual(fromDb.StudentCourseEnrollments[0].Id, dummyEnrollment.StudentCourseEnrollments[0].Id);
            Assert.AreEqual(fromDb.StudentCourseEnrollments[0].CourseId, dummyEnrollment.StudentCourseEnrollments[0].CourseId);
            Assert.AreEqual(fromDb.StudentCourseEnrollments[0].DateCompleted, dummyEnrollment.StudentCourseEnrollments[0].DateCompleted);

            // Clean up Db
            enrollmentsRepo.Remove(username);
        }
        public void CanRemoveStudentEnrollment()
        {
            // dummy username and course id
            string username = "******";
            string courseId = Guid.NewGuid().ToString();

            // Add the dummy student enrollment via the internal repository interfaces
            var dummyEnrollment = new StudentRecord() { Username = username };
            dummyEnrollment.StudentCourseEnrollments = new List<Enrollment>();
            dummyEnrollment.StudentCourseEnrollments.Add(
                new Enrollment()
                {
                    CourseId = courseId,
                    DateStarted = DateTime.Now,
                    DateCompleted = SqlServerHelper.GetSqlServerMinimumDateTimeValue()
                }
            );
            var enrollmentsRepo = new BizCollegeRepository<StudentRecord, string>();
            dummyEnrollment = enrollmentsRepo.AddOrUpdate(dummyEnrollment);

            // Remove the enrollment via the enrollmnent model interface that the webapp
            // will use to remove a student's enrollment from their record
            IStudentEnrollmentsModel enrollmentsModel = new StudentEnrollmentsModel();
            enrollmentsModel.RemoveEnrollment(username, courseId);

            // retrieve the enrollment record from the Db and ensure the student has
            // no enrollments in his/her student record
            var fromDb = enrollmentsRepo.Get(username);

            Assert.NotNull(fromDb);
            Assert.AreEqual(fromDb.Username, username);
            Assert.AreEqual(fromDb.StudentCourseEnrollments.Count, 0);

            // clean up Db
            enrollmentsRepo.Remove(fromDb.Username);
        }
        public void CanUpdateExistingStudentEnrollmentsCollection()
        {
            // dummy username and dummy courses
            string username = "******";
            var dummyCourse1 = DummyDataGenerator.CreateDummyCourse();
            var dummyCourse2 = DummyDataGenerator.CreateDummyCourse();

            // Add the dummy courses to the database via the internal course repository interface
            IRepository<Course, string> coursesRepo = new BizCollegeRepository<Course, string>();
            dummyCourse1.Id = coursesRepo.AddOrUpdate(dummyCourse1).Id;
            dummyCourse2.Id = coursesRepo.AddOrUpdate(dummyCourse2).Id;

            // Add the dummy student enrollment via the internal enrollments repository interfaces
            var dummyEnrollment = new StudentRecord() { Username = username };
            dummyEnrollment.StudentCourseEnrollments = new List<Enrollment>();
            dummyEnrollment.StudentCourseEnrollments.Add(
                new Enrollment()
                {
                    CourseId = dummyCourse1.Id,
                    DateStarted = DateTime.Now,
                    DateCompleted = SqlServerHelper.GetSqlServerMinimumDateTimeValue()
                }
            );

            var enrollmentsRepo = new BizCollegeRepository<StudentRecord, string>();
            dummyEnrollment = enrollmentsRepo.AddOrUpdate(dummyEnrollment);

            // To update the student enrollment, we'll add another enrollment to their
            // current enrollment record (so we should have two enrollments after the update)
            IStudentEnrollmentsModel model = new StudentEnrollmentsModel();
            var updatedRecord = model.AddEnrollment(username, dummyCourse2.Id);

            Assert.NotNull(updatedRecord);
            Assert.IsNotNullOrEmpty(updatedRecord.Username);
            Assert.AreEqual(updatedRecord.Username, username);

            Assert.NotNull(updatedRecord);
            Assert.IsNotNullOrEmpty(updatedRecord.Username);
            Assert.NotNull(updatedRecord.StudentCourseEnrollments);
            Assert.AreEqual(updatedRecord.StudentCourseEnrollments.Count, 2);

            var updatedEnrollments = new List<Enrollment>(updatedRecord.StudentCourseEnrollments);
            Assert.AreEqual(updatedEnrollments[0].CourseId, dummyCourse1.Id);
            Assert.AreEqual(updatedEnrollments[1].CourseId, dummyCourse2.Id);

            // Clean up Db
            enrollmentsRepo.Remove(username);
            coursesRepo.Remove(dummyCourse1.Id);
            coursesRepo.Remove(dummyCourse2.Id);
        }