Files
2026-02-23 21:09:46 -05:00

74 lines
2.5 KiB
C#

using System.Text.Json;
using maintenance.Data.Entities;
using maintenance.Services.Pages;
using Microsoft.EntityFrameworkCore;
namespace maintenance.Data.Seed;
public static class ManagedPageSeeder
{
public static async Task SeedAsync(AppDbContext dbContext, CancellationToken cancellationToken = default)
{
var defaults = new[]
{
new ManagedPageEntity
{
Id = Guid.NewGuid(),
Key = "generic-maintenance",
DisplayName = "Generic Maintenance",
PageKind = "maintenance",
IsDefaultFallback = true,
IsEnabled = true,
OverrideWindow = false,
Priority = 0,
ContentJson = JsonSerializer.Serialize(new PageContentModel
{
ErrorName = "Application Unavailable",
Description = "MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning. If this appears at another time, contact support.",
ErrorCode = "eau",
ShowInstructions = false
}),
LastUpdatedBy = "system-seed",
LastUpdatedAtUtc = DateTime.UtcNow
},
new ManagedPageEntity
{
Id = Guid.NewGuid(),
Key = "generic-error",
DisplayName = "Generic Error",
PageKind = "error",
IsDefaultFallback = true,
IsEnabled = true,
OverrideWindow = false,
Priority = 0,
ContentJson = JsonSerializer.Serialize(new PageContentModel
{
ErrorName = "System Error",
Description = "A system error has occurred. Please contact MAX Support.",
ErrorCode = "eag",
ShowInstructions = false
}),
LastUpdatedBy = "system-seed",
LastUpdatedAtUtc = DateTime.UtcNow
}
};
var existingKeys = await dbContext.ManagedPages
.AsNoTracking()
.Select(x => x.Key)
.ToListAsync(cancellationToken);
var missing = defaults
.Where(x => !existingKeys.Contains(x.Key, StringComparer.OrdinalIgnoreCase))
.ToList();
if (missing.Count == 0)
{
return;
}
await dbContext.ManagedPages.AddRangeAsync(missing, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
}