Files
BimAI/BimAI.UI.Shared/Services/ProductService.cs
Michał Zieliński 6d2c46d971
Some checks failed
Build Bimix / Build WebAPI and WebUI (push) Failing after 12s
App name refactor
2025-10-11 11:33:46 +02:00

52 lines
1.6 KiB
C#

using System.Text.Json;
using BimAI.Application.DTOModels;
using BimAI.Application.DTOModels.Common;
using Microsoft.AspNetCore.WebUtilities;
namespace BimAI.UI.Shared.Services;
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>();
}
}