367 lines
13 KiB
C#
367 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using ReserveManagementSystemLib.Models;
|
|
using ReserveManagementSystemUI.Helpers;
|
|
using ReserveManagementSystemUI.Model.Configuration;
|
|
using Serilog;
|
|
using LCU.Library.Internal.LcuHttpClient;
|
|
|
|
namespace ReserveManagementSystemUI.Services
|
|
{
|
|
public class ReserveService
|
|
{
|
|
private ILcuHttpClient _lcuHttpClient;
|
|
private IConfiguration _config;
|
|
private RmsUIConfiguration _rmsUIconfig;
|
|
private AuthenticationStateProvider _authenticationStateProvider;
|
|
private ServiceHelpers _serviceHelper;
|
|
|
|
public ReserveService(ILcuHttpClient lcuHttpClient,
|
|
AuthenticationStateProvider authenticationStateProvider,
|
|
ServiceHelpers serviceHelper,
|
|
RmsUIConfiguration rmsUIConfig)
|
|
{
|
|
_lcuHttpClient = lcuHttpClient;
|
|
_authenticationStateProvider = authenticationStateProvider;
|
|
_serviceHelper = serviceHelper;
|
|
_rmsUIconfig = rmsUIConfig;
|
|
}
|
|
|
|
public async Task<PaginatedListResponse> GetFilteredLoans(LoanFilter loanFilter)
|
|
{
|
|
PaginatedListResponse pagedLoanResponse = new PaginatedListResponse();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{ _serviceHelper.GetControllerApiUrl("ApiUrl", "LoanController")}/SearchLoans";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
|
|
string json = JsonConvert.SerializeObject(loanFilter);
|
|
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
HttpResponseMessage response = client.PostAsync(apiUrl, content).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
pagedLoanResponse = JsonConvert.DeserializeObject<PaginatedListResponse>(jsonString.Result);
|
|
}
|
|
|
|
return pagedLoanResponse;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting loans.");
|
|
pagedLoanResponse = null;
|
|
}
|
|
|
|
return await Task.FromResult(pagedLoanResponse);
|
|
}
|
|
|
|
public async Task<List<string>> GetMemberNumbers(string searchText)
|
|
{
|
|
var members = new List<string>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{ _serviceHelper.GetControllerApiUrl("ApiUrl", "LoanController")}/SearchMembers/{searchText}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
|
|
var response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
members = JsonConvert.DeserializeObject<List<string>>(jsonString.Result);
|
|
}
|
|
|
|
return members;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting members by search text.");
|
|
members = null;
|
|
}
|
|
|
|
return await Task.FromResult(members);
|
|
}
|
|
|
|
public async Task<List<string>> GetLoanNumbers(string searchText)
|
|
{
|
|
var loanNumbers = new List<string>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{ _serviceHelper.GetControllerApiUrl("ApiUrl", "LoanController")}/SearchLoanNumbers/{searchText}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
|
|
var response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
loanNumbers = JsonConvert.DeserializeObject<List<string>>(jsonString.Result);
|
|
}
|
|
|
|
return loanNumbers;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting loan numbers by search text.");
|
|
loanNumbers = null;
|
|
}
|
|
|
|
return await Task.FromResult(loanNumbers);
|
|
}
|
|
|
|
public async Task<List<string>> GetApplicationIds(string searchText)
|
|
{
|
|
var applicationIds = new List<string>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{ _serviceHelper.GetControllerApiUrl("ApiUrl", "LoanController")}/SearchApplicationIds/{searchText}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
|
|
var response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
applicationIds = JsonConvert.DeserializeObject<List<string>>(jsonString.Result);
|
|
}
|
|
|
|
return applicationIds;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting application ids by search text.");
|
|
applicationIds = null;
|
|
}
|
|
|
|
return await Task.FromResult(applicationIds);
|
|
}
|
|
|
|
public async Task<List<Dealer>> GetDealers(string dealerSearchText)
|
|
{
|
|
var dealers = new List<Dealer>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{ _serviceHelper.GetControllerApiUrl("ApiUrl", "DealerController")}/Search/{dealerSearchText}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
|
|
var response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
dealers = JsonConvert.DeserializeObject<List<Dealer>>(jsonString.Result);
|
|
}
|
|
|
|
return dealers;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting dealers by search text.");
|
|
dealers = null;
|
|
}
|
|
|
|
return await Task.FromResult(dealers);
|
|
}
|
|
|
|
public async Task<Loan> GetLoanById(int loanId)
|
|
{
|
|
Loan loanInfo = new Loan();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{_serviceHelper.GetControllerApiUrl("ApiUrl", "LoanController")}/GetById/{loanId}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
loanInfo = JsonConvert.DeserializeObject<Loan>(jsonString.Result);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting loan.");
|
|
loanInfo = null;
|
|
}
|
|
|
|
return await Task.FromResult(loanInfo);
|
|
}
|
|
|
|
public async Task<List<Import>> GetAllImports()
|
|
{
|
|
var imports = new List<Import>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = _serviceHelper.GetControllerApiUrl("ApiUrl", "ImportController");
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
imports = JsonConvert.DeserializeObject<List<Import>>(jsonString.Result) ?? new List<Import>();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting imports.");
|
|
imports = null;
|
|
}
|
|
|
|
return await Task.FromResult(imports);
|
|
}
|
|
|
|
public async Task<List<Import>> GetRecentImports(int numberOfImports)
|
|
{
|
|
var imports = new List<Import>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{_serviceHelper.GetControllerApiUrl("ApiUrl", "ImportController")}/GetRecentImports/{numberOfImports}";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
imports = JsonConvert.DeserializeObject<List<Import>>(jsonString.Result) ?? new List<Import>();
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting imports.");
|
|
imports = null;
|
|
}
|
|
|
|
return await Task.FromResult(imports);
|
|
}
|
|
|
|
public async Task<List<AuditHistory>> GetHistory()
|
|
{
|
|
var history = new List<AuditHistory>();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = _serviceHelper.GetControllerApiUrl("ApiUrl", "AuditHistoryController");
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
HttpResponseMessage response = client.GetAsync(apiUrl).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
history = JsonConvert.DeserializeObject<List<AuditHistory>>(jsonString.Result);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error getting audit history.");
|
|
history = null;
|
|
}
|
|
return await Task.FromResult(history);
|
|
}
|
|
|
|
public async Task<Import> GetActiveLoans()
|
|
{
|
|
var import = new Import();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{_serviceHelper.GetControllerApiUrl("ApiUrl", "ImportController")}/ImportActiveLoans";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
var timeout = Convert.ToDouble(_rmsUIconfig.DefaultImportTimeout);
|
|
client.Timeout = TimeSpan.FromMinutes(timeout);
|
|
|
|
HttpResponseMessage response = client.PutAsync(apiUrl, null).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
import = JsonConvert.DeserializeObject<Import>(jsonString.Result);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error running active loan import process.");
|
|
import = null;
|
|
}
|
|
|
|
return await Task.FromResult(import);
|
|
}
|
|
|
|
public async Task<Import> GetClosedLoans()
|
|
{
|
|
var import = new Import();
|
|
|
|
try
|
|
{
|
|
var authState = _authenticationStateProvider.GetAuthenticationStateAsync().Result;
|
|
var apiUrl = $"{_serviceHelper.GetControllerApiUrl("ApiUrl", "ImportController")}/ImportClosedLoans";
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
var timeout = Convert.ToDouble(_rmsUIconfig.DefaultImportTimeout);
|
|
client.Timeout = TimeSpan.FromMinutes(timeout);
|
|
|
|
HttpResponseMessage response = client.PutAsync(apiUrl, null).Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonString = response.Content.ReadAsStringAsync();
|
|
jsonString.Wait();
|
|
import = JsonConvert.DeserializeObject<Import>(jsonString.Result);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Error running closed loan import process.");
|
|
import = null;
|
|
}
|
|
|
|
return await Task.FromResult(import);
|
|
}
|
|
}
|
|
}
|
|
|
|
|