188 lines
6.4 KiB
C#
188 lines
6.4 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Text.Json;
|
|
using maintenance.Data;
|
|
using maintenance.Services.Pages;
|
|
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 string PreviewUrl { get; private set; } = "/errors/maintenance";
|
|
|
|
public sealed class InputModel
|
|
{
|
|
public Guid Id { get; set; }
|
|
|
|
public string Key { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
[StringLength(200)]
|
|
public string DisplayName { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
[RegularExpression("^(maintenance|error)$")]
|
|
public string PageKind { get; set; } = "maintenance";
|
|
|
|
public bool IsDefaultFallback { get; set; }
|
|
public bool IsEnabled { get; set; }
|
|
public bool OverrideWindow { get; set; }
|
|
public int Priority { get; set; }
|
|
public DateTime? StartUtc { get; set; }
|
|
public DateTime? EndUtc { get; set; }
|
|
public string? RecurrenceRule { get; set; }
|
|
|
|
[Required]
|
|
[StringLength(200)]
|
|
public string ErrorName { get; set; } = string.Empty;
|
|
public string? ShortDescription { get; set; }
|
|
[Required]
|
|
public string Description { get; set; } = string.Empty;
|
|
public string? InstructionsHeader { get; set; }
|
|
public string? InstructionSteps { get; set; }
|
|
public bool ShowInstructions { get; set; } = true;
|
|
public bool ShowSupport { get; set; } = true;
|
|
[Required]
|
|
public string SupportEmail { get; set; } = "maxsupport@max.gov";
|
|
[Required]
|
|
public string SupportPhone { get; set; } = "202-395-6860";
|
|
[Required]
|
|
public string SupportHours { get; set; } = "8:00AM - 8:00PM Weekdays, 9:00AM - 6:00PM Weekends (ET)";
|
|
[Required]
|
|
public string ErrorCode { get; set; } = "eag";
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetAsync(Guid id)
|
|
{
|
|
var page = await _dbContext.ManagedPages
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(t => t.Id == id, HttpContext.RequestAborted);
|
|
|
|
if (page is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var content = Deserialize(page.ContentJson);
|
|
|
|
Input = new InputModel
|
|
{
|
|
Id = page.Id,
|
|
Key = page.Key,
|
|
DisplayName = page.DisplayName,
|
|
PageKind = page.PageKind,
|
|
IsDefaultFallback = page.IsDefaultFallback,
|
|
IsEnabled = page.IsEnabled,
|
|
OverrideWindow = page.OverrideWindow,
|
|
Priority = page.Priority,
|
|
StartUtc = page.StartUtc,
|
|
EndUtc = page.EndUtc,
|
|
RecurrenceRule = page.RecurrenceRule,
|
|
ErrorName = content.ErrorName,
|
|
ShortDescription = content.ShortDescription,
|
|
Description = content.Description,
|
|
InstructionsHeader = content.InstructionsHeader,
|
|
InstructionSteps = content.InstructionSteps.Count == 0 ? null : string.Join('|', content.InstructionSteps),
|
|
ShowInstructions = content.ShowInstructions,
|
|
ShowSupport = content.ShowSupport,
|
|
SupportEmail = content.SupportEmail,
|
|
SupportPhone = content.SupportPhone,
|
|
SupportHours = content.SupportHours,
|
|
ErrorCode = content.ErrorCode
|
|
};
|
|
|
|
PreviewUrl = BuildPreviewUrl(page.PageKind, page.Key);
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key);
|
|
return Page();
|
|
}
|
|
|
|
var page = await _dbContext.ManagedPages
|
|
.FirstOrDefaultAsync(t => t.Id == Input.Id, HttpContext.RequestAborted);
|
|
|
|
if (page is null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
page.DisplayName = Input.DisplayName.Trim();
|
|
page.PageKind = Input.PageKind.Trim().ToLowerInvariant();
|
|
page.IsDefaultFallback = Input.IsDefaultFallback;
|
|
page.IsEnabled = Input.IsEnabled;
|
|
page.OverrideWindow = Input.OverrideWindow;
|
|
page.Priority = Input.Priority;
|
|
page.StartUtc = Input.StartUtc;
|
|
page.EndUtc = Input.EndUtc;
|
|
page.RecurrenceRule = string.IsNullOrWhiteSpace(Input.RecurrenceRule) ? null : Input.RecurrenceRule.Trim();
|
|
page.ContentJson = JsonSerializer.Serialize(new PageContentModel
|
|
{
|
|
ErrorName = Input.ErrorName.Trim(),
|
|
ShortDescription = string.IsNullOrWhiteSpace(Input.ShortDescription) ? null : Input.ShortDescription.Trim(),
|
|
Description = Input.Description.Trim(),
|
|
InstructionsHeader = string.IsNullOrWhiteSpace(Input.InstructionsHeader) ? null : Input.InstructionsHeader.Trim(),
|
|
InstructionSteps = ParseSteps(Input.InstructionSteps),
|
|
ShowInstructions = Input.ShowInstructions,
|
|
ShowSupport = Input.ShowSupport,
|
|
SupportEmail = Input.SupportEmail.Trim(),
|
|
SupportPhone = Input.SupportPhone.Trim(),
|
|
SupportHours = Input.SupportHours.Trim(),
|
|
ErrorCode = Input.ErrorCode.Trim()
|
|
});
|
|
page.LastUpdatedAtUtc = DateTime.UtcNow;
|
|
page.LastUpdatedBy = User.Identity?.Name ?? "unknown";
|
|
|
|
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
|
|
|
return RedirectToPage("./Index");
|
|
}
|
|
|
|
private static PageContentModel Deserialize(string json)
|
|
{
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<PageContentModel>(json) ?? new PageContentModel();
|
|
}
|
|
catch
|
|
{
|
|
return new PageContentModel();
|
|
}
|
|
}
|
|
|
|
private static List<string> ParseSteps(string? steps)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(steps))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
return steps.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.Where(x => !string.IsNullOrWhiteSpace(x))
|
|
.ToList();
|
|
}
|
|
|
|
private static string BuildPreviewUrl(string kind, string key)
|
|
{
|
|
return $"/errors/{kind}?page={Uri.EscapeDataString(key)}";
|
|
}
|
|
}
|