/// <summary> /// Creates a view-model for the given enrollment /// </summary> /// <param name="enrollment">Model containing the details for the view-model</param> public EnrollmentViewModel(Enrollment enrollment) { if(enrollment != null) { Id = enrollment.Id; Pending = enrollment.Pending; Class = new ClassViewModel(enrollment.Class); Student = new UserViewModel(enrollment.Student); Grade = -1; } }
/// <summary> /// Enrolls a new student into the class /// </summary> /// <param name="class">Class to enroll the student in</param> /// <param name="student">Student to enroll in a class</param> /// <returns>Null if the an enrollment has already been created. Otherwise, the /// enrollment that was just created.</returns> public async Task<Enrollment> RequestEnrollment(Class @class, ApplicationUser student) { var status = await (from enrollment in _db.Enrollments where enrollment.Student.Id == student.Id && enrollment.Class.Id == @class.Id select enrollment).FirstOrDefaultAsync(); if(status != null) { return null; } // Create the enrollment as pending Enrollment newEnroll = new Enrollment { Class = @class, Student = student, Pending = true }; _db.Enrollments.Add(newEnroll); await _db.SaveChangesAsync(); return newEnroll; }