Files

44 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SoroushAsadi.Services;
namespace SoroushAsadi.Pages.Admin.Sections;
[Authorize]
public class SectionEditModel(ContentService content) : PageModel
{
[BindProperty(SupportsGet = true)]
public string SectionKey { get; set; } = "";
public string CurrentJson { get; private set; } = "{\n \"fa\": {},\n \"en\": {}\n}";
public string Message { get; private set; } = "";
public void OnGet()
{
var node = content.GetSection(SectionKey);
if (node is not null)
CurrentJson = node.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
}
public IActionResult OnPost(string json)
{
try
{
// Validate JSON
JsonDocument.Parse(json);
content.SaveSection(SectionKey, json);
Message = "Saved.";
CurrentJson = json;
return Page();
}
catch (JsonException ex)
{
Message = $"Invalid JSON: {ex.Message}";
CurrentJson = json;
return Page();
}
}
}