public static UserCourseRoleMapping GetBotUser(int courseID) { using (var entities = new QBotEntities()) { try { var ucrm = entities.UserCourseRoleMappings .Include(x => x.User) .Include(x => x.User.TutorialGroupMemberships) .Include(x => x.User.AnswerPoster) .Include(x => x.User.OriginalPoster) .Include(x => x.User.UserCourseRoleMappings) .Include(x => x.Role) .Include(x => x.Role.UserCourseRoleMappings) .Include(x => x.Course) .Include(x => x.Course.Questions) .Include(x => x.Course.TutorialGroups) .Include(x => x.Course.UserCourseRoleMappings) .Where(x => x.CourseId == courseID && x.RoleId == 4).FirstOrDefault(); if (ucrm == null) { var user = entities.Users.Where(x => x.FirstName == "Question" && x.LastName == "Bot").FirstOrDefault(); if (user == null) { user = new User() { FirstName = "Question", LastName = "Bot", }; entities.Users.Add(user); entities.SaveChanges(); } ucrm = new UserCourseRoleMapping() { UserId = user.Id, CourseId = courseID, RoleId = 4, }; entities.UserCourseRoleMappings.Add(ucrm); entities.SaveChanges(); } return(ucrm); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <TutorialGroup> SaveTutorialGroup(TutorialGroup tutorial) { using (var entities = new QBotEntities()) { try { if (tutorial.Id == 0) { entities.TutorialGroups.Add(tutorial); } else { TutorialGroup editTutorial = entities.TutorialGroups.Where(x => x.Id == tutorial.Id).FirstOrDefault(); editTutorial.Name = tutorial.Name; editTutorial.Code = tutorial.Code; editTutorial.CourseId = tutorial.CourseId; } entities.SaveChanges(); return(entities.TutorialGroups.Where(x => x.CourseId == tutorial.CourseId).ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <UserCourseRoleMapping> DeleteUserCourseRoleMapping(UserCourseRoleMapping ucrm) { try { using (var entities = new QBotEntities()) { int courseId = ucrm.CourseId; var tutorialGroupMappingsToDelete = from tutorialMapping in entities.TutorialGroupMemberships where tutorialMapping.UserId == ucrm.UserId select tutorialMapping; if (tutorialGroupMappingsToDelete != null) { entities.TutorialGroupMemberships.RemoveRange(tutorialGroupMappingsToDelete); } var ucrms = entities.UserCourseRoleMappings.Where(x => x.Id == ucrm.Id).FirstOrDefault(); entities.UserCourseRoleMappings.Remove(ucrms); entities.SaveChanges(); var updatedList = entities.UserCourseRoleMappings.Where(x => x.CourseId == courseId) .Include(x => x.User) .Include(x => x.User.TutorialGroupMemberships) .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup)) .Include(x => x.Course) .Include(x => x.Role) .ToList(); return(updatedList); } } catch (Exception e) { LogExceptions(e); return(null); } }
public static List <Course> SaveCourse(Course course) { using (var entities = new QBotEntities()) { try { if (course.Id == 0)//new course { entities.Courses.Add(course); } else { Course editCourse = entities.Courses.Where(x => x.Id == course.Id).FirstOrDefault(); editCourse.Name = course.Name; editCourse.PredictiveQnAServiceHost = course.PredictiveQnAServiceHost; editCourse.PredictiveQnAServiceHost = course.PredictiveQnAServiceHost; editCourse.PredictiveQnAKnowledgeBaseId = course.PredictiveQnAKnowledgeBaseId; editCourse.PredictiveQnAEndpointKey = course.PredictiveQnAEndpointKey; editCourse.PredictiveQnAHttpEndpoint = course.PredictiveQnAHttpEndpoint; editCourse.PredictiveQnAHttpKey = course.PredictiveQnAHttpKey; editCourse.PredictiveQnAKnowledgeBaseName = course.PredictiveQnAKnowledgeBaseName; editCourse.PredictiveQnAConfidenceThreshold = course.PredictiveQnAConfidenceThreshold; editCourse.GroupId = course.GroupId; } entities.SaveChanges(); return(entities.Courses.ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <TutorialGroup> DeleteTutorialGroup(TutorialGroup tutorial) { using (var entities = new QBotEntities()) { try { int CourseId = tutorial.CourseId; TutorialGroup tutorialGroupToDelete = entities.TutorialGroups.Where(x => x.Id == tutorial.Id).FirstOrDefault(); if (tutorialGroupToDelete != null) { var tutorialGroupMappingsToDelete = from tutorialMapping in entities.TutorialGroupMemberships where tutorialMapping.TutorialGroupId == tutorialGroupToDelete.Id select tutorialMapping; if (tutorialGroupMappingsToDelete != null) { entities.TutorialGroupMemberships.RemoveRange(tutorialGroupMappingsToDelete); } entities.TutorialGroups.Remove(tutorialGroupToDelete); entities.SaveChanges(); } return(entities.TutorialGroups.Where(x => x.CourseId == CourseId).ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <UserCourseRoleMapping> SaveUserCourseRoleMapping(UserCourseRoleMapping ucrm, List <int> tutorialIds) { try { using (var entities = new QBotEntities()) { int courseId = ucrm.CourseId; var ucrmToUpdate = entities.UserCourseRoleMappings.Where(x => x.Id == ucrm.Id).FirstOrDefault(); if (ucrm == null) { ucrmToUpdate = new UserCourseRoleMapping(); ucrmToUpdate.UserId = ucrm.UserId; ucrmToUpdate.CourseId = courseId; } ucrmToUpdate.RoleId = ucrm.RoleId; entities.UserCourseRoleMappings.AddOrUpdate(ucrmToUpdate); // Reset all tutorial group memberships for this course List <TutorialGroupMembership> membershipsToDelete = entities.TutorialGroupMemberships .Where(x => x.UserId == ucrm.UserId && x.TutorialGroup.CourseId == courseId) .ToList(); if (membershipsToDelete != null) { entities.TutorialGroupMemberships.RemoveRange(membershipsToDelete); } // Add in tutorial group memberships from the UI foreach (int i in tutorialIds) { entities.TutorialGroupMemberships.Add(new TutorialGroupMembership() { TutorialGroupId = i, UserId = ucrm.UserId, }); } entities.SaveChanges(); var updatedList = entities.UserCourseRoleMappings.Where(x => x.CourseId == courseId) .Include(x => x.User) .Include(x => x.User.TutorialGroupMemberships) .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup)) .Include(x => x.Course) .Include(x => x.Role) .ToList(); return(updatedList); } } catch (Exception e) { LogExceptions(e); return(null); } }
public static int AddOrUpdateQuestion(Question question) { using (var entities = new QBotEntities()) { try { var dbQuestion = entities.Questions.Where(x => x.Id == question.Id) .Include("QuestionUser") .Include("AnswerUser") .Include("Course") .Include(x => x.Course.UserCourseRoleMappings) .Include(x => x.Course.Questions) .Include(x => x.Course.TutorialGroups) .FirstOrDefault(); if (dbQuestion != null) { dbQuestion.Status = question.Status; dbQuestion.AnswerText = question.AnswerText; dbQuestion.AnswerMessageId = question.AnswerMessageId; dbQuestion.AnswerPosterId = question.AnswerPosterId; dbQuestion.DateAnswered = question.DateAnswered; dbQuestion.AnswerCardActivityId = question.AnswerCardActivityId; entities.SaveChanges(); } else { entities.Questions.Add(question); } entities.SaveChanges(); return(question.Id); } catch (Exception e) { LogExceptions(e); return(0); } } }
public static List <TutorialGroupMembership> SetUserTutorialGroups(List <TutorialGroupMembership> tutorialGroupMembership) { using (var entities = new QBotEntities()) { try { var utg = entities.TutorialGroupMemberships.AddRange(tutorialGroupMembership); entities.SaveChanges(); return(utg.ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <User> SetUsers(List <User> users) { using (var entities = new QBotEntities()) { try { var u = entities.Users.AddRange(users); entities.SaveChanges(); return(users.ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static bool UpdateUser(User user) { using (var entities = new QBotEntities()) { try { var userId = user.Id; entities.Users.AddOrUpdate(user); entities.SaveChanges(); return(user.Id == userId); } catch (Exception e) { LogExceptions(e); return(false); } } }
public static List <Course> DeleteCourse(int id) { using (var entities = new QBotEntities()) { try { Course editCourse = entities.Courses.Where(x => x.Id == id).FirstOrDefault(); if (editCourse != null) { var ucrmToDelete = from ucrm in entities.UserCourseRoleMappings where ucrm.CourseId == editCourse.Id select ucrm; if (ucrmToDelete != null) { entities.UserCourseRoleMappings.RemoveRange(ucrmToDelete); } var tutorialGroupMembershipsToDelete = from tgm in entities.TutorialGroupMemberships where tgm.TutorialGroup.CourseId == id select tgm; if (tutorialGroupMembershipsToDelete != null) { entities.TutorialGroupMemberships.RemoveRange(tutorialGroupMembershipsToDelete); } var tutorialGroupsToDelete = from tg in entities.TutorialGroups where tg.CourseId == id select tg; if (tutorialGroupsToDelete != null) { entities.TutorialGroups.RemoveRange(tutorialGroupsToDelete); } entities.Courses.Remove(editCourse); entities.SaveChanges(); } return(entities.Courses.ToList()); } catch (Exception e) { LogExceptions(e); return(null); } } }
public static List <UserCourseRoleMapping> AddStudents(List <User> students, string coursename) { try { using (var entities = new QBotEntities()) { Course course = entities.Courses.Where(x => x.Name == coursename).FirstOrDefault(); int studentRoleId = entities.Roles.Where(x => x.Name == "Student").FirstOrDefault().Id; foreach (User student in students) { int studentID = 0; if (course != null) { User studentToUpdate = entities.Users.Where(x => x.UserPrincipalName == student.UserPrincipalName).FirstOrDefault(); // Existing student if (studentToUpdate != null) { studentToUpdate.FirstName = student.FirstName; studentToUpdate.LastName = student.LastName; studentToUpdate.UserPrincipalName = student.UserPrincipalName; studentToUpdate.Email = student.Email; studentID = studentToUpdate.Id; } else { User newStudent = new User { StudentId = student.StudentId, FirstName = student.FirstName, LastName = student.LastName, UserPrincipalName = student.UserPrincipalName, Email = student.Email, }; entities.Users.Add(newStudent); // Need this line to generate identity entities.SaveChanges(); studentID = newStudent.Id; } // Write course role mapping UserCourseRoleMapping ucrm = entities.UserCourseRoleMappings.Where(x => x.CourseId == course.Id && x.UserId == studentID).FirstOrDefault(); if (ucrm == null) { ucrm = new UserCourseRoleMapping { UserId = studentID, CourseId = course.Id, RoleId = studentRoleId, }; entities.UserCourseRoleMappings.Add(ucrm); entities.SaveChanges(); } } } entities.SaveChanges(); return(entities.UserCourseRoleMappings .Include(x => x.User) .Include(x => x.User.TutorialGroupMemberships) .Include(x => x.User.TutorialGroupMemberships.Select(t => t.TutorialGroup)) .Include(x => x.User.AnswerPoster) .Include(x => x.User.OriginalPoster) .Include(x => x.User.UserCourseRoleMappings) .Include(x => x.Role) .Include(x => x.Role.UserCourseRoleMappings) .Include(x => x.Course) .Include(x => x.Course.Questions) .Include(x => x.Course.TutorialGroups) .Include(x => x.Course.UserCourseRoleMappings) .Where(x => x.CourseId == course.Id).ToList()); } } catch (Exception e) { LogExceptions(e); return(null); } }