157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
using Flypaper.Storage.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Flypaper.Storage.Explanations;
|
|
|
|
public sealed record ExplanationRuleInput(
|
|
ExplanationKind? Kind = null,
|
|
string? Pattern = null,
|
|
string? Text = null,
|
|
int? Priority = null,
|
|
string? Note = null);
|
|
|
|
public sealed class ExplanationRuleStore(IDbContextFactory<FlypaperDbContext> dbContextFactory, TimeProvider timeProvider)
|
|
{
|
|
public const string DashboardActor = "Dashboard";
|
|
|
|
private readonly object _gate = new();
|
|
private RuleSet? _cached;
|
|
private int _version;
|
|
|
|
public async Task<RuleSet> GetRuleSetAsync(CancellationToken ct = default)
|
|
{
|
|
int version;
|
|
lock (_gate)
|
|
{
|
|
if (_cached is { } cached)
|
|
{
|
|
return cached;
|
|
}
|
|
|
|
version = _version;
|
|
}
|
|
|
|
var rules = new RuleSet((await ListAsync(ct)).Where(r => r.Enabled));
|
|
lock (_gate)
|
|
{
|
|
if (version == _version)
|
|
{
|
|
_cached = rules;
|
|
}
|
|
}
|
|
|
|
return rules;
|
|
}
|
|
|
|
public async Task<IReadOnlyList<ExplanationRuleEntity>> ListAsync(CancellationToken ct = default)
|
|
{
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
return await db.ExplanationRules.AsNoTracking().OrderBy(r => r.Kind).ThenByDescending(r => r.Priority).ThenBy(r => r.Id).ToListAsync(ct);
|
|
}
|
|
|
|
public async Task<ExplanationRuleEntity?> GetAsync(int id, CancellationToken ct = default)
|
|
{
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
return await db.ExplanationRules.AsNoTracking().FirstOrDefaultAsync(r => r.Id == id, ct);
|
|
}
|
|
|
|
public async Task<ExplanationRuleEntity> CreateAsync(ExplanationRuleInput input, string createdBy, CancellationToken ct = default)
|
|
{
|
|
var rule = new ExplanationRuleEntity
|
|
{
|
|
Kind = input.Kind ?? throw new ArgumentException("The kind is required."),
|
|
Pattern = ExplanationValidator.Pattern(input.Pattern),
|
|
Text = ExplanationValidator.Text(input.Text),
|
|
Priority = ExplanationValidator.Priority(input.Priority ?? 0),
|
|
Note = ExplanationValidator.Note(input.Note),
|
|
CreatedBy = createdBy.Length > ExplanationLimits.MaxCreatedByLength ? createdBy[..ExplanationLimits.MaxCreatedByLength] : createdBy,
|
|
CreatedAt = timeProvider.GetUtcNow().UtcDateTime
|
|
};
|
|
rule.UpdatedAt = rule.CreatedAt;
|
|
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
if (await db.ExplanationRules.CountAsync(ct) >= ExplanationLimits.MaxRules)
|
|
{
|
|
throw new ArgumentException($"There are already {ExplanationLimits.MaxRules} rules; delete unused ones first.");
|
|
}
|
|
|
|
db.ExplanationRules.Add(rule);
|
|
await db.SaveChangesAsync(ct);
|
|
Invalidate();
|
|
return rule;
|
|
}
|
|
|
|
public async Task<ExplanationRuleEntity?> UpdateAsync(int id, ExplanationRuleInput input, CancellationToken ct = default)
|
|
{
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
var rule = await db.ExplanationRules.FirstOrDefaultAsync(r => r.Id == id, ct);
|
|
if (rule is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (input.Kind is { } kind)
|
|
{
|
|
rule.Kind = kind;
|
|
}
|
|
|
|
if (input.Pattern is not null)
|
|
{
|
|
rule.Pattern = ExplanationValidator.Pattern(input.Pattern);
|
|
}
|
|
|
|
if (input.Text is not null)
|
|
{
|
|
rule.Text = ExplanationValidator.Text(input.Text);
|
|
}
|
|
|
|
if (input.Priority is { } priority)
|
|
{
|
|
rule.Priority = ExplanationValidator.Priority(priority);
|
|
}
|
|
|
|
if (input.Note is not null)
|
|
{
|
|
rule.Note = ExplanationValidator.Note(input.Note);
|
|
}
|
|
|
|
rule.UpdatedAt = timeProvider.GetUtcNow().UtcDateTime;
|
|
await db.SaveChangesAsync(ct);
|
|
Invalidate();
|
|
return rule;
|
|
}
|
|
|
|
public async Task<ExplanationRuleEntity?> SetEnabledAsync(int id, bool enabled, CancellationToken ct = default)
|
|
{
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
var rule = await db.ExplanationRules.FirstOrDefaultAsync(r => r.Id == id, ct);
|
|
if (rule is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
rule.Enabled = enabled;
|
|
rule.UpdatedAt = timeProvider.GetUtcNow().UtcDateTime;
|
|
await db.SaveChangesAsync(ct);
|
|
Invalidate();
|
|
return rule;
|
|
}
|
|
|
|
public async Task<bool> DeleteAsync(int id, CancellationToken ct = default)
|
|
{
|
|
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
|
var deleted = await db.ExplanationRules.Where(r => r.Id == id).ExecuteDeleteAsync(ct) > 0;
|
|
Invalidate();
|
|
return deleted;
|
|
}
|
|
|
|
private void Invalidate()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
_version++;
|
|
_cached = null;
|
|
}
|
|
}
|
|
}
|