68 lines
1.9 KiB
C#
68 lines
1.9 KiB
C#
using maintenance.Data;
|
|
using maintenance.Data.Entities;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace maintenance.Pages.Admin.Templates;
|
|
|
|
[Authorize(Policy = "AdminOnly")]
|
|
public sealed class IndexModel : PageModel
|
|
{
|
|
private readonly AppDbContext _dbContext;
|
|
|
|
public IndexModel(AppDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public IReadOnlyList<ManagedPageEntity> Templates { get; private set; } = [];
|
|
[TempData]
|
|
public string? StatusMessage { get; set; }
|
|
|
|
public async Task OnGetAsync()
|
|
{
|
|
Templates = await _dbContext.ManagedPages
|
|
.AsNoTracking()
|
|
.OrderBy(t => t.PageKind)
|
|
.ThenByDescending(t => t.IsDefaultFallback)
|
|
.ThenBy(t => t.Key)
|
|
.ToListAsync(HttpContext.RequestAborted);
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteAsync(Guid id)
|
|
{
|
|
var page = await _dbContext.ManagedPages
|
|
.FirstOrDefaultAsync(t => t.Id == id, HttpContext.RequestAborted);
|
|
|
|
if (page is null)
|
|
{
|
|
StatusMessage = "Page was not found.";
|
|
return RedirectToPage();
|
|
}
|
|
|
|
if (page.IsDefaultFallback)
|
|
{
|
|
StatusMessage = "Default template pages cannot be deleted.";
|
|
return RedirectToPage();
|
|
}
|
|
|
|
_dbContext.ManagedPages.Remove(page);
|
|
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
|
|
|
StatusMessage = $"Deleted page '{page.Key}'.";
|
|
return RedirectToPage();
|
|
}
|
|
|
|
public string GetPreviewUrl(ManagedPageEntity page)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(page.Stub))
|
|
{
|
|
return $"/{page.PageKind}?stub={Uri.EscapeDataString(page.Stub)}";
|
|
}
|
|
|
|
return $"/{page.PageKind}?page={Uri.EscapeDataString(page.Key)}";
|
|
}
|
|
}
|