using ContactService.WebApi.Application.Dtos; using Grpc.Core; using IM.Commons; using IM.Protocols.Grpc.User; namespace ContactService.WebApi.Application.IntegrationServices { public class IdentityIntegrationService : IIdentityIntegrationService { private readonly UserInternal.UserInternalClient client; public IdentityIntegrationService(UserInternal.UserInternalClient client) { this.client = client; } public async Task> FindUserByIdAsync(Guid id) { var req = new GetUserInfoRequest() { UserId = id.ToString() }; try { var res = await client.GetUserInfoAsyncAsync(req); return Result.Success(new UserInfoDto { Avatar = res.Avatar, CreationTime = res.CreationTime.ToDateTimeOffset(), Deletion = res.Deletion != null ? res.Deletion.ToDateTimeOffset() : null, Description = res.Description, Email = res.Email, Id = Guid.Parse(res.Id), NickName = res.NickName, Phone = res.Phone, Region = res.Region, UserName = res.UserName }); }catch(RpcException e) { return Result.Fail(ResultCode.USER_NOT_FOUND); } } public async Task>> FindByIdsAsync(List ids) { if (ids == null || ids.Count == 0) return Result.Success(new Dictionary()); var req = new GetUserListRequest(); req.UserIds.AddRange(ids.Select(x => x.ToString())); try { var res = await client.GetUserListAsyncAsync(req); var dict = res.Users.ToDictionary( u => Guid.Parse(u.Id), u => new UserInfoDto { Id = Guid.Parse(u.Id), UserName = u.UserName, NickName = u.NickName, Email = u.Email, Phone = u.Phone, Region = u.Region, Description = u.Description, Avatar = u.Avatar, CreationTime = u.CreationTime.ToDateTimeOffset(), Deletion = u.Deletion != null ? u.Deletion.ToDateTimeOffset() : null }); return Result.Success(dict); } catch (RpcException) { return Result.Fail>(ResultCode.USER_NOT_FOUND); } } } }