TourIAm/TIAMWebApp/Shared/Services/ServiceProviderDataService.cs

292 lines
9.6 KiB
C#

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<ServiceProviderDataService> _logger;
private AdminSignalRClient _adminSignalRClient;
public ServiceProviderDataService(HttpClient http, ISecureStorageHandler secureStorageHandler, AdminSignalRClient adminSignalRClient, IEnumerable<IAcLogWriterClientBase> logWriters)
{
this.http = http;
this.secureStorageHandler = secureStorageHandler;
_adminSignalRClient = adminSignalRClient;
_logger = new LoggerClient<ServiceProviderDataService>(logWriters.ToArray());
}
//22.
public Task<bool> CreateUserProductMappingAsync(UserProductMapping userProductMapping)
{
throw new NotImplementedException();
}
//19.
public async Task<Product> 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<Company> 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<bool> DeleteServiceProviderAsync(Guid serviceProviderId)
{
throw new NotImplementedException();
}
//23.
public Task<List<UserProductMapping>> GetUserProductMappingsByProductIdAsync(Guid productId)
{
throw new NotImplementedException();
}
public async Task<UserProductMapping> GetUserProductMappingByIdAsync(Guid userProductMappingId)
{
var result = await _adminSignalRClient.GetByIdAsync<UserProductMapping>(SignalRTags.GetUserProductMappingById, userProductMappingId);
return result;
}
//17.
public Task GetPropertiesByOwnerIdAsync(Guid id, Action<Dictionary<Guid, string>?> callback)
{
return _adminSignalRClient.GetByIdAsync<Dictionary<Guid, string>>(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<Dictionary<Guid, string>?> GetPropertiesByOwnerIdAsync(Guid id)
{
var companyPropertiesByOwner = await _adminSignalRClient.GetByIdAsync<Dictionary<Guid, string>>(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<Dictionary<Guid, string>>();
// string resultString = "";
// foreach (var item in result)
// {
// resultString += item.Value + ", ";
// }
// _logger.DetailConditional(resultString);
// return result;
//}
//else
//{
// return null;
//}
}
//18.
public async Task<Company?> GetServiceProviderByIdAsync(Guid id)
{
var company = await _adminSignalRClient.GetByIdAsync<Company>(SignalRTags.GetCompany, id);
if (company != null) _logger.DetailConditional($"company: {company.Name}");
return company;
}
//16.
public async Task<List<Company>> GetServiceProvidersAsync()
{
var companies = await _adminSignalRClient.GetAllAsync<List<Company>>(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<bool> UpdateProductAsync(Product product)
{
throw new NotImplementedException();
}
//14.
public async Task<Company> UpdateServiceProviderAsync(Company company)
{
var result = await _adminSignalRClient.PostDataAsync(SignalRTags.UpdateCompany, company);
return result;
}
public async Task<string> 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<string> 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<IEnumerable<Product>> 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<IEnumerable<Product>>(result);
if (data != null)
{
return data;
}
else {
return null;
}
}
else
{
return null;
}
}
public async Task<List<Product>> GetAllProductsAsync()
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetAllProducts}";
var response = await http.GetFromJsonAsync(url, typeof (List<Product>));
if (response != null)
{
return (List<Product>)response;
}
else
{
return null;
}
}
public async Task<Product> GetProductByIdAsync(Guid id)
{
var result = await _adminSignalRClient.GetByIdAsync<Product>(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;
}
}
}