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; } }