Files
soroushasadi/Pages/Admin/Posts/Edit.cshtml.cs
T

72 lines
2.5 KiB
C#
Raw Normal View History

using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SoroushAsadi.Services;
namespace SoroushAsadi.Pages.Admin.Posts;
[Authorize]
public class PostEditModel(ContentService content) : PageModel
{
[BindProperty(SupportsGet = true)]
public string Slug { get; set; } = "";
public string CurrentBody { get; private set; } = "";
public string Message { get; private set; } = "";
public bool HasOverride { get; private set; }
// Known default bodies live in Blog/Post.cshtml.cs (DefaultBodies)
private static readonly Dictionary<string, string> _defaults = new()
{
["rag-eval-framework"] = SoroushAsadi.Pages.Blog.DefaultBodies.RagEval,
["agentic-n8n-patterns"] = SoroushAsadi.Pages.Blog.DefaultBodies.N8nPatterns,
["vertex-cost-control"] = SoroushAsadi.Pages.Blog.DefaultBodies.VertexCost,
["k8s-llm-inference"] = SoroushAsadi.Pages.Blog.DefaultBodies.K8sInference,
["flutter-on-device-ai"] = SoroushAsadi.Pages.Blog.DefaultBodies.FlutterAI,
["enterprise-ai-roadmap"] = SoroushAsadi.Pages.Blog.DefaultBodies.EnterpriseRoadmap,
};
public void OnGet()
{
var overrides = content.GetPostOverrides();
if (overrides.TryGetValue(Slug, out var node) && node["body"]?.GetValue<string>() is { } body)
{
CurrentBody = body;
HasOverride = true;
}
else
{
CurrentBody = _defaults.GetValueOrDefault(Slug, "");
}
}
public IActionResult OnPost(string body, string? reset)
{
if (reset == "1")
{
// Remove override — default body shows through
var existing = content.GetPostOverrides();
existing.Remove(Slug);
// Rebuild the posts JSON without this slug
var obj = new JsonObject();
foreach (var kv in existing) obj[kv.Key] = kv.Value.DeepClone();
content.SaveSection(ContentService.PostsKey, obj.ToJsonString());
Message = "Reset to default.";
HasOverride = false;
CurrentBody = _defaults.GetValueOrDefault(Slug, "");
return Page();
}
content.SavePost(Slug, new JsonObject { ["body"] = body });
HasOverride = true;
CurrentBody = body;
Message = "Saved.";
return Page();
}
}
// Re-export DefaultBodies from the Blog page so this page can use them