@page "/admin/blog/{id:int}/edit"
@attribute [Authorize]
@using TaxBaik.Application.DTOs
@using TaxBaik.Application.Services
@using TaxBaik.Domain.Interfaces
@inject BlogService BlogService
@inject ICategoryRepository CategoryRepository
@inject NavigationManager Navigation
@inject IJSRuntime JS
포스트 수정
Content
포스트 수정
블로그 포스트를 수정합니다.
@if (isLoading)
{
}
else if (post == null)
{
포스트를 찾을 수 없습니다.
}
else
{
}
@code {
[Parameter] public int Id { get; set; }
private Domain.Entities.BlogPost? post;
private List categories = [];
private EditPostModel model = new();
private bool isLoading = true;
private string CategoryIdText { get => model.CategoryId?.ToString() ?? ""; set => model.CategoryId = int.TryParse(value, out var id) ? id : null; }
protected override async Task OnInitializedAsync()
{
try
{
post = await BlogService.GetByIdAsync(Id);
if (post != null)
{
categories = (await CategoryRepository.GetAllAsync()).ToList();
MapPostToModel(post);
}
}
catch (Exception ex)
{
await JS.InvokeVoidAsync("alert", $"포스트 로드 실패: {ex.Message}");
}
finally
{
isLoading = false;
}
}
private void MapPostToModel(Domain.Entities.BlogPost post)
{
model.Title = post.Title;
model.Content = post.Content;
model.CategoryId = post.CategoryId;
model.Tags = post.Tags;
model.SeoTitle = post.SeoTitle;
model.SeoDescription = post.SeoDescription;
model.IsPublished = post.IsPublished;
}
private async Task SavePost()
{
if (post == null) return;
try
{
await BlogService.UpdateAsync(post.Id, new CreateBlogPostDto
{
Title = model.Title,
Content = model.Content,
CategoryId = model.CategoryId,
Tags = model.Tags,
SeoTitle = model.SeoTitle,
SeoDescription = model.SeoDescription,
IsPublished = model.IsPublished
});
await JS.InvokeVoidAsync("alert", "포스트가 저장되었습니다.");
Navigation.NavigateTo("/taxbaik/admin/blog");
}
catch (ValidationException ex)
{
await JS.InvokeVoidAsync("alert", ex.Message);
}
}
private async Task DeletePost()
{
if (post == null) return;
if (!await JS.InvokeAsync("confirm", "정말 삭제하시겠습니까? 이 작업은 취소할 수 없습니다.")) return;
await BlogService.DeleteAsync(post.Id);
await JS.InvokeVoidAsync("alert", "포스트가 삭제되었습니다.");
Navigation.NavigateTo("/taxbaik/admin/blog");
}
private class EditPostModel
{
public string Title { get; set; } = "";
public string Content { get; set; } = "";
public int? CategoryId { get; set; }
public string? Tags { get; set; }
public string? SeoTitle { get; set; }
public string? SeoDescription { get; set; }
public bool IsPublished { get; set; }
}
}