Exemplo n.º 1
0
 public int GetCourseId(int chapterId)
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Chapters.First(c => c.Id == chapterId).Module.CourseId;
     }
 }
Exemplo n.º 2
0
 public Task<IEnumerable<QuestionBaseModel>> GetQuestions(int page, int perPage)
 {
     using (var dbModel = new EduTestEntities())
     {
         throw new NotImplementedException();
     }
 }
Exemplo n.º 3
0
 public async Task<bool> ExistsTopic(int id)
 {
     using (var dbContext = new EduTestEntities())
     {
         return await dbContext.Topics.AnyAsync(u => u.Id == id);
     }
 }
Exemplo n.º 4
0
 public Task<int> GetTotalCount()
 {
     using (var dbModel = new EduTestEntities())
     {
         return dbModel.Students.CountAsync();
     }
 }
Exemplo n.º 5
0
        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;
            }
        }
Exemplo n.º 6
0
 public int GetCourseId(int moduleId)
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Modules.First(m => m.Id == moduleId).CourseId;
     }
 }
Exemplo n.º 7
0
 public int GetCourseId(int topicId)
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Topics.First(c => c.Id == topicId).Chapter.Module.CourseId;
     }
 }
Exemplo n.º 8
0
 public Task<int> AddQuestion(QuestionBaseModel question)
 {
     using (var dbModel = new EduTestEntities())
     {
         throw new NotImplementedException();
         //var dbQuestion = Mapper.Map<QuestionBase>()
     }
 }
Exemplo n.º 9
0
        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);
            }
        }
Exemplo n.º 10
0
        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)));
            }
        }
Exemplo n.º 11
0
        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);
            }
        }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
            }
        }
Exemplo n.º 14
0
        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");
            }
        }
Exemplo n.º 15
0
        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");
            }
        }
Exemplo n.º 16
0
        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");
            }
        }
Exemplo n.º 17
0
        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;
            }   
        }
Exemplo n.º 18
0
        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;
            }  
        }
Exemplo n.º 19
0
        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;
            }
        }
Exemplo n.º 20
0
        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);
            }
        }
Exemplo n.º 21
0
 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;
     }
 }
Exemplo n.º 22
0
        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;
            }
        }
Exemplo n.º 23
0
        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;
            }
        }
Exemplo n.º 24
0
        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");                    
                }
            }
        }
Exemplo n.º 25
0
        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;
            }
        }
Exemplo n.º 26
0
 public Task<bool> ExistsCourse(int courseId)
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Courses.AnyAsync(c => c.Id == courseId);
     } 
 }
Exemplo n.º 27
0
 public Task<int> GetNumberOfCourses(int userId)
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Courses.CountAsync(c => c.Users.Any(u => u.Id == userId));
     }
 }
Exemplo n.º 28
0
 public Task<int> GetNumberOfCourses()
 {
     using (var dbContext = new EduTestEntities())
     {
         return dbContext.Courses.CountAsync();
     }
 }
Exemplo n.º 29
0
        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");
            }
        }
Exemplo n.º 30
0
        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");
            }
        }