184 lines
6.5 KiB
C#
184 lines
6.5 KiB
C#
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Accounts;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.CoreRequests;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Mixed;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Organizations;
|
|
using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Persons;
|
|
using LCU.Library.Internal.LcuHttpClient.HttpRequest;
|
|
using LCU.WebApps.Internal.DocumentManager.Configurations;
|
|
using Newtonsoft.Json;
|
|
using Serilog;
|
|
using System.Collections.Specialized;
|
|
using System.Web;
|
|
|
|
namespace LCU.WebApps.Internal.DocumentManager.Services;
|
|
|
|
public class CoreApiWrapperService : IEntityService
|
|
{
|
|
private readonly IGetRequest _getRequest;
|
|
private readonly CoreApiWrapperConfiguration _coreApiWrapperConfiguration;
|
|
|
|
public CoreApiWrapperService(IGetRequest getRequest,
|
|
CoreApiWrapperConfiguration coreApiWrapperConfiguration)
|
|
{
|
|
_getRequest = getRequest;
|
|
_coreApiWrapperConfiguration = coreApiWrapperConfiguration;
|
|
}
|
|
|
|
public async Task<IEnumerable<Person>> GetPersonByEntityNumber(string entityNumber)
|
|
{
|
|
try
|
|
{
|
|
var url = _coreApiWrapperConfiguration.GetPersonEndpointUrl.Replace("{personNumber}", entityNumber);
|
|
|
|
return await _getRequest.AsyncWithNewton<List<Person>>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get Person By Entity Number. EntityNumber: {entityNumber}");
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<Person>?> GetPersonsByNameLike(PersonNameRequest request, int limit)
|
|
{
|
|
try
|
|
{
|
|
var uriString = EncodePersonNameRequestUri(request, limit);
|
|
var url = $"{_coreApiWrapperConfiguration.GetPersonsByNameLikeUrl}{uriString}";
|
|
|
|
return await _getRequest.AsyncWithNewton<List<Person>>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get Persons By Name Like: PersonNameRequest. PersonNameRequest: {JsonConvert.SerializeObject(request)}. Limit: {limit}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<int> GetCountPersonsByNameLike(PersonNameRequest request)
|
|
{
|
|
try
|
|
{
|
|
var uriString = EncodePersonNameRequestUri(request);
|
|
var url = $"{_coreApiWrapperConfiguration.GetCountPersonsByNameLikeUrl}{uriString}";
|
|
|
|
return await _getRequest.AsyncWithNewton<int>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get Count Persons By Name Like: PersonNameRequest: {JsonConvert.SerializeObject(request)}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<Organization>> GetOrganizationByEntityNumber(string entityNumber)
|
|
{
|
|
try
|
|
{
|
|
var url = _coreApiWrapperConfiguration.GetOrganizationEndpointUrl.Replace("{organizationNumber}", entityNumber);
|
|
|
|
return await _getRequest.AsyncWithNewton<List<Organization>>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get Organization By Entity Number. EntityNumber: {entityNumber}");
|
|
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<Organization>?> GetOrganizationByOrganizationName(string organizationName, int limit)
|
|
{
|
|
try
|
|
{
|
|
var query = HttpUtility.ParseQueryString(string.Empty);
|
|
query["limit"] = limit.ToString();
|
|
var url = _coreApiWrapperConfiguration.GetOrganizationByNameEndpointUrl.Replace("{organizationName}", organizationName) + $"?{query}";
|
|
|
|
return await _getRequest.AsyncWithNewton<List<Organization>>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get Organization By Organization Name. OrganizationName: {organizationName}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private string EncodePersonNameRequestUri(PersonNameRequest request, int? limit = null)
|
|
{
|
|
NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(string.Empty);
|
|
if (!string.IsNullOrWhiteSpace(request.FirstName))
|
|
{
|
|
nameValueCollection.Add("FirstName", request.FirstName);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.MdlName))
|
|
{
|
|
nameValueCollection.Add("MdlName", request.MdlName);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.LastName))
|
|
{
|
|
nameValueCollection.Add("LastName", request.LastName);
|
|
}
|
|
|
|
if (limit != null)
|
|
{
|
|
nameValueCollection.Add("limit", limit.ToString());
|
|
}
|
|
|
|
return $"?{nameValueCollection}";
|
|
}
|
|
|
|
public async Task<IEnumerable<AccountOwnership>> GetOwnersByAccountNumber(long accountNumber, DateTime creationDate)
|
|
{
|
|
try
|
|
{
|
|
var url = _coreApiWrapperConfiguration.GetOwnersByAccountNumberUrl;
|
|
var route = new UriBuilder(url);
|
|
var query = HttpUtility.ParseQueryString("");
|
|
query["accountNumber"] = accountNumber.ToString();
|
|
query["creationDate"] = creationDate.ToString();
|
|
route.Query = query.ToString();
|
|
|
|
return await _getRequest.AsyncWithNewton<List<AccountOwnership>>(route.ToString());
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Get AccountOwnership by account number. AccountNumber: {accountNumber}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<long> CountOrganizationNumbersByName(string organizationName)
|
|
{
|
|
try
|
|
{
|
|
var url = _coreApiWrapperConfiguration.CountOrganizationNumbersByNameUrl.Replace("{organizationName}", organizationName);
|
|
|
|
return await _getRequest.AsyncWithNewton<long>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Counting Organization By Organization Name. OrganizationName: {organizationName}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<PersonOrganization> GetPersonOrOrganizationNumberByMemberId(long memberId)
|
|
{
|
|
try
|
|
{
|
|
var url = _coreApiWrapperConfiguration.GetPersonOrOrganizationNumberByMemberIdUrl.Replace("{memberId}", memberId.ToString());
|
|
|
|
return await _getRequest.AsyncWithNewton<PersonOrganization>(url);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Error Getting Member by Id. MemberId: {memberId}");
|
|
throw;
|
|
}
|
|
}
|
|
}
|