using System.Net.Http.Json; using System.Text.Json; 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 LogToBrowserConsole logToBrowserConsole; public TransferDataService(HttpClient http, LogToBrowserConsole logToBrowserConsole) { this.http = http; this.logToBrowserConsole = logToBrowserConsole; } public async Task> GetDestinationsAsync() { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferDestinations}"; //var url = $"{APIUrls.GetTransferDestinations}"; logToBrowserConsole.LogToBC(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) { logToBrowserConsole.LogToBC("CreateTransfer called"); logToBrowserConsole.LogToBC(model.PickupAddress); logToBrowserConsole.LogToBC(model.Destination); logToBrowserConsole.LogToBC(model.EmailAddress); logToBrowserConsole.LogToBC(model.PhoneNumber); logToBrowserConsole.LogToBC(model.TripDate.ToString()); logToBrowserConsole.LogToBC(model.NumberOfPassengers.ToString()); model.Id = Guid.NewGuid(); 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) { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransferById}"; //var url = $"{APIUrls.GetTransferDestinations}"; logToBrowserConsole.LogToBC(url); Transfer? response = await http.GetFromJsonAsync(url); if (response == null) return new Transfer(); return response; } public async Task> GetTransfersAsync() { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetTransfers}"; //var url = $"{APIUrls.GetTransferDestinations}"; logToBrowserConsole.LogToBC(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; } } }