121 lines
4.3 KiB
C#
121 lines
4.3 KiB
C#
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)
|
|
{
|
|
// Calculate start index from page number (page 1 = start 0, page 2 = start 50, etc.)
|
|
var start = (filterRequest.Page - 1) * filterRequest.PageSize;
|
|
var query = $"Layers?start={start}&limit={filterRequest.PageSize}";
|
|
|
|
if (!string.IsNullOrEmpty(filterRequest.Search))
|
|
query += $"&name={Uri.EscapeDataString(filterRequest.Search)}";
|
|
|
|
if (filterRequest.Type.HasValue)
|
|
query += $"&type={(int)filterRequest.Type.Value}";
|
|
|
|
var response = await _httpClient.GetAsync(query);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var json = await response.Content.ReadAsStringAsync();
|
|
var result = JsonSerializer.Deserialize<PagedResult<LayerDto>>(json, _jsonOptions);
|
|
|
|
return result ?? new PagedResult<LayerDto>();
|
|
}
|
|
|
|
public async Task<LayerDto?> GetLayerByIdAsync(Guid id)
|
|
{
|
|
var response = await _httpClient.GetAsync($"Layers/{id}");
|
|
|
|
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);
|
|
}
|
|
|
|
public async Task<RecordDto?> CreateRecordAsync(Guid layerId, RecordDto record)
|
|
{
|
|
var response = await _httpClient.PostAsJsonAsync($"Layers/{layerId}/records", record);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync();
|
|
Console.Error.WriteLine($"CreateRecordAsync failed: {response.StatusCode} - {error}");
|
|
return null;
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<RecordDto>();
|
|
}
|
|
|
|
public async Task<RecordDto?> UpdateRecordAsync(Guid layerId, Guid recordId, RecordDto record)
|
|
{
|
|
var response = await _httpClient.PutAsJsonAsync($"Layers/{layerId}/records/{recordId}", record);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync();
|
|
Console.Error.WriteLine($"UpdateRecordAsync failed: {response.StatusCode} - {error}");
|
|
return null;
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<RecordDto>();
|
|
}
|
|
|
|
public async Task<bool> DeleteRecordAsync(Guid layerId, Guid recordId)
|
|
{
|
|
var response = await _httpClient.DeleteAsync($"Layers/{layerId}/records/{recordId}");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
|
|
public async Task<List<RecordHistoryDto>> GetRecordHistoryAsync(Guid layerId, Guid recordId)
|
|
{
|
|
var response = await _httpClient.GetAsync($"Layers/{layerId}/records/{recordId}/history");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync();
|
|
Console.Error.WriteLine($"GetRecordHistoryAsync failed: {response.StatusCode} - {error}");
|
|
return new List<RecordHistoryDto>();
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<List<RecordHistoryDto>>() ?? new List<RecordHistoryDto>();
|
|
}
|
|
|
|
public async Task<List<DeletedRecordDto>> GetDeletedRecordsAsync(Guid layerId)
|
|
{
|
|
var response = await _httpClient.GetAsync($"Layers/{layerId}/records/deleted");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var error = await response.Content.ReadAsStringAsync();
|
|
Console.Error.WriteLine($"GetDeletedRecordsAsync failed: {response.StatusCode} - {error}");
|
|
return new List<DeletedRecordDto>();
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<List<DeletedRecordDto>>() ?? new List<DeletedRecordDto>();
|
|
}
|
|
} |