| | | 1 | | using KT.Domain.Ports; |
| | | 2 | | using KT.Modules.Security.Presentation.Contracts; |
| | | 3 | | using KT.Modules.Security.Presentation.Dto; |
| | | 4 | | |
| | | 5 | | namespace KT.Modules.Security.Core.Application |
| | | 6 | | { |
| | 11 | 7 | | internal class SearchUserService(IUserRepository userRepository, IConnectionProvider connectionProvider) : ISearchUs |
| | | 8 | | { |
| | | 9 | | public async Task<ProfileDto> GetProfile(string userId) |
| | 4 | 10 | | { |
| | 4 | 11 | | if (string.IsNullOrEmpty(userId)) |
| | 2 | 12 | | throw new ArgumentException("Username cannot be null or empty", nameof(userId)); |
| | 2 | 13 | | var userFound = await userRepository.GetAsyncById(userId); |
| | 2 | 14 | | if (userFound == null) |
| | 1 | 15 | | throw new InvalidOperationException("User not found"); |
| | 1 | 16 | | var profileDto = new ProfileDto |
| | 1 | 17 | | { |
| | 1 | 18 | | CreatedAt = userFound.CreatedAt, |
| | 1 | 19 | | Email = userFound.Email, |
| | 1 | 20 | | Id = userFound.Id, |
| | 1 | 21 | | RealName = userFound.RealName, |
| | 1 | 22 | | Username = userFound.UsernameDisplay |
| | 1 | 23 | | }; |
| | 1 | 24 | | return profileDto; |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | // Agregamos currentUserId como parámetro para saber quién está buscando |
| | | 28 | | public async Task<List<UserDto>> SearchAsync(string username, string currentUserId) |
| | 3 | 29 | | { |
| | 3 | 30 | | if (string.IsNullOrWhiteSpace(username)) |
| | 1 | 31 | | return new List<UserDto>(); |
| | | 32 | | |
| | | 33 | | // PASO 1: Obtener los IDs de los amigos actuales |
| | | 34 | | // (Asumo que tienes un repositorio o método para obtener esto) |
| | | 35 | | // Debería devolver algo como: ["id_de_julitop", "id_de_otro_amigo"] |
| | 2 | 36 | | var friendIds = await connectionProvider.GetFriendIdsAsync(currentUserId); |
| | | 37 | | |
| | | 38 | | // Si friendIds es null, inicialízalo para evitar errores |
| | 3 | 39 | | if (friendIds == null) friendIds = new List<string>(); |
| | | 40 | | |
| | | 41 | | // PASO 2: Agregar TU propio ID a la lista de exclusión |
| | | 42 | | // (Para que no te encuentres a ti mismo en el buscador) |
| | 2 | 43 | | friendIds.Add(currentUserId); |
| | | 44 | | |
| | | 45 | | // PASO 3: Buscar en el repositorio pasando la "lista negra" |
| | 2 | 46 | | var usersFound = await userRepository.SearchUsersAsync(username.Trim(), friendIds); |
| | | 47 | | |
| | | 48 | | // PASO 4: Mapear a DTO |
| | 3 | 49 | | var userDto = usersFound.Select(u => new UserDto |
| | 3 | 50 | | { |
| | 3 | 51 | | Id = u.Id, |
| | 3 | 52 | | Username = u.UsernameDisplay |
| | 3 | 53 | | }).ToList(); |
| | | 54 | | |
| | 2 | 55 | | return userDto; |
| | 3 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<UserDto> SearchByIdAsync(string userId) |
| | 4 | 59 | | { |
| | 4 | 60 | | if (string.IsNullOrEmpty(userId)) |
| | 2 | 61 | | throw new ArgumentException("Username cannot be null or empty", nameof(userId)); |
| | 2 | 62 | | var userFound = await userRepository.GetAsyncById(userId); |
| | 2 | 63 | | if (userFound == null) |
| | 1 | 64 | | throw new InvalidOperationException("User not found"); |
| | 1 | 65 | | var userDto = new UserDto |
| | 1 | 66 | | { |
| | 1 | 67 | | Id = userFound.Id, |
| | 1 | 68 | | Username = userFound.UsernameDisplay |
| | 1 | 69 | | }; |
| | 1 | 70 | | return userDto; |
| | 1 | 71 | | } |
| | | 72 | | } |
| | | 73 | | } |