public async Task <List <CourseWithTeachers> > GetTeachListAsync(string userID) { try { var ucList = await dbContext.UserCourse.Where(uc => uc.UserId == userID).ToListAsync(); var teachList = new List <CourseWithTeachers>(); foreach (var uc in ucList) { if (uc.UserRole == UserRole.Teacher) { var course = await dbContext.Courses.FindAsync(uc.CourseId); var t = new CourseWithTeachers() { Course = course, Teachers = await this.GetCourseTeachersAsync(course.Id) }; teachList.Add(t); } } return(teachList); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("Action Failed"); } }
public async Task <CourseResList> GetCoursesAsync(string keyword, int start, int limit) { try { var q = dbContext.Courses.Where(c => c.Name.Contains(keyword)); var courseList = await q.Skip(start).Take(limit).ToListAsync(); var totalCount = q.Count(); var resList = new List <CourseWithTeachers>(); foreach (var course in courseList) { var teacherList = await this.GetCourseTeachersAsync(course.Id); var res = new CourseWithTeachers() { Course = course, Teachers = teacherList }; resList.Add(res); } return(new CourseResList() { TotalCount = totalCount, ResList = resList }); } catch (Exception e) { logger.LogError(e.Message); throw new Exception("Unable to Get Course Info"); } }