@@ -99,23 +99,30 @@ private static async Task<IResult> GetUsers(IRepository<User> userRepository, st
9999 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
100100 private static async Task < IResult > GetUsersByCohort ( IRepository < Cohort > repository , int cohort_id , ClaimsPrincipal claimsPrincipal )
101101 {
102- var response = await repository . GetByIdWithIncludes ( a => a
102+ var response = await repository . GetWithIncludes ( a => a
103103 . Include ( p => p . CohortCourses )
104104 . ThenInclude ( b => b . Course )
105105 . Include ( p => p . CohortCourses )
106106 . ThenInclude ( b => b . UserCCs )
107107 . ThenInclude ( a => a . User )
108- . ThenInclude ( u => u . Notes ) , cohort_id ) ;
108+ . ThenInclude ( u => u . Notes ) ) ;
109+
110+ var cohortCourses = response . SelectMany ( c => c . CohortCourses ) . ToList ( ) ;
111+ var userCohortCourses = cohortCourses . SelectMany ( cc => cc . UserCCs ) . ToList ( ) ;
112+ var groupedUserCohortCourses = userCohortCourses . GroupBy ( uc => uc . UserId ) . ToList ( ) ;
113+ var currentUserCohortCourses = groupedUserCohortCourses . Select ( g => g . OrderBy ( f => f . Id ) . LastOrDefault ( ) ) . ToList ( ) ;
114+ var userCohortCoursesInCohort = currentUserCohortCourses . Where ( f => f . CohortCourse . CohortId == cohort_id ) . ToList ( ) ;
115+ var usersInCohort = userCohortCoursesInCohort . Select ( uc => uc . User ) . ToList ( ) ;
116+
117+
109118
110- var results = response . CohortCourses . SelectMany ( a => a . UserCCs ) . Select ( a => a . User ) . ToList ( ) ;
111- var dto_results = results . Select ( a => new UserDTO ( a ) ) ;
112119
113120 var userRole = claimsPrincipal . Role ( ) ;
114121 var authorizedAsTeacher = AuthorizeTeacher ( claimsPrincipal ) ;
115122
116123 var userData = new UsersSuccessDTO
117124 {
118- Users = results . Select ( user => authorizedAsTeacher
125+ Users = usersInCohort . Select ( user => authorizedAsTeacher
119126 ? new UserDTO ( user , PrivilegeLevel . Teacher ) //if teacher loads students, also load notes for students.
120127 : new UserDTO ( user , PrivilegeLevel . Student ) ) . ToList ( ) //if teacher loads students, also load notes for students.
121128 } ;
@@ -132,22 +139,27 @@ private static async Task<IResult> GetUsersByCohort(IRepository<Cohort> reposito
132139 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
133140 private static async Task < IResult > GetUsersByCohortCourse ( IRepository < CohortCourse > ccRepository , int cc_id , ClaimsPrincipal claimsPrincipal )
134141 {
135- var response = await ccRepository . GetByIdWithIncludes ( a => a
142+ var response = await ccRepository . GetWithIncludes ( a => a
136143 . Include ( z => z . Cohort )
137144 . Include ( z => z . Course )
138145 . Include ( b => b . UserCCs )
139- . ThenInclude ( a => a . User )
140- . ThenInclude ( u => u . Notes ) , cc_id ) ;
141-
142- var results = response . UserCCs . Select ( a => a . User ) . ToList ( ) ;
143- var dto_results = results . Select ( a => new UserDTO ( a ) ) ;
146+ . ThenInclude ( a => a . User )
147+ . ThenInclude ( u => u . Notes ) ) ;
148+
149+ var userCohortCourses = response . SelectMany ( cc => cc . UserCCs ) . ToList ( ) ;
150+ var groupedUserCohortCourses = userCohortCourses . GroupBy ( uc => uc . UserId ) . ToList ( ) ;
151+ var currentUserCohortCourses = groupedUserCohortCourses . Select ( g => g . OrderBy ( f => f . Id ) . LastOrDefault ( ) ) . ToList ( ) ;
152+ var userCohortCoursesInCohortCourse = currentUserCohortCourses . Where ( f => f . CohortCourse . Id == cc_id ) . ToList ( ) ;
153+ var usersInCohortCourse = userCohortCoursesInCohortCourse . Select ( uc => uc . User ) . ToList ( ) ;
154+
155+
144156
145157 var userRole = claimsPrincipal . Role ( ) ;
146158 var authorizedAsTeacher = AuthorizeTeacher ( claimsPrincipal ) ;
147159
148160 var userData = new UsersSuccessDTO
149161 {
150- Users = results . Select ( user => authorizedAsTeacher
162+ Users = usersInCohortCourse . Select ( user => authorizedAsTeacher
151163 ? new UserDTO ( user , PrivilegeLevel . Teacher ) //if teacher loads students, also load notes for students.
152164 : new UserDTO ( user , PrivilegeLevel . Student ) ) . ToList ( ) //if teacher loads students, also load notes for students.
153165 } ;
0 commit comments