public bool RemoveInstructorFromCourse(string userId, string instructorEmail, string courseName) { var result = false; try { var log = UpdateLog(userId, DatabaseOperation.edit, TableName.Course, courseName); if (!log) { throw new Exception(); } var instructor = db.Users.FirstOrDefault(x => x.Email == instructorEmail); Course course = db.Courses.FirstOrDefault(x => x.Name == courseName); ApplicationUserCourse applicationUserCourse = db.ApplicationUserCourses.FirstOrDefault(x => x.ApplicationUserId == instructor.Id); db.ApplicationUserCourses.Remove(applicationUserCourse); course.ApplicationUsers.Remove(applicationUserCourse); instructor.Courses.Remove(applicationUserCourse); db.SaveChanges(); result = true; return(result); } catch (Exception) { return(result); } }
public bool RegisterForCourse(string userId, string courseName, string studentId) { try { var log = UpdateLog(userId, DatabaseOperation.add, TableName.Course, courseName); if (!log) { throw new Exception(); } var student = db.Users.Find(studentId); var result = db.Courses.FirstOrDefault(x => x.Name == courseName); if (result == null || student == null) { throw new Exception(); } result.Capacity--; ApplicationUserCourse application = new ApplicationUserCourse(); application.CourseId = result.Id; application.Course = result; application.ApplicationUserId = student.Id; application.ApplicationUser = student; application.DateJoined = DateTime.Now; db.ApplicationUserCourses.Add(application); db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public bool AddStudentsToCourse(string userId, List <string> studentEmails, string courseName) { var result = false; try { var log = UpdateLog(userId, DatabaseOperation.add, TableName.Course, courseName); if (!log) { throw new Exception(); } var course = db.Courses.FirstOrDefault(x => x.Name == courseName); if (course == null || course.Capacity < studentEmails.Count()) { throw new Exception(); } var length = db.ApplicationUserCourses.Count(); var students = db.Users.Where(x => studentEmails.Contains(x.Email)).ToList(); foreach (var student in students) { ApplicationUserCourse applicationUserCourse = new ApplicationUserCourse(); applicationUserCourse.ApplicationUserId = student.Id; applicationUserCourse.CourseId = course.Id; applicationUserCourse.Course = course; applicationUserCourse.ApplicationUser = student; applicationUserCourse.DateJoined = DateTime.Now; course.ApplicationUsers.Add(applicationUserCourse); } db.SaveChanges(); if (length + studentEmails.Count() != db.ApplicationUserCourses.Count()) { throw new Exception(); } // if there is an error in updated database then i should delete any updated data. result = true; } catch (Exception) { result = false; } return(result); }
public bool AddStudentToACourse(string userId, string studentEmail, string courseName) { bool result = false; try { var log = UpdateLog(userId, DatabaseOperation.add, TableName.Course, courseName); if (!log) { throw new Exception(); } var student = db.Users.FirstOrDefault(x => x.Email == studentEmail); var course = db.Courses.FirstOrDefault(x => x.Name == courseName); if (student == null || course == null) { throw new Exception(); } var length = db.ApplicationUserCourses.Count(); ApplicationUserCourse applicationUserCourse = new ApplicationUserCourse(); applicationUserCourse.ApplicationUserId = student.Id; applicationUserCourse.CourseId = course.Id; applicationUserCourse.Course = course; applicationUserCourse.ApplicationUser = student; applicationUserCourse.DateJoined = DateTime.Now; course.ApplicationUsers.Add(applicationUserCourse); db.SaveChanges(); if (length + 1 == db.ApplicationUserCourses.Count()) { result = true; } return(result); } catch (Exception) { return(result); } }
public bool RemoveStudentFromCourse(string userId, string courseName, string studentEmail) { var result = false; try { var log = UpdateLog(userId, DatabaseOperation.edit, TableName.Course, courseName); if (!log) { throw new Exception(); } var student = db.Users.FirstOrDefault(x => x.Email == studentEmail); Course course = db.Courses.FirstOrDefault(x => x.Name == courseName); if (student == null || course == null) { throw new Exception(); } ApplicationUserCourse applicationUserCourse = db.ApplicationUserCourses.FirstOrDefault(x => x.CourseId == course.Id && x.ApplicationUserId == student.Id); db.ApplicationUserCourses.Remove(applicationUserCourse); course.ApplicationUsers.Remove(applicationUserCourse); var users = db.Users.Where(x => x.Courses.Contains(applicationUserCourse)).ToList(); users.ForEach(user => { user.Courses.Remove(applicationUserCourse); }); result = true; } catch (Exception) { } return(result); }
public bool AddInstructorToCourse(string userId, string courseName, string instructorId) { // course can contain multiple instructors. var result = false; try { var log = UpdateLog(userId, DatabaseOperation.add, TableName.Course, courseName); if (!log) { throw new Exception(); } var instructor = db.Users.Find(instructorId); Course course = db.Courses.FirstOrDefault(x => x.Name == courseName); if (instructor == null || course == null || instructor.Type == null) { throw new Exception(); } ApplicationUserCourse applicationUserCourse = new ApplicationUserCourse(); applicationUserCourse.ApplicationUserId = instructor.Id; applicationUserCourse.ApplicationUser = instructor; applicationUserCourse.CourseId = course.Id; applicationUserCourse.Course = course; applicationUserCourse.DateJoined = DateTime.Now; course.ApplicationUsers.Add(applicationUserCourse); db.SaveChanges(); result = true; return(result); } catch (Exception) { return(result); } }