示例#1
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");
            }
        }
        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");
            }
        }
示例#3
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;
            }  
        }
        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 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 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<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 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 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");
            }
        }
示例#10
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");
            }
        }
示例#11
0
        public async Task UpdateUser(int id, UserModel userModel)
        {
            using (var dbContext = new EduTestEntities())
            {
                var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == id);

                if (user == null)
                    throw new Exception("UserRepository.UpdateUser: User not found");

                //user.FirstName = userModel.FirstName;
                //user.LastName = userModel.LastName;
                //user.Email = userModel.Email;         // personal det
                user.Roles = ObjectMapper.MapRoles(userModel.Roles).ToList();                

                if (await dbContext.SaveChangesAsync() == 0)
                    throw new Exception("UserRepository.UpdateUser: Could not add user to db");
            }
        }
示例#12
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;
            }
        }
示例#13
0
        public async void RemoveUser(int id)
        {
            using (var dbContext = new EduTestEntities())
            {
                var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == id);

                if (user == null)
                    throw new ObjectNotFoundException("UserRepository.RemoveUser: User not found");

                if (user.Courses.Any() || user.QuestionBases.Any() || 
                    user.TestInstances.Any() || user.Tests.Any())
                    throw new InvalidOperationException("UserRepository.RemoveUser cannot remove user as it has children");

                dbContext.Users.Remove(user);
                if (await dbContext.SaveChangesAsync() == 0)
                    throw new Exception("UserRepository.RemoveUser: Could not remove user from db");
            }
        }
示例#14
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;
     }
 }