diff --git a/TaxBaik.Application/Services/BlogService.cs b/TaxBaik.Application/Services/BlogService.cs index 013cf5a..de43148 100644 --- a/TaxBaik.Application/Services/BlogService.cs +++ b/TaxBaik.Application/Services/BlogService.cs @@ -9,6 +9,9 @@ using Microsoft.Extensions.Caching.Memory; public class BlogService(IBlogPostRepository repository, IMemoryCache memoryCache) { + public async Task GetByIdAsync(int id, CancellationToken ct = default) => + await repository.GetByIdAsync(id, ct); + public async Task GetBySlugAsync(string slug, CancellationToken ct = default) => await repository.GetBySlugAsync(slug, ct); diff --git a/TaxBaik.Web/Components/Admin/Pages/Blog/BlogCreate.razor b/TaxBaik.Web/Components/Admin/Pages/Blog/BlogCreate.razor index 86805ed..fabf2fb 100644 --- a/TaxBaik.Web/Components/Admin/Pages/Blog/BlogCreate.razor +++ b/TaxBaik.Web/Components/Admin/Pages/Blog/BlogCreate.razor @@ -19,7 +19,7 @@ 취소 - + diff --git a/TaxBaik.Web/Components/Admin/Pages/Blog/BlogEdit.razor b/TaxBaik.Web/Components/Admin/Pages/Blog/BlogEdit.razor new file mode 100644 index 0000000..9b7bdd5 --- /dev/null +++ b/TaxBaik.Web/Components/Admin/Pages/Blog/BlogEdit.razor @@ -0,0 +1,187 @@ +@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 ISnackbar Snackbar +@inject IDialogService DialogService + +포스트 수정 + +
+
+ Content + 포스트 수정 + 블로그 포스트를 수정합니다. +
+ 취소 +
+ +@if (isLoading) +{ + +} +else if (post == null) +{ + 포스트를 찾을 수 없습니다. +} +else +{ + + + + + + @foreach (var category in categories) + { + @category.Name + } + + + + + + + + + + + + +
+ 저장 + 삭제 +
+
+
+} + +@code { + [Parameter] + public int Id { get; set; } + + private MudForm? form; + private Domain.Entities.BlogPost? post; + private List categories = []; + private EditPostModel model = new(); + private bool isLoading = true; + + protected override async Task OnInitializedAsync() + { + try + { + post = await BlogService.GetByIdAsync(Id); + if (post != null) + { + categories = (await CategoryRepository.GetAllAsync()).ToList(); + MapPostToModel(post); + } + } + catch (Exception ex) + { + Snackbar.Add($"포스트 로드 실패: {ex.Message}", Severity.Error); + } + 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 void GoBack() + { + Navigation.NavigateTo("/taxbaik/admin/blog"); + } + + private async Task SavePost() + { + if (form == null || post == null) + return; + + await form.Validate(); + if (!form.IsValid) + 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 + }); + + Snackbar.Add("포스트가 저장되었습니다.", Severity.Success); + Navigation.NavigateTo("/taxbaik/admin/blog"); + } + catch (ValidationException ex) + { + Snackbar.Add(ex.Message, Severity.Error); + } + catch (Exception ex) + { + Snackbar.Add($"저장 실패: {ex.Message}", Severity.Error); + } + } + + private async Task DeletePost() + { + if (post == null) + return; + + var result = await DialogService.ShowMessageBox( + "포스트 삭제", + "정말 삭제하시겠습니까? 이 작업은 취소할 수 없습니다.", + "삭제", "취소"); + + if (result != true) + return; + + try + { + await BlogService.DeleteAsync(post.Id); + Snackbar.Add("포스트가 삭제되었습니다.", Severity.Success); + Navigation.NavigateTo("/taxbaik/admin/blog"); + } + catch (Exception ex) + { + Snackbar.Add($"삭제 실패: {ex.Message}", Severity.Error); + } + } + + 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; } + } +}