Fix blog category selection and empty state rendering
TaxBaik CI/CD / build-and-deploy (push) Successful in 53s
TaxBaik CI/CD / build-and-deploy (push) Successful in 53s
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
@model TaxBaik.Web.Pages.Admin.Blog.CreateModel
|
||||
@{
|
||||
ViewData["Title"] = "블로그 등록";
|
||||
ViewData["BlogCategories"] = Model.Categories;
|
||||
}
|
||||
|
||||
@await Html.PartialAsync("_BlogEditor", new TaxBaik.Web.Pages.Shared.BlogEditorModel(
|
||||
|
||||
@@ -3,12 +3,13 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using TaxBaik.Application.DTOs;
|
||||
using TaxBaik.Application.Services;
|
||||
using TaxBaik.Domain.Entities;
|
||||
using TaxBaik.Web.Services;
|
||||
|
||||
namespace TaxBaik.Web.Pages.Admin.Blog;
|
||||
|
||||
[Authorize(AuthenticationSchemes = AdminAuthDefaults.Scheme)]
|
||||
public class CreateModel(BlogService blogService) : PageModel
|
||||
public class CreateModel(BlogService blogService, CategoryService categoryService) : PageModel
|
||||
{
|
||||
[BindProperty]
|
||||
public CreateBlogPostDto Input { get; set; } = new()
|
||||
@@ -17,14 +18,47 @@ public class CreateModel(BlogService blogService) : PageModel
|
||||
Title = string.Empty
|
||||
};
|
||||
|
||||
public IActionResult OnGet() => Page();
|
||||
public IReadOnlyList<Category> Categories { get; private set; } = [];
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(CancellationToken ct)
|
||||
{
|
||||
Categories = (await EnsureCategoriesAsync(ct)).ToList();
|
||||
if (Categories.Count > 0)
|
||||
Input.CategoryId = Categories[0].Id;
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync(CancellationToken ct)
|
||||
{
|
||||
Categories = (await EnsureCategoriesAsync(ct)).ToList();
|
||||
if (!ModelState.IsValid)
|
||||
return Page();
|
||||
|
||||
await blogService.CreateAsync(Input, ct);
|
||||
return RedirectToPage("/Admin/Blog/Index");
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<Category>> EnsureCategoriesAsync(CancellationToken ct)
|
||||
{
|
||||
var categories = (await categoryService.GetAllAsync(ct)).ToList();
|
||||
if (categories.Count > 0)
|
||||
return categories;
|
||||
|
||||
var defaults = new[]
|
||||
{
|
||||
("사업자 세무", "business-tax", 1),
|
||||
("부동산 세금", "real-estate-tax", 2),
|
||||
("종합소득세", "income-tax", 3),
|
||||
("부가가치세", "vat", 4),
|
||||
("가족자산·증여", "family-asset", 5)
|
||||
};
|
||||
|
||||
foreach (var (name, slug, sortOrder) in defaults)
|
||||
{
|
||||
await categoryService.CreateAsync(name, null, ct);
|
||||
}
|
||||
|
||||
return await categoryService.GetAllAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
@{
|
||||
ViewData["Title"] = "블로그 수정";
|
||||
ViewData["BlogEditorId"] = Model.Id;
|
||||
ViewData["BlogCategories"] = Model.Categories;
|
||||
}
|
||||
|
||||
@await Html.PartialAsync("_BlogEditor", new TaxBaik.Web.Pages.Shared.BlogEditorModel(
|
||||
|
||||
@@ -3,12 +3,13 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using TaxBaik.Application.DTOs;
|
||||
using TaxBaik.Application.Services;
|
||||
using TaxBaik.Domain.Entities;
|
||||
using TaxBaik.Web.Services;
|
||||
|
||||
namespace TaxBaik.Web.Pages.Admin.Blog;
|
||||
|
||||
[Authorize(AuthenticationSchemes = AdminAuthDefaults.Scheme)]
|
||||
public class EditModel(BlogService blogService) : PageModel
|
||||
public class EditModel(BlogService blogService, CategoryService categoryService) : PageModel
|
||||
{
|
||||
[BindProperty(SupportsGet = true)]
|
||||
public int Id { get; set; }
|
||||
@@ -20,17 +21,20 @@ public class EditModel(BlogService blogService) : PageModel
|
||||
Title = string.Empty
|
||||
};
|
||||
|
||||
public IReadOnlyList<Category> Categories { get; private set; } = [];
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(CancellationToken ct)
|
||||
{
|
||||
var post = await blogService.GetByIdAsync(Id, ct);
|
||||
if (post is null)
|
||||
return NotFound();
|
||||
|
||||
Categories = (await EnsureCategoriesAsync(ct)).ToList();
|
||||
Input = new CreateBlogPostDto
|
||||
{
|
||||
Title = post.Title,
|
||||
Content = post.Content,
|
||||
CategoryId = post.CategoryId,
|
||||
CategoryId = post.CategoryId ?? 0,
|
||||
Tags = post.Tags,
|
||||
SeoTitle = post.SeoTitle,
|
||||
SeoDescription = post.SeoDescription,
|
||||
@@ -44,6 +48,7 @@ public class EditModel(BlogService blogService) : PageModel
|
||||
|
||||
public async Task<IActionResult> OnPostAsync(CancellationToken ct)
|
||||
{
|
||||
Categories = (await EnsureCategoriesAsync(ct)).ToList();
|
||||
if (!ModelState.IsValid)
|
||||
return Page();
|
||||
|
||||
@@ -59,4 +64,27 @@ public class EditModel(BlogService blogService) : PageModel
|
||||
await blogService.DeleteAsync(Id, ct);
|
||||
return RedirectToPage("/Admin/Blog/Index");
|
||||
}
|
||||
|
||||
private async Task<IEnumerable<Category>> EnsureCategoriesAsync(CancellationToken ct)
|
||||
{
|
||||
var categories = (await categoryService.GetAllAsync(ct)).ToList();
|
||||
if (categories.Count > 0)
|
||||
return categories;
|
||||
|
||||
var defaults = new[]
|
||||
{
|
||||
("사업자 세무", "business-tax", 1),
|
||||
("부동산 세금", "real-estate-tax", 2),
|
||||
("종합소득세", "income-tax", 3),
|
||||
("부가가치세", "vat", 4),
|
||||
("가족자산·증여", "family-asset", 5)
|
||||
};
|
||||
|
||||
foreach (var (name, slug, sortOrder) in defaults)
|
||||
{
|
||||
await categoryService.CreateAsync(name, null, ct);
|
||||
}
|
||||
|
||||
return await categoryService.GetAllAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user