public void CanAddNewCourse() { var dummyCourse = DummyDataGenerator.CreateDummyCourse(); // Course Id's are autogenerated by the database, so when we // add a new one to the db, we cache the assigned Id after the // add operation has completed (that way we can retrieve the course // from the db to run our assertions on the data ICoursesModel model = new CoursesModel(); dummyCourse.Id = model.AddOrUpdateCourse(dummyCourse).Id; // Retrieve the newly added course back out from the Db var repo = new BizCollegeRepository<Course, string>(); var fromDb = repo.Get(dummyCourse.Id); // Check the values against our dummy course Assert.NotNull(fromDb); Assert.AreEqual(dummyCourse.Id, fromDb.Id); Assert.AreEqual(dummyCourse.Name, fromDb.Name); Assert.AreEqual(dummyCourse.Description, fromDb.Description); Assert.AreEqual(dummyCourse.CreatedByUsername, fromDb.CreatedByUsername); Assert.AreEqual(dummyCourse.LastUpdateByUsername, fromDb.LastUpdateByUsername); Assert.AreEqual(dummyCourse.CourseSlides.Count, fromDb.CourseSlides.Count); // clean-up db repo.Remove(fromDb.Id); }
/// <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 CanUpdateExistingCourse() { // Create a dummy course and add it to the database var dummyCourse = DummyDataGenerator.CreateDummyCourse(); ICoursesModel model = new CoursesModel(); dummyCourse.Id = model.AddOrUpdateCourse(dummyCourse).Id; // Update the course by adding a course slide and changing dummyCourse.CourseSlides.Add(new CourseContent() { CourseContentType = ContentType.Video, Description = "This video talks about how to start your own business", ResourcePath = "http://www.youtube.com", Title = "Business Course Introduction Video", IndexInSquence = 0, }); // Also update the other properties of the coures dummyCourse.Name = "Business Savvy 101"; dummyCourse.Description = "Tips on how to run a business properly"; dummyCourse.State = CourseState.Active; dummyCourse.LastUpdateByUsername = "******"; dummyCourse.CreatedByUsername = "******"; // Persist the course udpates to the database model.AddOrUpdateCourse(dummyCourse); // Retrieve a copy of the updated course back out from the Db var repo = new BizCollegeRepository<Course, string>(); var fromDb = repo.Get(dummyCourse.Id); // Check that the course properties we updated were persisted Assert.NotNull(fromDb); Assert.AreEqual(dummyCourse.Id, fromDb.Id); Assert.AreEqual(dummyCourse.Name, fromDb.Name); Assert.AreEqual(dummyCourse.Description, fromDb.Description); Assert.AreEqual(dummyCourse.State, fromDb.State); Assert.AreEqual(dummyCourse.LastUpdateByUsername, fromDb.LastUpdateByUsername); Assert.AreEqual(dummyCourse.CreatedByUsername, fromDb.CreatedByUsername); Assert.NotNull(fromDb.CourseSlides[0]); Assert.AreEqual(dummyCourse.CourseSlides.Count, fromDb.CourseSlides.Count); Assert.AreEqual(dummyCourse.CourseSlides[0].CourseContentType, fromDb.CourseSlides[0].CourseContentType); Assert.AreEqual(dummyCourse.CourseSlides[0].Description, fromDb.CourseSlides[0].Description); Assert.AreEqual(dummyCourse.CourseSlides[0].ResourcePath, fromDb.CourseSlides[0].ResourcePath); Assert.AreEqual(dummyCourse.CourseSlides[0].Title, fromDb.CourseSlides[0].Title); Assert.AreEqual(dummyCourse.CourseSlides[0].IndexInSquence, fromDb.CourseSlides[0].IndexInSquence); // clean-up db repo.Remove(fromDb.Id); }
public void CanRemoveExistingCourse() { var dummyCourse = DummyDataGenerator.CreateDummyCourse(); ICoursesModel model = new CoursesModel(); dummyCourse.Id = model.AddOrUpdateCourse(dummyCourse).Id; // Remove the dummyCourse model.RemoveCourse(dummyCourse.Id); // Check that the course was removed from the Db var repo = new BizCollegeRepository<Course, string>(); var fromDb = repo.Get(dummyCourse.Id); Assert.IsNull(fromDb); }
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 CanSetStudentEnrollmentCourseCompletion() { string username = "******"; // Create a dummy course and add it to the database var dummyCourse = DummyDataGenerator.CreateDummyCourse(); ICoursesModel courseModel = new CoursesModel(); dummyCourse.Id = courseModel.AddOrUpdateCourse(dummyCourse).Id; // add the new enrollment for the given user and specified course IStudentEnrollmentsModel model = new StudentEnrollmentsModel(); StudentRecord studentEnrollmentRecord = model.AddEnrollment(username, dummyCourse.Id); // set the course as completed model.SetStudentCourseCompletion(username, dummyCourse.Id); // retrieve the record from the database and make sure that course // was set to complete. var enrollmentsRepo = new BizCollegeRepository<StudentRecord, string>(); var fromDb = enrollmentsRepo.Get(studentEnrollmentRecord.Username); Assert.NotNull(fromDb); Assert.IsTrue(fromDb.StudentCourseEnrollments[0].WasCourseCompleted); // clean up Db: remove student record enrollmentsRepo.Remove(studentEnrollmentRecord.Username); // clean up Db: remove dummy course var coursesRepo = new BizCollegeRepository<Course, string>(); coursesRepo.Remove(dummyCourse.Id); }