80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
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<Result<UserInfoDto>> 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<UserInfoDto>(ResultCode.USER_NOT_FOUND);
|
|
}
|
|
}
|
|
|
|
public async Task<Result<Dictionary<Guid, UserInfoDto>>> FindByIdsAsync(List<Guid> ids)
|
|
{
|
|
if (ids == null || ids.Count == 0)
|
|
return Result.Success(new Dictionary<Guid, UserInfoDto>());
|
|
|
|
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<Dictionary<Guid, UserInfoDto>>(ResultCode.USER_NOT_FOUND);
|
|
}
|
|
}
|
|
}
|
|
}
|