54367696dc
Architecture: - Admin UI: /admin (Standalone Blazor WebAssembly, 219 WASM files) - Portal: /portal (Razor Pages, Cookie/OAuth auth) - Homepage: / (Razor Pages, SSR) - API: /api (FastEndpoints + JWT) SEO: - Sitemap: Public content only (blog, FAQ, announcements, contact) - robots.txt: Exclude /admin and /portal, reference production domain - Naver verification: naverb1813cd79ddc2ded5c5291fca5cb46c2.html ready Technical: - TaxBaik.Web.Client: StaticWebAssetBasePath=admin - Server Program.cs: UseBlazorFrameworkFiles + MapFallback for SPA routing - base href="/admin/" for client-side navigation - blazor.webassembly.js (standalone, not web.js) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
127 lines
4.3 KiB
C#
127 lines
4.3 KiB
C#
namespace TaxBaik.Web.Client.Components.Admin.Services;
|
|
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using TaxBaik.Application.DTOs;
|
|
using TaxBaik.Domain.Entities;
|
|
|
|
public interface IAnnouncementBrowserClient
|
|
{
|
|
Task<IEnumerable<Announcement>> GetAllAsync(CancellationToken ct = default);
|
|
Task<Announcement?> GetByIdAsync(int id, CancellationToken ct = default);
|
|
Task<Announcement?> CreateAsync(AnnouncementDto dto, CancellationToken ct = default);
|
|
Task<Announcement?> UpdateAsync(int id, AnnouncementDto dto, CancellationToken ct = default);
|
|
Task<bool> DeleteAsync(int id, CancellationToken ct = default);
|
|
}
|
|
|
|
public class AnnouncementBrowserClient : IAnnouncementBrowserClient
|
|
{
|
|
private readonly HttpClient _http;
|
|
private readonly ILogger<AnnouncementBrowserClient> _logger;
|
|
private readonly ITokenStore _tokenStore;
|
|
|
|
public AnnouncementBrowserClient(HttpClient http, ILogger<AnnouncementBrowserClient> logger, ITokenStore tokenStore)
|
|
{
|
|
_http = http;
|
|
_logger = logger;
|
|
_tokenStore = tokenStore;
|
|
}
|
|
|
|
private void EnsureAuthHeader()
|
|
{
|
|
if (!string.IsNullOrEmpty(_tokenStore.AccessToken))
|
|
_http.DefaultRequestHeaders.Authorization = new("Bearer", _tokenStore.AccessToken);
|
|
else
|
|
_http.DefaultRequestHeaders.Authorization = null;
|
|
}
|
|
|
|
public async Task<IEnumerable<Announcement>> GetAllAsync(CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
var result = await _http.GetFromJsonAsync<AnnouncementListResponse>("announcement", cancellationToken: ct);
|
|
return result?.Data ?? [];
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to fetch announcements");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Announcement?> GetByIdAsync(int id, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
return await _http.GetFromJsonAsync<Announcement>($"announcement/{id}", cancellationToken: ct);
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to fetch announcement {AnnouncementId}", id);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Announcement?> CreateAsync(AnnouncementDto dto, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
var response = await _http.PostAsJsonAsync("announcement", dto, cancellationToken: ct);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
|
|
var content = await response.Content.ReadAsStringAsync(ct);
|
|
return System.Text.Json.JsonSerializer.Deserialize<Announcement>(
|
|
content,
|
|
new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to create announcement");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Announcement?> UpdateAsync(int id, AnnouncementDto dto, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
var response = await _http.PutAsJsonAsync($"announcement/{id}", dto, cancellationToken: ct);
|
|
if (!response.IsSuccessStatusCode) return null;
|
|
|
|
var content = await response.Content.ReadAsStringAsync(ct);
|
|
return System.Text.Json.JsonSerializer.Deserialize<Announcement>(
|
|
content,
|
|
new System.Text.Json.JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to update announcement {AnnouncementId}", id);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
EnsureAuthHeader();
|
|
var response = await _http.DeleteAsync($"announcement/{id}", cancellationToken: ct);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogError(ex, "Failed to delete announcement {AnnouncementId}", id);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private class AnnouncementListResponse
|
|
{
|
|
public List<Announcement> Data { get; set; } = [];
|
|
}
|
|
}
|