/// <summary> /// Removes a student from a class /// </summary> /// <param name="classId">The id of the class to remove the student from</param> /// <param name="studentId">The id of the student to unenroll</param> /// <returns>The deleted enrollment</returns> public Enrollment UnEnrollStudent(int classId, int studentId) { var enrollment = _db.Enrollments .Include(e => e.Class) .FirstOrDefault(e => e.Class.Id == classId && e.Student.Id == studentId); if (enrollment == null) { throw new InvalidOperationException("Error unenrolling student"); } // Delete the Person object from the PersonGroup var faceClient = RecognitionService.GetFaceClient(); Task.Run(() => faceClient.DeletePersonAsync(classId.ToString(), enrollment.PersonId)); // Remove all attendance and enrollment information var attendance = _db.Attendance.Where(a => a.Student.Id == studentId); _db.Attendance.RemoveRange(attendance); enrollment.Class.TrainingStatus = TrainingStatus.UnTrained; // Update training status for class _db.Enrollments.Remove(enrollment); _db.SaveChanges(); return(enrollment); }
/// <summary> /// Deletes a class with the given id and all other related information /// </summary> /// <param name="classId">Id of the class to delete</param> public Class DeleteClass(int classId) { var @class = _db.Classes.Find(classId); if (@class == null) { throw new InvalidOperationException("Error deleting class."); } // Delete all lectures var lectureManager = new LectureManager(_db); var lectures = new List <Lecture>(@class.Lectures); foreach (var lecture in lectures) { lectureManager.Delete(lecture.Id); } // Delete all enrollments _db.Enrollments.RemoveRange(@class.Enrollment); // Delete cognitive data var faceClient = RecognitionService.GetFaceClient(); Task.Run(() => faceClient.DeletePersonGroupAsync(@class.Id.ToString())).Wait(); _db.Classes.Remove(@class); _db.SaveChanges(); return(@class); }
/// <summary> /// Enrolls a student into a class /// </summary> /// <param name="classId">The id of the class to enroll the student into</param> /// <param name="studentId">The id of the student to enroll</param> /// <returns>The enrollment created</returns> public Enrollment EnrollStudent(int classId, int studentId) { var existing = _db.Enrollments .FirstOrDefault(e => e.Class.Id == classId && e.Student.Id == studentId); var @class = _db.Classes.Find(classId); var student = _db.Students.Find(studentId); // Already enrolled if (existing != null) { throw new InvalidOperationException("Error enrolling student"); } var enrollment = new Enrollment { Class = @class, EnrollDate = DateTime.Now, Student = student }; _db.Enrollments.Add(enrollment); // Add the faces if (!student.Profile.Images.Any()) { throw new InvalidOperationException("Profile must be added before enrollment."); } // Create the Person for this student var faceClient = RecognitionService.GetFaceClient(); var person = Task.Run(() => faceClient.CreatePersonAsync(classId.ToString(), student.FirstName)).Result; foreach (var image in student.Profile.Images) { Task.Run(() => faceClient.AddPersonFaceAsync(@class.Id.ToString(), person.PersonId, image.Url)).Wait(); } // Update training status for class enrollment.PersonId = person.PersonId; enrollment.Class.TrainingStatus = TrainingStatus.UnTrained; _db.SaveChanges(); return(enrollment); }
/// <summary> /// Creates a new class /// </summary> /// <param name="name">The name/title of the class</param> /// <param name="number">The course number</param> /// <param name="section">The section</param> /// <param name="semester">The semester e.g. Fall 2016</param> /// <param name="teacher">The teacher for this class</param> /// <returns>The created class</returns> /// <remarks>Throws InvalidOperationException if class already exists with the given details</remarks> public Class CreateClass(string name, int number, string section, Semester semester, int year, int teacherId) { var existing = _db.Classes .Count(c => c.Name == name && c.Number == number && c.Section == section && c.Semester == semester && c.Teacher.Id == teacherId); var teacher = _db.Teachers.Find(teacherId); if (existing > 0 || teacher == null) { throw new InvalidOperationException("Error creating class"); } var newClass = new Class { Name = name, Number = number, Section = section, Semester = semester, Year = year, TrainingStatus = TrainingStatus.UnTrained, Teacher = teacher }; var added = _db.Classes.Add(newClass); _db.SaveChanges(); // Create the PersonGroup for this class var faceClient = RecognitionService.GetFaceClient(); Task.Run(() => faceClient.CreatePersonGroupAsync(added.Id.ToString(), added.Name)).Wait(); return(added); }