@@ -19,6 +19,7 @@ import org.springframework.data.mongodb.core.aggregation.Aggregation.*
1919import org.springframework.data.mongodb.core.aggregation.ConvertOperators
2020import org.springframework.data.mongodb.core.aggregation.LookupOperation
2121import org.springframework.data.mongodb.core.query.Criteria
22+ import org.springframework.data.mongodb.core.query.Query
2223import org.springframework.http.HttpStatus
2324import org.springframework.security.crypto.password.PasswordEncoder
2425import org.springframework.stereotype.Service
@@ -155,15 +156,29 @@ class UserService(
155156
156157 fun findAllUsersAdmin (pageable : Pageable ): PagedResponseDTO <AdminUserDTO > {
157158 val userPage = userRepository.findAll(pageable)
159+ val userIds = userPage.content.mapNotNull { it.id }
160+
161+ val profilesMap: Map <String , Profile > = if (userIds.isNotEmpty()) {
162+ val profileQuery = Query (Criteria .where(" userId" ).`in `(userIds))
163+ mongoTemplate.find(profileQuery, Profile ::class .java).associateBy { it.userId }
164+ } else {
165+ emptyMap()
166+ }
167+
158168 val userDTOs = userPage.content.map { user ->
169+ val profile = profilesMap[user.id!! ]
159170 AdminUserDTO (
160171 id = user.id!! ,
161172 username = user.username,
162173 email = user.email,
163174 isVerified = user.isVerified,
164- roles = user.roles
175+ roles = user.roles,
176+ imageUrl = profile?.avatarUrl,
177+ createdAt = user.createdAt,
178+ lastLoginDate = user.lastLoginDate
165179 )
166180 }
181+
167182 return PagedResponseDTO (
168183 content = userDTOs,
169184 pageNumber = userPage.number,
@@ -177,12 +192,18 @@ class UserService(
177192 fun getUserByIdForAdmin (id : String ): AdminUserDTO {
178193 val user = userRepository.findById(id)
179194 .orElseThrow { ResponseStatusException (HttpStatus .NOT_FOUND , " Пользователь не найден" ) }
195+
196+ val profile = profileRepository.findByUserId(user.id!! )
197+
180198 return AdminUserDTO (
181199 id = user.id!! ,
182200 username = user.username,
183201 email = user.email,
184202 isVerified = user.isVerified,
185- roles = user.roles
203+ roles = user.roles,
204+ imageUrl = profile?.avatarUrl,
205+ createdAt = user.createdAt,
206+ lastLoginDate = user.lastLoginDate
186207 )
187208 }
188209
@@ -219,13 +240,17 @@ class UserService(
219240 )
220241
221242 val savedUser = userRepository.save(updatedUser)
243+ val userProfile = profileService.getProfileByUserId(user.id!! )
222244
223245 return AdminUserDTO (
224246 id = savedUser.id!! ,
225247 username = savedUser.username,
226248 email = savedUser.email,
227249 isVerified = savedUser.isVerified,
228- roles = savedUser.roles
250+ roles = savedUser.roles,
251+ imageUrl = userProfile.avatarUrl,
252+ createdAt = savedUser.createdAt,
253+ lastLoginDate = savedUser.lastLoginDate
229254 )
230255 }
231256
0 commit comments