2025-07-17 14:29:02 +02:00
|
|
|
using System.Text.Json;
|
2025-10-11 11:33:46 +02:00
|
|
|
using BimAI.Application.DTOModels;
|
|
|
|
|
using BimAI.Application.DTOModels.Common;
|
2025-07-17 14:29:02 +02:00
|
|
|
using Microsoft.AspNetCore.WebUtilities;
|
|
|
|
|
|
2025-10-11 11:33:46 +02:00
|
|
|
namespace BimAI.UI.Shared.Services;
|
2025-07-17 14:29:02 +02:00
|
|
|
|
|
|
|
|
public class ProductService(HttpClient httpClient)
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient = httpClient;
|
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public async Task<PagedResult<ProductDto>> GetProductsAsync(ProductFilterRequest request)
|
|
|
|
|
{
|
|
|
|
|
var queryParams = new Dictionary<string, string?>
|
|
|
|
|
{
|
|
|
|
|
["page"] = request.Page.ToString(),
|
|
|
|
|
["pageSize"] = request.PageSize.ToString(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Search))
|
|
|
|
|
{
|
|
|
|
|
queryParams["search"] = request.Search;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Name))
|
|
|
|
|
{
|
|
|
|
|
queryParams["name"] = request.Name;
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Code))
|
|
|
|
|
{
|
|
|
|
|
queryParams["code"] = request.Code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.Ean))
|
|
|
|
|
{
|
|
|
|
|
queryParams["ean"] = request.Ean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uri = QueryHelpers.AddQueryString("api/products", queryParams);
|
|
|
|
|
var response = await _httpClient.GetAsync(uri);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var result = JsonSerializer.Deserialize<PagedResult<ProductDto>>(json, _jsonOptions);
|
|
|
|
|
|
|
|
|
|
return result ?? new PagedResult<ProductDto>();
|
|
|
|
|
}
|
|
|
|
|
}
|