adminUI
This commit is contained in:
@@ -7,5 +7,6 @@
|
||||
<div class="text-center">
|
||||
<h1 class="display-5">Admin Console</h1>
|
||||
<p>This page is protected by the <code>AdminOnly</code> policy.</p>
|
||||
<p><a asp-page="/Admin/Templates/Index">Manage Templates</a></p>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
@page "{id:guid}"
|
||||
@model maintenance.Pages.Admin.Templates.EditModel
|
||||
@{
|
||||
ViewData["Title"] = "Edit Template";
|
||||
}
|
||||
|
||||
<h1>Edit Template</h1>
|
||||
|
||||
<form method="post">
|
||||
<input asp-for="Input.Id" type="hidden" />
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Name" class="form-label"></label>
|
||||
<input asp-for="Input.Name" class="form-control" readonly />
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Title" class="form-label"></label>
|
||||
<input asp-for="Input.Title" class="form-control" />
|
||||
<span asp-validation-for="Input.Title" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Body" class="form-label"></label>
|
||||
<textarea asp-for="Input.Body" class="form-control" rows="5"></textarea>
|
||||
<span asp-validation-for="Input.Body" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.SupportContactBlock" class="form-label"></label>
|
||||
<textarea asp-for="Input.SupportContactBlock" class="form-control" rows="6"></textarea>
|
||||
<span asp-validation-for="Input.SupportContactBlock" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a asp-page="./Index" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
|
||||
<partial name="_ValidationScriptsPartial" />
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using maintenance.Data;
|
||||
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 EditModel : PageModel
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
||||
public EditModel(AppDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public InputModel Input { get; set; } = new();
|
||||
|
||||
public sealed class InputModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public string? SupportContactBlock { get; set; }
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(Guid id)
|
||||
{
|
||||
var template = await _dbContext.Templates
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(t => t.Id == id, HttpContext.RequestAborted);
|
||||
|
||||
if (template is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Input = new InputModel
|
||||
{
|
||||
Id = template.Id,
|
||||
Name = template.Name,
|
||||
Title = template.Title,
|
||||
Body = template.Body,
|
||||
SupportContactBlock = template.SupportContactBlock
|
||||
};
|
||||
|
||||
return Page();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAsync()
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Page();
|
||||
}
|
||||
|
||||
var template = await _dbContext.Templates
|
||||
.FirstOrDefaultAsync(t => t.Id == Input.Id, HttpContext.RequestAborted);
|
||||
|
||||
if (template is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
template.Title = Input.Title.Trim();
|
||||
template.Body = Input.Body.Trim();
|
||||
template.SupportContactBlock = string.IsNullOrWhiteSpace(Input.SupportContactBlock)
|
||||
? null
|
||||
: Input.SupportContactBlock.Trim();
|
||||
template.LastUpdatedAtUtc = DateTime.UtcNow;
|
||||
template.LastUpdatedBy = User.Identity?.Name ?? "unknown";
|
||||
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
@page
|
||||
@model maintenance.Pages.Admin.Templates.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "Templates";
|
||||
}
|
||||
|
||||
<h1>Templates</h1>
|
||||
<p>Manage error template content used by user-facing pages.</p>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Title</th>
|
||||
<th>Updated</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var item in Model.Templates)
|
||||
{
|
||||
<tr>
|
||||
<td>@item.Name</td>
|
||||
<td>@item.Title</td>
|
||||
<td>@item.LastUpdatedAtUtc.ToString("u")</td>
|
||||
<td><a asp-page="./Edit" asp-route-id="@item.Id">Edit</a></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -0,0 +1,28 @@
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Entities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
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<TemplateEntity> Templates { get; private set; } = [];
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
Templates = await _dbContext.Templates
|
||||
.AsNoTracking()
|
||||
.OrderBy(t => t.Name)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user