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 logWriters) { _http = http; _logger = new LoggerClient(logWriters.ToArray()); } public async Task> GetDestinationsAsync() { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinations}"; //var url = $"{APIUrls.GetTransferDestinations}"; _logger.Info(url); List? response = await _http.GetFromJsonAsync>(url); if(response == null) return new List(); return response; } public async Task GetTransferDestinationbyCoordinatesAsync(string destinationId) { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByCoordinates}"; return await _http.GetFromJsonAsync(url); } public async Task GetTransferDestinationbyAddressAsync(string destinationId) { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinationByAddress}"; return await _http.GetFromJsonAsync(url); } public async Task 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 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?> CreateTransfers(List modelList) { List transferList = new List(); 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)(await response.Content.ReadFromJsonAsync(typeof(List)))!; return result; } public async Task 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 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> 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? response = await _http.GetFromJsonAsync>(url, options); if (response == null) return new List(); return response; } public async Task 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; } } }