From 718ac72c57f37be50929d95d0d32498a288017c0 Mon Sep 17 00:00:00 2001 From: Ccjcarkbla Date: Mon, 23 Feb 2026 13:14:57 -0500 Subject: [PATCH] adminUI --- Data/AppDbContextFactory.cs | 32 ++++++++++ Data/Seed/TemplateSeeder.cs | 61 ++++++++++++++++++ Pages/Admin/Index.cshtml | 1 + Pages/Admin/Templates/Edit.cshtml | 39 ++++++++++++ Pages/Admin/Templates/Edit.cshtml.cs | 89 +++++++++++++++++++++++++++ Pages/Admin/Templates/Index.cshtml | 30 +++++++++ Pages/Admin/Templates/Index.cshtml.cs | 28 +++++++++ Program.cs | 15 +++++ 8 files changed, 295 insertions(+) create mode 100644 Data/AppDbContextFactory.cs create mode 100644 Data/Seed/TemplateSeeder.cs create mode 100644 Pages/Admin/Templates/Edit.cshtml create mode 100644 Pages/Admin/Templates/Edit.cshtml.cs create mode 100644 Pages/Admin/Templates/Index.cshtml create mode 100644 Pages/Admin/Templates/Index.cshtml.cs diff --git a/Data/AppDbContextFactory.cs b/Data/AppDbContextFactory.cs new file mode 100644 index 0000000..0724ecf --- /dev/null +++ b/Data/AppDbContextFactory.cs @@ -0,0 +1,32 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace maintenance.Data; + +public sealed class AppDbContextFactory : IDesignTimeDbContextFactory +{ + public AppDbContext CreateDbContext(string[] args) + { + var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"; + + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: true) + .AddJsonFile($"appsettings.{environment}.json", optional: true) + .AddJsonFile("FutureSecrets.json", optional: true) + .AddEnvironmentVariables() + .Build(); + + var connectionString = configuration.GetConnectionString("PrimaryDb"); + if (string.IsNullOrWhiteSpace(connectionString)) + { + throw new InvalidOperationException( + "Missing connection string for design-time DbContext: ConnectionStrings:PrimaryDb"); + } + + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(connectionString); + + return new AppDbContext(optionsBuilder.Options); + } +} diff --git a/Data/Seed/TemplateSeeder.cs b/Data/Seed/TemplateSeeder.cs new file mode 100644 index 0000000..6d198cc --- /dev/null +++ b/Data/Seed/TemplateSeeder.cs @@ -0,0 +1,61 @@ +using maintenance.Data.Entities; +using Microsoft.EntityFrameworkCore; + +namespace maintenance.Data.Seed; + +public static class TemplateSeeder +{ + public static async Task SeedAsync(AppDbContext dbContext, CancellationToken cancellationToken = default) + { + var defaults = new[] + { + new TemplateEntity + { + Id = Guid.NewGuid(), + Name = "generic", + Title = "System Error", + Body = "A system error has occurred. Please contact MAX Support.", + SupportContactBlock = null, + LastUpdatedBy = "system-seed", + LastUpdatedAtUtc = DateTime.UtcNow + }, + new TemplateEntity + { + Id = Guid.NewGuid(), + Name = "unavailable", + Title = "Application Unavailable", + Body = "MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning.\nIf you are receiving this message at any other time, please contact MAX Support.", + SupportContactBlock = null, + LastUpdatedBy = "system-seed", + LastUpdatedAtUtc = DateTime.UtcNow + }, + new TemplateEntity + { + Id = Guid.NewGuid(), + Name = "template", + Title = "Error Name", + Body = "An error has occurred. Please contact MAX Support for assistance.", + SupportContactBlock = null, + LastUpdatedBy = "system-seed", + LastUpdatedAtUtc = DateTime.UtcNow + } + }; + + var existingNames = await dbContext.Templates + .AsNoTracking() + .Select(x => x.Name) + .ToListAsync(cancellationToken); + + var missing = defaults + .Where(x => !existingNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase)) + .ToList(); + + if (missing.Count == 0) + { + return; + } + + await dbContext.Templates.AddRangeAsync(missing, cancellationToken); + await dbContext.SaveChangesAsync(cancellationToken); + } +} diff --git a/Pages/Admin/Index.cshtml b/Pages/Admin/Index.cshtml index 521d42c..258154e 100644 --- a/Pages/Admin/Index.cshtml +++ b/Pages/Admin/Index.cshtml @@ -7,5 +7,6 @@

Admin Console

This page is protected by the AdminOnly policy.

+

Manage Templates

diff --git a/Pages/Admin/Templates/Edit.cshtml b/Pages/Admin/Templates/Edit.cshtml new file mode 100644 index 0000000..b1d7bdb --- /dev/null +++ b/Pages/Admin/Templates/Edit.cshtml @@ -0,0 +1,39 @@ +@page "{id:guid}" +@model maintenance.Pages.Admin.Templates.EditModel +@{ + ViewData["Title"] = "Edit Template"; +} + +

Edit Template

+ +
+ + +
+ + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ + + Cancel +
+ + diff --git a/Pages/Admin/Templates/Edit.cshtml.cs b/Pages/Admin/Templates/Edit.cshtml.cs new file mode 100644 index 0000000..b208171 --- /dev/null +++ b/Pages/Admin/Templates/Edit.cshtml.cs @@ -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 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 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"); + } +} diff --git a/Pages/Admin/Templates/Index.cshtml b/Pages/Admin/Templates/Index.cshtml new file mode 100644 index 0000000..56e270a --- /dev/null +++ b/Pages/Admin/Templates/Index.cshtml @@ -0,0 +1,30 @@ +@page +@model maintenance.Pages.Admin.Templates.IndexModel +@{ + ViewData["Title"] = "Templates"; +} + +

Templates

+

Manage error template content used by user-facing pages.

+ + + + + + + + + + + + @foreach (var item in Model.Templates) + { + + + + + + + } + +
NameTitleUpdated
@item.Name@item.Title@item.LastUpdatedAtUtc.ToString("u")Edit
diff --git a/Pages/Admin/Templates/Index.cshtml.cs b/Pages/Admin/Templates/Index.cshtml.cs new file mode 100644 index 0000000..8b40fdf --- /dev/null +++ b/Pages/Admin/Templates/Index.cshtml.cs @@ -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 Templates { get; private set; } = []; + + public async Task OnGetAsync() + { + Templates = await _dbContext.Templates + .AsNoTracking() + .OrderBy(t => t.Name) + .ToListAsync(HttpContext.RequestAborted); + } +} diff --git a/Program.cs b/Program.cs index 019582d..e693cde 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using maintenance.Authentication.Core; using maintenance.Authentication.Providers.AdfsWsFed; using maintenance.Authentication.Providers.Oidc; using maintenance.Data; +using maintenance.Data.Seed; using maintenance.Services.Templates; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; @@ -97,6 +98,20 @@ builder.Services.AddAuthorization(options => var app = builder.Build(); +if (app.Environment.IsDevelopment()) +{ + using var scope = app.Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + try + { + await TemplateSeeder.SeedAsync(db); + } + catch + { + // Seed is best-effort for offline/dev environments. + } +} + if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error");