public int GetCourseId(int chapterId) { using (var dbContext = new EduTestEntities()) { return dbContext.Chapters.First(c => c.Id == chapterId).Module.CourseId; } }
public Task<IEnumerable<QuestionBaseModel>> GetQuestions(int page, int perPage) { using (var dbModel = new EduTestEntities()) { throw new NotImplementedException(); } }
public async Task<bool> ExistsTopic(int id) { using (var dbContext = new EduTestEntities()) { return await dbContext.Topics.AnyAsync(u => u.Id == id); } }
public Task<int> GetTotalCount() { using (var dbModel = new EduTestEntities()) { return dbModel.Students.CountAsync(); } }
public async Task<int> AddDeepCourse(CourseModel courseModel) { using (var dbContext = new EduTestEntities()) { var course = new Course() { Name = courseModel.Name, Modules = courseModel.Modules.Select(m => new Module() { Name = m.Name, Chapters = m.Chapters.Select(c => new Chapter() { Name = c.Name, Topics = c.Topics.Select(t => new Topic() { Name = t.Name }).ToList() }).ToList() }).ToList() }; dbContext.Courses.Add(course); if (await dbContext.SaveChangesAsync() < 0) throw new Exception("CoursesRepository.AddCourse: Could not add course to db"); return course.Id; } }
public int GetCourseId(int moduleId) { using (var dbContext = new EduTestEntities()) { return dbContext.Modules.First(m => m.Id == moduleId).CourseId; } }
public int GetCourseId(int topicId) { using (var dbContext = new EduTestEntities()) { return dbContext.Topics.First(c => c.Id == topicId).Chapter.Module.CourseId; } }
public Task<int> AddQuestion(QuestionBaseModel question) { using (var dbModel = new EduTestEntities()) { throw new NotImplementedException(); //var dbQuestion = Mapper.Map<QuestionBase>() } }
public async Task<StudentModel> GetStudent(int id) { using (var dbModel = new EduTestEntities()) { var st = await dbModel.Students .Include(s => s.User) .FirstOrDefaultAsync(s => s.UserId == id); return Mapper.Map<StudentModel>(st); } }
public async Task<int> GetTotalCount4Teacher(int teacherId) { using (var dbModel = new EduTestEntities()) { var teacherCourses = (await GetUserCoursesAsync(dbModel, teacherId)) .Select(c => c.Id); return await dbModel.Students .CountAsync(s => s.User.Courses.Any( c => teacherCourses.Contains(c.Id))); } }
public async Task<IEnumerable<StudentModel>> GetStudents(int page, int perPage) { using (var dbModel = new EduTestEntities()) { var students = await dbModel.Students .Include(s => s.User) .Include(s => s.User.PersonalDetail) .OrderBy(s => s.UserId) .Skip(page * perPage) .Take(perPage) .ToListAsync(); return base.Mapper.Map<IEnumerable<StudentModel>>(students); } }
public async Task<IEnumerable<CourseModel>> GetCourses(int page, int perPage) { using (var dbContext = new EduTestEntities()) { var courses = await dbContext.Courses .Include(c => c.Modules .Select(m => m.Chapters .Select(ch => ch.Topics))) .OrderBy(c => c.Name) .Skip(page) .Take(perPage) .ToListAsync(); return courses.Select(ObjectMapper.MapCourse); } }
public async Task<CourseModel> GetCourse(int id) { using (var dbContext = new EduTestEntities()) { var course = await dbContext.Courses .Include(c => c.Modules .Select(m => m.Chapters .Select(ch => ch.Topics))) .FirstOrDefaultAsync(c => c.Id == id); if (course == null) throw new ObjectNotFoundException("Course with id " + id + " was not found in the database"); return ObjectMapper.MapCourse(course); } }
public async void RemoveModule(int id) { using (var dbContext = new EduTestEntities()) { var module = await dbContext.Modules.FirstOrDefaultAsync(u => u.Id == id); if (module == null) throw new ObjectNotFoundException("ModulesRepository.RemoveModule: Module not found"); if (module.Chapters.Any() || await dbContext.Tests.AnyAsync(t => t.ParentId == id)) throw new InvalidOperationException("ModulesRepository.RemoveModule cannot remove module as it has children"); dbContext.Modules.Remove(module); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("ModulesRepository.RemoveModule: Could not module from db"); } }
public async void RemoveChapter(int id) { using (var dbContext = new EduTestEntities()) { var chapter = await dbContext.Chapters.FirstOrDefaultAsync(u => u.Id == id); if (chapter == null) throw new ObjectNotFoundException("ChaptersRepository.RemoveChapter: Chapter not found"); if (chapter.Topics.Any() || await dbContext.Tests.AnyAsync(t => t.ParentId == id)) throw new InvalidOperationException("ChaptersRepository.RemoveChapter cannot remove chapter as it has children"); dbContext.Chapters.Remove(chapter); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("ChaptersRepository.RemoveChapter: Could not remove chapter from db"); } }
public async void RemoveTopic(int id) { using (var dbContext = new EduTestEntities()) { var topic = await dbContext.Topics.FirstOrDefaultAsync(u => u.Id == id); if (topic == null) throw new ObjectNotFoundException("TopicsRepository.RemoveTopic: Topic not found"); if (topic.QuestionBases.Any() || await dbContext.Tests.AnyAsync(t => t.ParentId == id)) throw new InvalidOperationException("TopicsRepository.RemoveTopic cannot remove topic as it has children"); dbContext.Topics.Remove(topic); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("TopicsRepository.RemoveTopic: Could not remove topic from db"); } }
public async Task<int> AddChapter(int moduleId, ChapterModel chapterModel) { using (var dbContext = new EduTestEntities()) { var chapter = new Chapter() { Name = chapterModel.Name, ModuleId = moduleId }; dbContext.Chapters.Add(chapter); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("ChapterRepository.AddChapter: Could not add user to db"); return chapter.Id; } }
public async Task<int> AddTopic(int chapterId, TopicModel topicModel) { using (var dbContext = new EduTestEntities()) { var topic = new Topic() { Name = topicModel.Name, ChapterId = chapterId }; dbContext.Topics.Add(topic); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("TopicRepository.AddTopic: Could not add topic to db"); return topic.Id; } }
public async Task<int> AddModule(int courseId, ModuleModel moduleModel) { using (var dbContext = new EduTestEntities()) { var module = new Module() { Name = moduleModel.Name, CourseId = courseId }; dbContext.Modules.Add(module); if (await dbContext.SaveChangesAsync() == 0) throw new Exception("UserRepository.AddUser: Could not add user to db"); return module.Id; } }
public async Task<IEnumerable<StudentModel>> GetStudents4Teacher(int teacherId, int page, int perPage) { using (var dbModel = new EduTestEntities()) { var teacherCourses = (await GetUserCoursesAsync(dbModel, teacherId)) .Select(c => c.Id); var students = await dbModel.Students .Include(s => s.User) .Include(s => s.User.PersonalDetail) .Where(s => s.User.Courses.Any(c => teacherCourses.Contains(c.Id))) .OrderBy(s => s.UserId) .Skip(page * perPage) .Take(perPage) .ToListAsync(); return base.Mapper.Map<IEnumerable<StudentModel>>(students); } }
public async Task<int> AddStudent(StudentModel student) { using (var dbModel = new EduTestEntities()) { var dbStud = Mapper.Map<Student>(student); dbStud.User.Password = "******"; // TODO add logic for generating and sharing password if (dbStud.User.Roles != null && dbStud.User.Roles.Any()) { foreach (var role in dbStud.User.Roles) dbModel.Entry(role).State = EntityState.Unchanged; } if (dbStud.User.Courses != null && dbStud.User.Courses.Any()) { foreach (var course in dbStud.User.Courses) dbModel.Entry(course).State = EntityState.Unchanged; } dbModel.Students.Add(dbStud); await dbModel.SaveChangesAsync(); return dbStud.UserId; } }
public async Task<UserModel> GetUser(int id) { using (var dbContext = new EduTestEntities()) { var user = await dbContext.Users .Include(u => u.Roles) .FirstOrDefaultAsync(u => u.Id == id); if (user == null) return null; //var userModel = new UserModel() //{ // Id = user.Id, // FirstName = user.FirstName, // LastName = user.LastName, // Email = user.Email, // Roles = user.Roles.Select(r => r.Name).ToArray() //}; var userModel = _mapper.Map<UserModel>(user); return userModel; } }
public async Task<int> AddUser(UserModel userModel) { using (var dbContext = new EduTestEntities()) { //var user = new User() //{ // //FirstName = userModel.FirstName, // //LastName = userModel.LastName, // //Email = userModel.Email, // personal details // //Password = userModel.Password, // Need to decide // Roles = ObjectMapper.MapRoles(userModel.Roles).ToList() //}; var user = _mapper.Map<User>(userModel); user.Password = "******"; // TODO should decide how to add pass dbContext.Users.Add(user); if (await dbContext.SaveChangesAsync() < 0) throw new Exception("UserRepository.AddUser: Could not add user to db"); return user.Id; } }
static void Main(string[] args) { using (var context = new EduTestEntities()) { //var users = context.Users.ToList(); //foreach (var user in users) //{ // Console.WriteLine(user.Id + " " + user.FirstName + " " + user.LastName); // Console.Write("Roles: "); // foreach (var role in user.Roles) // { // Console.Write("{0} ", role.Name); // } // Console.WriteLine("\n"); // var course = user.Courses.FirstOrDefault(); //} var user = context.Users.First(); var roles = context.Roles.ToEnumerable(); user.Roles.Clear(); foreach (var ro in roles) { user.Roles.Add(ro); } //user.Roles = roles.ToList(); context.SaveChanges(); var modules = context.Modules.Include(m => m.Chapters).ToList(); foreach (var module in modules) { Console.WriteLine(module.Name + "\n"); } } }
public async Task<int> AddCourse(CourseModel courseModel) { using (var dbContext = new EduTestEntities()) { var course = new Course() { Name = courseModel.Name }; dbContext.Courses.Add(course); if (await dbContext.SaveChangesAsync() < 0) throw new Exception("CoursesRepository.AddCourse: Could not add course to db"); return course.Id; } }
public Task<bool> ExistsCourse(int courseId) { using (var dbContext = new EduTestEntities()) { return dbContext.Courses.AnyAsync(c => c.Id == courseId); } }
public Task<int> GetNumberOfCourses(int userId) { using (var dbContext = new EduTestEntities()) { return dbContext.Courses.CountAsync(c => c.Users.Any(u => u.Id == userId)); } }
public Task<int> GetNumberOfCourses() { using (var dbContext = new EduTestEntities()) { return dbContext.Courses.CountAsync(); } }
public async void RemoveCourse(int id) { using (var dbContext = new EduTestEntities()) { var course = dbContext.Courses.FirstOrDefault(crs => crs.Id == id); if (course == null) throw new ObjectNotFoundException("CoursesRepository.RemoveCourse: Course not found"); if (course.Modules.Any() || await dbContext.Tests.AnyAsync(t => t.ParentId == id)) throw new InvalidOperationException("CoursesRepository.RemoveCourse cannot remove course as it has children"); dbContext.Courses.Remove(course); if (await dbContext.SaveChangesAsync() < 0) throw new Exception("CoursesRepository.RemoveCourse: Could not remove course from db"); } }
public async void RemoveDeepCourse(int id) { throw new NotImplementedException(); using (var dbContext = new EduTestEntities()) { var course = dbContext.Courses .Include(c => c.Modules .Select(m => m.Chapters .Select(ch => ch.Topics))) .First(crs => crs.Id == id); foreach (var module in course.Modules) { foreach (var chapter in module.Chapters) dbContext.Topics.RemoveRange(chapter.Topics); dbContext.Chapters.RemoveRange(module.Chapters); } dbContext.Modules.RemoveRange(course.Modules); dbContext.Courses.Remove(course); if (await dbContext.SaveChangesAsync() < 0) throw new Exception("CoursesRepository.RemoveDeepCourse: Could not remove course from db"); } }