2025-11-06 10:20:00 +01:00
|
|
|
using System.Net.Http.Json;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using DiunaBI.Application.DTOModels;
|
|
|
|
|
using DiunaBI.Application.DTOModels.Common;
|
|
|
|
|
|
|
|
|
|
namespace DiunaBI.UI.Shared.Services;
|
|
|
|
|
|
|
|
|
|
public class LayerService
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
|
|
|
|
|
public LayerService(HttpClient httpClient)
|
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public async Task<PagedResult<LayerDto>> GetLayersAsync(LayerFilterRequest filterRequest)
|
|
|
|
|
{
|
2025-11-19 16:42:02 +01:00
|
|
|
// Calculate start index from page number (page 1 = start 0, page 2 = start 50, etc.)
|
|
|
|
|
var start = (filterRequest.Page - 1) * filterRequest.PageSize;
|
2025-11-19 17:21:14 +01:00
|
|
|
var query = $"Layers?start={start}&limit={filterRequest.PageSize}";
|
2025-11-19 16:42:02 +01:00
|
|
|
|
2025-11-06 10:20:00 +01:00
|
|
|
if (!string.IsNullOrEmpty(filterRequest.Search))
|
|
|
|
|
query += $"&name={Uri.EscapeDataString(filterRequest.Search)}";
|
2025-11-19 16:42:02 +01:00
|
|
|
|
2025-12-01 13:21:45 +01:00
|
|
|
if (filterRequest.Type.HasValue)
|
|
|
|
|
query += $"&type={(int)filterRequest.Type.Value}";
|
2025-11-06 10:20:00 +01:00
|
|
|
|
|
|
|
|
var response = await _httpClient.GetAsync(query);
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2025-11-19 16:42:02 +01:00
|
|
|
|
2025-11-06 10:20:00 +01:00
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var result = JsonSerializer.Deserialize<PagedResult<LayerDto>>(json, _jsonOptions);
|
2025-11-19 16:42:02 +01:00
|
|
|
|
2025-11-06 10:20:00 +01:00
|
|
|
return result ?? new PagedResult<LayerDto>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<LayerDto?> GetLayerByIdAsync(Guid id)
|
|
|
|
|
{
|
2025-11-19 17:21:14 +01:00
|
|
|
var response = await _httpClient.GetAsync($"Layers/{id}");
|
2025-11-06 10:20:00 +01:00
|
|
|
|
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return await response.Content.ReadFromJsonAsync<LayerDto>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> UpdateRecordsAsync(Guid layerId, List<RecordDto> records)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement if needed - backend doesn't have PUT endpoint yet
|
|
|
|
|
// For now we don't need it for read-only view
|
|
|
|
|
return await Task.FromResult(false);
|
|
|
|
|
}
|
|
|
|
|
}
|