示例#1
0
        public async Task <ActionResult <CourseRolesResponse> > CourseRoles()
        {
            var userId = User.GetUserId();
            var isSystemAdministrator = await usersRepo.IsSystemAdministrator(userId);

            var visibleCourses = await unitsRepo.GetVisibleCourses();

            var rolesByCourse = (await courseRolesRepo.GetRoles(userId).ConfigureAwait(false))
                                .Where(kvp => kvp.Value != CourseRoleType.Student).ToList();
            var courseAccesses = await coursesRepo.GetUserAccesses(userId).ConfigureAwait(false);

            var courseAccessesByCourseId = courseAccesses.GroupBy(a => a.CourseId).Select(
                g => new CourseAccessResponse
            {
                CourseId = g.Key,
                Accesses = g.Select(a => a.AccessType).ToList()
            }
                ).ToList();
            var coursesInWhichUserHasAnyRole = new HashSet <string>(rolesByCourse.Select(kvp => kvp.Key), StringComparer.OrdinalIgnoreCase);
            var groupsWhereIAmStudent        = (await groupMembersRepo.GetUserGroupsAsync(userId).ConfigureAwait(false))
                                               .Where(g => visibleCourses.Contains(g.CourseId) || coursesInWhichUserHasAnyRole.Contains(g.CourseId) || isSystemAdministrator);

            return(new CourseRolesResponse
            {
                IsSystemAdministrator = isSystemAdministrator,
                CourseRoles = rolesByCourse.Select(kvp => new CourseRoleResponse
                {
                    CourseId = kvp.Key,
                    Role = kvp.Value,
                }).ToList(),
                CourseAccesses = courseAccessesByCourseId,
                GroupsAsStudent = groupsWhereIAmStudent.Where(g => g.CanUsersSeeGroupProgress).Select(BuildShortGroupInfo).ToList()
            });
        }