public async Task <ActionResult <List <UserDTO> > > GetCourseMembers(Guid id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var cachedMembers = await cache.GetCourseMembersAsync(id);

            if (!(cachedMembers is null))
            {
                return(cachedMembers);
            }

            var course = await _context.Courses.FindAsync(id);

            if (course is null)
            {
                return(NotFound());
            }

            var courseGroups = await _context.Courses
                               .Where(c => c.Id == id)
                               .Select(c => c.Groups.Select(g => g.Group))
                               .FirstOrDefaultAsync();

            var members = new List <UserDTO>();

            foreach (var group in courseGroups)
            {
                var users = await userManager.GetUsersForClaimAsync(new System.Security.Claims.Claim("study_group", group.Code));

                members.AddRange(users.Select(user => courseMapper.MapUser(user)));
            }

            var nonGroupMembers = await _context.Courses
                                  .Where(c => c.Id == id)
                                  .Select(c => c.Students.Select(s => s.Student.User))
                                  .FirstOrDefaultAsync();

            members.AddRange(nonGroupMembers.Select(user => courseMapper.MapUser(user)));

            members = members.Distinct(ProjectionEqualityComparer <UserDTO> .Create(u => u.Id))
                      .OrderBy(u => u.FirstName)
                      .ToList();

            await cache.SetCourseMembersAsync(id, members);

            return(members);
        }
Exemple #2
0
 private async Task <IEnumerable <UserDTO> > GetUsersInRole(string role)
 {
     return((await userManager.GetUsersInRoleAsync(role))
            .Select(user => courseMapper.MapUser(user)));
 }