using AyCode.Interfaces.StorageHandlers; using Newtonsoft.Json; using System.Net.Http.Json; using AyCode.Services.Loggers; using AyCode.Services.SignalRs; using TIAM.Entities.Products; using TIAM.Entities.ServiceProviders; using TIAM.Entities.Users; using TIAMWebApp.Shared.Application.Interfaces; using TIAMWebApp.Shared.Application.Models; using TIAMWebApp.Shared.Application.Models.ClientSide; using TIAMWebApp.Shared.Application.Utility; using TIAM.Services; using Microsoft.Extensions.DependencyInjection; namespace TIAMWebApp.Shared.Application.Services { public class ServiceProviderDataService : IServiceProviderDataService { private readonly HttpClient http; private readonly ISecureStorageHandler secureStorageHandler; private readonly LoggerClient _logger; private AdminSignalRClient _adminSignalRClient; public ServiceProviderDataService(HttpClient http, ISecureStorageHandler secureStorageHandler, AdminSignalRClient adminSignalRClient, IEnumerable logWriters) { this.http = http; this.secureStorageHandler = secureStorageHandler; _adminSignalRClient = adminSignalRClient; _logger = new LoggerClient(logWriters.ToArray()); } //22. public Task CreateUserProductMappingAsync(UserProductMapping userProductMapping) { throw new NotImplementedException(); } //19. public async Task CreateProductAsync(Product product) { var result = await _adminSignalRClient.PostDataAsync(SignalRTags.AddProduct, product); return result; //var url = $"{Setting.ApiBaseUrl}/{APIUrls.AddProduct}"; //var response = await http.PostAsJsonAsync(url, product); //if (response != null) //{ // var resultCompany = await response.Content.ReadFromJsonAsync(typeof(Product)); // if (resultCompany != null) // { // return (resultCompany as Product)!; // } // else // { // return null; // } //} //else //{ // return null; //} } //15. public async Task CreateServiceProviderAsync(Company serviceProvider) { var url = $"{Setting.ApiBaseUrl}/{APIUrls.CreateServiceProvider}"; var response = await http.PostAsJsonAsync(url, serviceProvider); if (response != null) { var resultCompany = await response.Content.ReadFromJsonAsync(typeof(Company)); if(resultCompany != null) { return (resultCompany as Company)!; } else { return null; } } else { return null; } } //21. public Task DeleteProductAsync(Guid productId) { throw new NotImplementedException(); } //13. public Task DeleteServiceProviderAsync(Guid serviceProviderId) { throw new NotImplementedException(); } //23. public Task> GetUserProductMappingsByProductIdAsync(Guid productId) { throw new NotImplementedException(); } public async Task GetUserProductMappingByIdAsync(Guid userProductMappingId) { var result = await _adminSignalRClient.GetByIdAsync(SignalRTags.GetUserProductMappingById, userProductMappingId); return result; } //17. public Task GetPropertiesByOwnerIdAsync(Guid id, Action?> callback) { return _adminSignalRClient.GetByIdAsync>(SignalRTags.GetPropertiesByOwnerId, response => { if (response.Status == SignalResponseStatus.Error) callback.Invoke(null); _logger.DetailConditional($"companyPropertiesByOwner async: {string.Join("; ", response.ResponseData!.Values)}"); callback.Invoke(response.ResponseData); return Task.CompletedTask; }, id); } //17. public async Task?> GetPropertiesByOwnerIdAsync(Guid id) { var companyPropertiesByOwner = await _adminSignalRClient.GetByIdAsync>(SignalRTags.GetPropertiesByOwnerId, id); if (companyPropertiesByOwner != null) _logger.DetailConditional($"companyPropertiesByOwner: {string.Join("; ", companyPropertiesByOwner.Values)}"); return companyPropertiesByOwner; //var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetServiceProvidersByOwnerId}"; //var response = await http.PostAsJsonAsync(url, id); //if (response.IsSuccessStatusCode) //{ // var result = await response.Content.ReadFromJsonAsync>(); // string resultString = ""; // foreach (var item in result) // { // resultString += item.Value + ", "; // } // _logger.DetailConditional(resultString); // return result; //} //else //{ // return null; //} } //18. public async Task GetServiceProviderByIdAsync(Guid id) { var company = await _adminSignalRClient.GetByIdAsync(SignalRTags.GetCompany, id); if (company != null) _logger.DetailConditional($"company: {company.Name}"); return company; } //16. public async Task> GetServiceProvidersAsync() { var companies = await _adminSignalRClient.GetAllAsync>(SignalRTags.GetCompanies); if (companies != null) _logger.DetailConditional($"companies: {string.Join("; ", companies.Count)}"); return companies ?? []; } //24. public Task RemoveUserProductMappingsByContextIdAsync(Guid productId) { throw new NotImplementedException(); } //20. public Task UpdateProductAsync(Product product) { throw new NotImplementedException(); } //14. public async Task UpdateServiceProviderAsync(Company company) { var result = await _adminSignalRClient.PostDataAsync(SignalRTags.UpdateCompany, company); return result; } public async Task GetQRCodeByProductIdAsync(Guid productId) { var url = APIUrls.GetQrCodeByProductId; var response = await http.PostAsJsonAsync(url, productId); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { return null; } } public async Task GetQRCodeByProductIdAndOwnerAffiliateIdAsync(Guid[] Ids) { var url = APIUrls.GetQrCodeByProductIdAndOwnerAffiliateId; var response = await http.PostAsJsonAsync(url, Ids); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { return null; } } public async Task> GetProductsForServiceProviderAsync(Guid serviceProviderId) { var url = APIUrls.GetProductsByServiceProviderId; var response = await http.PostAsJsonAsync(url, serviceProviderId); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); //_logger.Detail("Json: " + result); var data = JsonConvert.DeserializeObject>(result); if (data != null) { return data; } else { return null; } } else { return null; } } public async Task> GetAllProductsAsync() { var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetAllProducts}"; var response = await http.GetFromJsonAsync(url, typeof (List)); if (response != null) { return (List)response; } else { return null; } } public async Task GetProductByIdAsync(Guid id) { var result = await _adminSignalRClient.GetByIdAsync(SignalRTags.GetProductById, id); //var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetProductById}"; //var response = await http.GetFromJsonAsync(url, typeof(Product)); //if (response != null) //{ // return (Product)response; //} //else //{ // return null; //} return result; } } }