TourIAm/TIAMWebApp/Shared/Services/TransferDataService.cs

208 lines
7.3 KiB
C#

using AyCode.Core.Loggers;
using AyCode.Services.Loggers;
using System.Net.Http.Json;
using System.Text.Json;
using TIAM.Core.Loggers;
using TIAM.Entities.Transfers;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels;
using TIAMWebApp.Shared.Application.Utility;
namespace TIAMWebApp.Shared.Application.Services
{
public class TransferDataService : ITransferDataService
{
private readonly HttpClient _http;
private readonly ILogger _logger;
public TransferDataService(HttpClient http, IEnumerable<IAcLogWriterClientBase> logWriters)
{
_http = http;
_logger = new LoggerClient<TransferDataService>(logWriters.ToArray());
}
public async Task<List<TransferDestination>> GetDestinationsAsync()
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinations}";
//var url = $"{APIUrls.GetTransferDestinations}";
_logger.Info(url);
List<TransferDestination>? response = await _http.GetFromJsonAsync<List<TransferDestination>>(url);
if(response == null)
return new List<TransferDestination>();
return response;
}
public async Task<TransferDestination?> GetTransferDestinationbyCoordinatesAsync(string destinationId)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}";
return await _http.GetFromJsonAsync<TransferDestination>(url);
}
public async Task<TransferDestination?> GetTransferDestinationbyAddressAsync(string destinationId)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByAddress}";
return await _http.GetFromJsonAsync<TransferDestination>(url);
}
public async Task<TransferDestination?> CreateTransferDestination(TransferDestination model)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransferDestination}";
var response = await _http.PostAsJsonAsync(url, model);
//var result = new WizardProcessorResult();
//if (response.IsSuccessStatusCode)
//{
// result.IsSucces = true;
// result.ResultJson = await response.Content.ReadAsStringAsync();
//}
if (!response.IsSuccessStatusCode)
return null;
var result = (TransferDestination)(await response.Content.ReadFromJsonAsync(typeof(TransferDestination)))!;
return result;
}
public async Task<Transfer?> CreateTransfer(TransferWizardModel model)
{
_logger.Info("CreateTransfer called");
model.Id = Guid.NewGuid();
_logger.Debug(model.ToString());
var transfer = model.CopyToTransfer();
var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransfer}";
var response = await _http.PostAsJsonAsync(url, transfer);
//var result = new WizardProcessorResult();
//if (response.IsSuccessStatusCode)
//{
// result.IsSucces = true;
// result.ResultJson = await response.Content.ReadAsStringAsync();
//}
if (!response.IsSuccessStatusCode)
return null;
var result = (Transfer)(await response.Content.ReadFromJsonAsync(typeof(Transfer)))!;
return result;
}
public async Task<List<Transfer>?> CreateTransfers(List<TransferWizardModel> modelList)
{
List<Transfer> transferList = new List<Transfer>();
foreach (var model in modelList)
{
model.Id = Guid.NewGuid();
var transfer = model.CopyToTransfer();
transferList.Add(transfer);
}
var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateTransfers}";
var response = await _http.PostAsJsonAsync(url, transferList);
//var result = new WizardProcessorResult();
//if (response.IsSuccessStatusCode)
//{
// result.IsSucces = true;
// result.ResultJson = await response.Content.ReadAsStringAsync();
//}
if (!response.IsSuccessStatusCode)
return null;
var result = (List<Transfer>)(await response.Content.ReadFromJsonAsync(typeof(List<Transfer>)))!;
return result;
}
public async Task<TransferDestination?> UpdateTransferDestination(TransferDestination model)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.UpdateTransferDestination}";
var response = await _http.PostAsJsonAsync(url, model);
//var result = new WizardProcessorResult();
//if (response.IsSuccessStatusCode)
//{
// result.IsSucces = true;
// result.ResultJson = await response.Content.ReadAsStringAsync();
//}
if (!response.IsSuccessStatusCode)
return null;
var result = (TransferDestination)(await response.Content.ReadFromJsonAsync(typeof(TransferDestination)))!;
return result;
}
public async Task<Transfer?> GetTransferByIdAsync(Guid id)
{
Transfer resultTransfer;
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferById}";
//var url = $"{APIUrls.GetTransferDestinations}";
_logger.Info(url);
var response = await _http.PostAsJsonAsync(url, id);
if (response == null)
return new Transfer();
else
{
resultTransfer = (Transfer)(await response.Content.ReadFromJsonAsync(typeof(Transfer)));
}
return resultTransfer;
}
public async Task<List<Transfer>> GetTransfersAsync()
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransfers}";
//var url = $"{APIUrls.GetTransferDestinations}";
_logger.Info(url);
//add json settings ignore readonly properties
JsonSerializerOptions options = new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true
};
List<Transfer>? response = await _http.GetFromJsonAsync<List<Transfer>>(url, options);
if (response == null)
return new List<Transfer>();
return response;
}
public async Task<bool> UpdateTransferAsync(Transfer model)
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.UpdateTransfer}";
var response = await _http.PostAsJsonAsync(url, model);
//var result = new WizardProcessorResult();
//if (response.IsSuccessStatusCode)
//{
// result.IsSucces = true;
// result.ResultJson = await response.Content.ReadAsStringAsync();
//}
if (!response.IsSuccessStatusCode)
return false;
var result = (bool)(await response.Content.ReadFromJsonAsync(typeof(bool)))!;
return result;
}
}
}