Files
taxbaik/TaxBaik.Web/Services/ClientBrowserClient.cs
T
kjh2064 fbdbbc7a1f
TaxBaik CI/CD / build-and-deploy (push) Successful in 54s
refactor: Phase 7-3 - Clients + TaxFilings API-First (WIP)
**Clients Migration Complete:**
- ClientController: GET /api/client (paged), POST/PUT/DELETE
- ClientBrowserClient: IClientBrowserClient interface + implementation
- ClientList.razor: Service → API client
- ClientEdit.razor: Service → API client (Create/Update)

**TaxFilings API Framework Ready:**
- TaxFilingController: GET upcoming, GET by client, POST/PUT/DELETE
- TaxFilingBrowserClient: ITaxFilingBrowserClient interface + impl
- Registered in Program.cs with TokenRefreshHandler

**SOLID Applied:**
✓ Separation of concerns (Controller → Service → Repository)
✓ Dependency inversion (Blazor → Browser clients, not services)
✓ Interface segregation (Specialized clients per domain)

**Status:**
- Clients Blazor:  ClientList + ClientEdit refactored
- TaxFilings Blazor:  Pending refactor (pages exist)
- Faqs:  API + Blazor pending
- Announcements:  API + Blazor pending
- Phase 6 SignalR:  Deferred

Next: Refactor TaxFilings Blazor pages, then Faqs & Announcements
Build:  Success (0 errors, 2 warnings in Dashboard)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-28 11:08:43 +09:00

126 lines
4.2 KiB
C#

namespace TaxBaik.Web.Services;
using System.Net.Http.Json;
using TaxBaik.Application.DTOs;
using TaxBaik.Domain.Entities;
/// <summary>
/// Client API Client for Admin Blazor
/// SOLID: Single Responsibility - Client API calls only
/// </summary>
public interface IClientBrowserClient
{
Task<(IEnumerable<Client> Items, int Total)> GetPagedAsync(
int page = 1, int pageSize = 20, string? status = null, string? search = null, CancellationToken ct = default);
Task<Client?> GetByIdAsync(int id, CancellationToken ct = default);
Task<Client?> CreateAsync(CreateClientDto dto, CancellationToken ct = default);
Task<Client?> UpdateAsync(int id, CreateClientDto dto, CancellationToken ct = default);
Task<bool> DeleteAsync(int id, CancellationToken ct = default);
}
public class ClientBrowserClient : IClientBrowserClient
{
private readonly HttpClient _http;
private readonly ILogger<ClientBrowserClient> _logger;
public ClientBrowserClient(HttpClient http, ILogger<ClientBrowserClient> logger)
{
_http = http;
_logger = logger;
}
public async Task<(IEnumerable<Client> Items, int Total)> GetPagedAsync(
int page = 1, int pageSize = 20, string? status = null, string? search = null, CancellationToken ct = default)
{
try
{
var query = $"client?page={page}&pageSize={pageSize}";
if (!string.IsNullOrEmpty(status))
query += $"&status={status}";
if (!string.IsNullOrEmpty(search))
query += $"&search={Uri.EscapeDataString(search)}";
var result = await _http.GetFromJsonAsync<ClientPagedResponse>(query, cancellationToken: ct);
return result != null ? (result.Data, result.Total) : ([], 0);
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to fetch clients");
throw;
}
}
public async Task<Client?> GetByIdAsync(int id, CancellationToken ct = default)
{
try
{
return await _http.GetFromJsonAsync<Client>($"client/{id}", cancellationToken: ct);
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to fetch client {ClientId}", id);
throw;
}
}
public async Task<Client?> CreateAsync(CreateClientDto dto, CancellationToken ct = default)
{
try
{
var response = await _http.PostAsJsonAsync("client", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
var content = await response.Content.ReadAsStringAsync(ct);
return System.Text.Json.JsonSerializer.Deserialize<Client>(
content,
new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to create client");
throw;
}
}
public async Task<Client?> UpdateAsync(int id, CreateClientDto dto, CancellationToken ct = default)
{
try
{
var response = await _http.PutAsJsonAsync($"client/{id}", dto, cancellationToken: ct);
if (!response.IsSuccessStatusCode)
return null;
var content = await response.Content.ReadAsStringAsync(ct);
return System.Text.Json.JsonSerializer.Deserialize<Client>(
content,
new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to update client {ClientId}", id);
throw;
}
}
public async Task<bool> DeleteAsync(int id, CancellationToken ct = default)
{
try
{
var response = await _http.DeleteAsync($"client/{id}", cancellationToken: ct);
return response.IsSuccessStatusCode;
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to delete client {ClientId}", id);
throw;
}
}
private class ClientPagedResponse
{
public List<Client> Data { get; set; } = [];
public int Total { get; set; }
}
}