Files
xIdentityService/Providers/XPersonService.cs
T

92 lines
2.4 KiB
C#

using System;
using System.Linq;
using xModels.Dtos;
using xCommons.Extensions;
using xExceptions.Constants;
using System.Threading.Tasks;
using xIdentityService.Extensions;
using xIdentityService.Interfaces;
using System.Collections.Concurrent;
namespace xIdentityService.Providers
{
public class XPersonService : IXPersonService
{
//
private readonly IXIdentityProvider identityProvider;
private readonly ConcurrentDictionary<string, XPersonDto> persons = new ConcurrentDictionary<string, XPersonDto>();
public XPersonService(
IXIdentityProvider identityProvider
)
{
this.identityProvider = identityProvider;
}
public async Task<XPersonDto> GetPerson(string userId)
{
//
// Validate ...
if (userId.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
//
XPersonDto result = null;
//
// if Persons has Child, Check it ...
if (persons.HasChild())
{
//
var personEntry = persons
.FirstOrDefault(p => p.Key.Equals(userId, StringComparison.InvariantCultureIgnoreCase));
if (!personEntry.IsNull())
{
result = personEntry.Value;
}
}
//
// Check if Person not Exists ...
if (result.IsNullOrDefault())
{
//
// Prepare Device ...
var device = identityProvider.GetDevice();
//
// Retrieve User Info in OpenAction ...
var userInfo = await identityProvider
.GetUserInfo(
device: device,
userSelectByParam: userId
);
//
// Converts to Person Dto ...
result = userInfo.ToXPersonDto();
//
// Update Persons Dictionary ...
if (!result.IsNullOrDefault())
{
persons[userId] = result;
}
}
//
Cleanup();
//
return result;
}
public void Cleanup()
{
//
// TODO: Implement Cleanup Pesons ...
}
}
}