Adding stub targeting
This commit is contained in:
@@ -104,12 +104,17 @@ public sealed class AppDbContext : DbContext
|
||||
e.ToTable("ManagedPage");
|
||||
e.HasKey(x => x.Id);
|
||||
e.Property(x => x.Key).HasMaxLength(100).IsRequired();
|
||||
e.Property(x => x.Stub).HasMaxLength(100);
|
||||
e.Property(x => x.NormalizedStub).HasMaxLength(100);
|
||||
e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired();
|
||||
e.Property(x => x.PageKind).HasMaxLength(50).IsRequired();
|
||||
e.Property(x => x.RecurrenceRule).HasMaxLength(200);
|
||||
e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired();
|
||||
e.Property(x => x.ContentJson).IsRequired();
|
||||
e.HasIndex(x => x.Key).IsUnique();
|
||||
e.HasIndex(x => new { x.PageKind, x.NormalizedStub })
|
||||
.IsUnique()
|
||||
.HasFilter("[NormalizedStub] IS NOT NULL");
|
||||
e.HasIndex(x => new { x.PageKind, x.IsEnabled, x.Priority });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ public sealed class ManagedPageEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Key { get; set; } = string.Empty;
|
||||
public string? Stub { get; set; }
|
||||
public string? NormalizedStub { get; set; }
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
public string PageKind { get; set; } = "maintenance"; // maintenance | error
|
||||
public bool IsDefaultFallback { get; set; }
|
||||
|
||||
@@ -26,6 +26,12 @@
|
||||
<span asp-validation-for="Input.DisplayName" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Stub" class="form-label"></label>
|
||||
<input asp-for="Input.Stub" class="form-control" placeholder="team-a-maintenance" />
|
||||
<span asp-validation-for="Input.Stub" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Create</button>
|
||||
<a asp-page="./Index" class="btn btn-secondary">Cancel</a>
|
||||
</form>
|
||||
|
||||
@@ -41,6 +41,11 @@ public sealed class CreateModel : PageModel
|
||||
[StringLength(200)]
|
||||
[Display(Name = "Display Name")]
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
|
||||
[StringLength(100)]
|
||||
[RegularExpression("^[a-z0-9-]*$")]
|
||||
[Display(Name = "Stub (optional)")]
|
||||
public string? Stub { get; set; }
|
||||
}
|
||||
|
||||
public async Task OnGetAsync()
|
||||
@@ -68,6 +73,7 @@ public sealed class CreateModel : PageModel
|
||||
}
|
||||
|
||||
var normalizedKey = Input.NewKey.Trim().ToLowerInvariant();
|
||||
var normalizedStub = NormalizeStub(Input.Stub);
|
||||
var exists = await _dbContext.ManagedPages
|
||||
.AnyAsync(t => t.Key == normalizedKey, HttpContext.RequestAborted);
|
||||
|
||||
@@ -77,10 +83,24 @@ public sealed class CreateModel : PageModel
|
||||
return Page();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(normalizedStub))
|
||||
{
|
||||
var stubExists = await _dbContext.ManagedPages
|
||||
.AnyAsync(t => t.PageKind == source.PageKind && t.NormalizedStub == normalizedStub, HttpContext.RequestAborted);
|
||||
|
||||
if (stubExists)
|
||||
{
|
||||
ModelState.AddModelError(nameof(Input.Stub), "A page with this stub already exists for this page kind.");
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
||||
var clone = new ManagedPageEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Key = normalizedKey,
|
||||
Stub = normalizedStub,
|
||||
NormalizedStub = normalizedStub,
|
||||
DisplayName = Input.DisplayName.Trim(),
|
||||
PageKind = source.PageKind,
|
||||
IsDefaultFallback = false,
|
||||
@@ -127,4 +147,14 @@ public sealed class CreateModel : PageModel
|
||||
return JsonSerializer.Serialize(new PageContentModel());
|
||||
}
|
||||
}
|
||||
|
||||
private static string? NormalizeStub(string? stub)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(stub))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return stub.Trim().ToLowerInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,19 @@
|
||||
<input asp-for="Input.Id" type="hidden" />
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.Key" class="form-label"></label>
|
||||
<input asp-for="Input.Key" class="form-control" readonly />
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.DisplayName" class="form-label"></label>
|
||||
<input asp-for="Input.DisplayName" class="form-control" />
|
||||
</div>
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.Stub" class="form-label"></label>
|
||||
<input asp-for="Input.Stub" class="form-control" placeholder="team-a-maintenance" />
|
||||
<span asp-validation-for="Input.Stub" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
|
||||
@@ -30,6 +30,11 @@ public sealed class EditModel : PageModel
|
||||
|
||||
public string Key { get; set; } = string.Empty;
|
||||
|
||||
[StringLength(100)]
|
||||
[RegularExpression("^[a-z0-9-]*$")]
|
||||
[Display(Name = "Stub (optional)")]
|
||||
public string? Stub { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
@@ -85,6 +90,7 @@ public sealed class EditModel : PageModel
|
||||
{
|
||||
Id = page.Id,
|
||||
Key = page.Key,
|
||||
Stub = page.Stub,
|
||||
DisplayName = page.DisplayName,
|
||||
PageKind = page.PageKind,
|
||||
IsDefaultFallback = page.IsDefaultFallback,
|
||||
@@ -107,7 +113,7 @@ public sealed class EditModel : PageModel
|
||||
ErrorCode = content.ErrorCode
|
||||
};
|
||||
|
||||
PreviewUrl = BuildPreviewUrl(page.PageKind, page.Key);
|
||||
PreviewUrl = BuildPreviewUrl(page.PageKind, page.Key, page.Stub);
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -115,7 +121,7 @@ public sealed class EditModel : PageModel
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key);
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -129,10 +135,29 @@ public sealed class EditModel : PageModel
|
||||
|
||||
page.DisplayName = Input.DisplayName.Trim();
|
||||
page.PageKind = Input.PageKind.Trim().ToLowerInvariant();
|
||||
page.Stub = NormalizeStub(Input.Stub);
|
||||
page.NormalizedStub = page.Stub;
|
||||
page.IsDefaultFallback = Input.IsDefaultFallback;
|
||||
page.IsEnabled = Input.IsEnabled;
|
||||
page.OverrideWindow = Input.OverrideWindow;
|
||||
page.Priority = Input.Priority;
|
||||
if (!string.IsNullOrWhiteSpace(page.NormalizedStub))
|
||||
{
|
||||
var duplicateStubExists = await _dbContext.ManagedPages
|
||||
.AnyAsync(t =>
|
||||
t.Id != page.Id &&
|
||||
t.PageKind == page.PageKind &&
|
||||
t.NormalizedStub == page.NormalizedStub,
|
||||
HttpContext.RequestAborted);
|
||||
|
||||
if (duplicateStubExists)
|
||||
{
|
||||
ModelState.AddModelError("Input.Stub", "A page with this stub already exists for this page kind.");
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
||||
var hasInvalidEasternTime = false;
|
||||
if (!TryConvertEasternLocalToUtc(Input.StartUtc, out var startUtc))
|
||||
{
|
||||
@@ -148,7 +173,7 @@ public sealed class EditModel : PageModel
|
||||
|
||||
if (hasInvalidEasternTime)
|
||||
{
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key);
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key, Input.Stub);
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -201,11 +226,26 @@ public sealed class EditModel : PageModel
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static string BuildPreviewUrl(string kind, string key)
|
||||
private static string BuildPreviewUrl(string kind, string key, string? stub = null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(stub))
|
||||
{
|
||||
return $"/{kind}?stub={Uri.EscapeDataString(stub.Trim())}";
|
||||
}
|
||||
|
||||
return $"/{kind}?page={Uri.EscapeDataString(key)}";
|
||||
}
|
||||
|
||||
private static string? NormalizeStub(string? stub)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(stub))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return stub.Trim().ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static bool TryConvertEasternLocalToUtc(DateTime? easternLocal, out DateTime? utc)
|
||||
{
|
||||
utc = null;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Key</th>
|
||||
<th>Stub</th>
|
||||
<th>Kind</th>
|
||||
<th>Default</th>
|
||||
<th>Enabled</th>
|
||||
@@ -29,6 +30,7 @@
|
||||
{
|
||||
<tr>
|
||||
<td>@item.Key</td>
|
||||
<td>@(string.IsNullOrWhiteSpace(item.Stub) ? "-" : item.Stub)</td>
|
||||
<td>@item.PageKind</td>
|
||||
<td>@item.IsDefaultFallback</td>
|
||||
<td>@item.IsEnabled</td>
|
||||
|
||||
@@ -57,6 +57,11 @@ public sealed class IndexModel : PageModel
|
||||
|
||||
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)}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,12 +15,25 @@ public sealed class IndexModel : PageModel
|
||||
[FromQuery(Name = "template")]
|
||||
public string? TemplateName { get; set; }
|
||||
|
||||
[FromQuery(Name = "stub")]
|
||||
public string? Stub { get; set; }
|
||||
|
||||
public IActionResult OnGet()
|
||||
{
|
||||
var effectiveTemplate = string.IsNullOrWhiteSpace(TemplateName)
|
||||
? (_configuration["ErrorPages:DefaultTemplate"] ?? "unavailable")
|
||||
: TemplateName;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Stub))
|
||||
{
|
||||
var encodedStub = Uri.EscapeDataString(Stub.Trim());
|
||||
var targetKind = effectiveTemplate.Equals("generic", StringComparison.OrdinalIgnoreCase) ||
|
||||
effectiveTemplate.Equals("error", StringComparison.OrdinalIgnoreCase)
|
||||
? "error"
|
||||
: "maintenance";
|
||||
return Redirect($"/{targetKind}?stub={encodedStub}");
|
||||
}
|
||||
|
||||
if (effectiveTemplate.Equals("generic", StringComparison.OrdinalIgnoreCase) ||
|
||||
effectiveTemplate.Equals("error", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
|
||||
@@ -15,7 +15,10 @@ public sealed class RenderModel : PageModel
|
||||
|
||||
public ResolvedPage Resolved { get; private set; } = new();
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(string kind, [FromQuery] string? page = null)
|
||||
public async Task<IActionResult> OnGetAsync(
|
||||
string kind,
|
||||
[FromQuery] string? page = null,
|
||||
[FromQuery] string? stub = null)
|
||||
{
|
||||
if (!kind.Equals("maintenance", StringComparison.OrdinalIgnoreCase) &&
|
||||
!kind.Equals("error", StringComparison.OrdinalIgnoreCase))
|
||||
@@ -23,7 +26,7 @@ public sealed class RenderModel : PageModel
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Resolved = await _managedPageService.ResolveAsync(kind, page, DateTime.UtcNow, HttpContext.RequestAborted);
|
||||
Resolved = await _managedPageService.ResolveAsync(kind, page, stub, DateTime.UtcNow, HttpContext.RequestAborted);
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ public interface IManagedPageService
|
||||
Task<ResolvedPage> ResolveAsync(
|
||||
string pageKind,
|
||||
string? requestedPageKey = null,
|
||||
string? requestedStub = null,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -21,10 +21,12 @@ public sealed class ManagedPageService : IManagedPageService
|
||||
public async Task<ResolvedPage> ResolveAsync(
|
||||
string pageKind,
|
||||
string? requestedPageKey = null,
|
||||
string? requestedStub = null,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var normalizedKind = NormalizeKind(pageKind);
|
||||
var normalizedStub = NormalizeStub(requestedStub);
|
||||
var utcNow = nowUtc ?? DateTime.UtcNow;
|
||||
|
||||
var pages = await _dbContext.ManagedPages
|
||||
@@ -37,9 +39,16 @@ public sealed class ManagedPageService : IManagedPageService
|
||||
|
||||
ManagedPageEntity? selected = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(requestedPageKey))
|
||||
if (!string.IsNullOrWhiteSpace(normalizedStub))
|
||||
{
|
||||
selected = pages.FirstOrDefault(p =>
|
||||
p.NormalizedStub == normalizedStub &&
|
||||
IsWindowActive(p, utcNow));
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(requestedPageKey))
|
||||
{
|
||||
selected ??= pages.FirstOrDefault(p =>
|
||||
p.Key.Equals(requestedPageKey, StringComparison.OrdinalIgnoreCase) &&
|
||||
IsWindowActive(p, utcNow));
|
||||
}
|
||||
@@ -73,6 +82,16 @@ public sealed class ManagedPageService : IManagedPageService
|
||||
return pageKind.Equals("error", StringComparison.OrdinalIgnoreCase) ? "error" : "maintenance";
|
||||
}
|
||||
|
||||
private static string? NormalizeStub(string? stub)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(stub))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return stub.Trim().ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static bool IsWindowActive(ManagedPageEntity page, DateTime utcNow)
|
||||
{
|
||||
// Initial behavior:
|
||||
|
||||
Reference in New Issue
Block a user