public async Task <UserCourseDescription[]> GetUserCoursesAsync(string userId) { var studentCoursesIds = await _courseMatesRepository .FindAll(cm => cm.StudentId == userId && cm.IsAccepted == true) .Select(cm => cm.CourseId) .ToArrayAsync() .ConfigureAwait(false); var getStudentCoursesTasks = studentCoursesIds .Select(id => _coursesRepository.GetAsync(id)) // TODO: optimize .ToArray(); var studentCourses = await Task.WhenAll(getStudentCoursesTasks).ConfigureAwait(false); var getMentorCoursesTask = _coursesRepository .FindAll(c => c.MentorId == userId) .ToArrayAsync(); var mentorCourses = await getMentorCoursesTask.ConfigureAwait(false); return(studentCourses .Union(mentorCourses) .Select(c => { var userCourseDescription = _mapper.Map <UserCourseDescription>(c); userCourseDescription.UserIsMentor = c.MentorId == userId; return userCourseDescription; }) .OrderBy(c => c.UserIsMentor).ThenBy(c => c.Name) .ToArray()); }
public IActionResult Index() { var courses = _coursesRepository.FindAll().ToList(); var model = _mapper.Map <List <CourseVM> >(courses); return(View(model)); }