Files
taxbaik/TaxBaik.Web/Components/Admin/Pages/Blog/BlogList.razor
T

92 lines
3.1 KiB
Plaintext

@page "/admin/blog"
@attribute [Authorize]
@inject IApiClient ApiClient
@inject DialogService DialogService
@inject ISnackbar Snackbar
<PageTitle>블로그 관리</PageTitle>
<div class="mb-4 d-flex justify-content-between align-items-center">
<MudText Typo="Typo.h5">📝 블로그 관리</MudText>
<MudButton Variant="Variant.Filled" Color="Color.Primary"
Href="/taxbaik/admin/blog/create">새 포스트</MudButton>
</div>
<MudDataGrid Items="@posts" Striped="true" Hoverable="true" Loading="@isLoading">
<Columns>
<PropertyColumn Property="x => x.Title" Title="제목" />
<PropertyColumn Property="x => x.IsPublished" Title="발행">
<CellTemplate Context="cell">
<MudCheckBox T="bool" Value="@cell.Item.IsPublished"
ValueChanged="@(async (bool value) => await TogglePublish(cell.Item, value))" />
</CellTemplate>
</PropertyColumn>
<PropertyColumn Property="x => x.ViewCount" Title="조회수" />
<PropertyColumn Property="x => x.CreatedAt" Title="작성일" Format="yyyy-MM-dd" />
<TemplateColumn>
<CellTemplate Context="cell">
<MudButton Variant="Variant.Text" Color="Color.Primary"
Href="@($"/taxbaik/admin/blog/{cell.Item.Id}/edit")">수정</MudButton>
<MudButton Variant="Variant.Text" Color="Color.Error"
@onclick="@(async () => await DeletePost(cell.Item.Id))">삭제</MudButton>
</CellTemplate>
</TemplateColumn>
</Columns>
</MudDataGrid>
@code {
private List<TaxBaik.Domain.Entities.BlogPost> posts = [];
private bool isLoading = true;
protected override async Task OnInitializedAsync()
{
await LoadPosts();
}
private async Task LoadPosts()
{
isLoading = true;
try
{
var items = await ApiClient.GetAsync<List<TaxBaik.Domain.Entities.BlogPost>>("blog/admin/all");
posts = items ?? [];
}
catch { }
isLoading = false;
}
private async Task TogglePublish(TaxBaik.Domain.Entities.BlogPost post, bool isPublished)
{
var previous = post.IsPublished;
post.IsPublished = isPublished;
var result = await ApiClient.PutAsync<TaxBaik.Domain.Entities.BlogPost>($"blog/{post.Id}", new
{
post.Title,
post.Content,
post.CategoryId,
post.Tags,
post.SeoTitle,
post.SeoDescription,
post.ThumbnailUrl,
IsPublished = isPublished,
post.AuthorId
});
if (result == null)
{
post.IsPublished = previous;
Snackbar.Add("발행 상태 변경에 실패했습니다.", Severity.Error);
return;
}
Snackbar.Add("발행 상태가 변경되었습니다.", Severity.Success);
}
private async Task DeletePost(int postId)
{
await ApiClient.DeleteAsync($"blog/{postId}");
Snackbar.Add("포스트가 삭제되었습니다.", Severity.Success);
await LoadPosts();
}
}