initial commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using lxc_songlist.Data;
|
||||
using lxc_songlist.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace lxc_songlist.Services;
|
||||
|
||||
public sealed class SiteSettingsService(
|
||||
IDbContextFactory<SongListDbContext> dbFactory,
|
||||
IOptions<SiteOptions> options)
|
||||
{
|
||||
public const string RequestTemplateKey = "request_template";
|
||||
|
||||
public async Task<string> GetRequestTemplateAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using var db = await dbFactory.CreateDbContextAsync(cancellationToken);
|
||||
var setting = await db.SiteSettings.FindAsync([RequestTemplateKey], cancellationToken);
|
||||
return string.IsNullOrWhiteSpace(setting?.Value)
|
||||
? options.Value.RequestTemplate
|
||||
: setting.Value;
|
||||
}
|
||||
|
||||
public async Task SaveRequestTemplateAsync(string template, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var cleanTemplate = string.IsNullOrWhiteSpace(template)
|
||||
? options.Value.RequestTemplate
|
||||
: template.Trim();
|
||||
|
||||
await using var db = await dbFactory.CreateDbContextAsync(cancellationToken);
|
||||
var setting = await db.SiteSettings.FindAsync([RequestTemplateKey], cancellationToken);
|
||||
if (setting is null)
|
||||
{
|
||||
db.SiteSettings.Add(new SiteSetting
|
||||
{
|
||||
Key = RequestTemplateKey,
|
||||
Value = cleanTemplate,
|
||||
UpdatedAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
setting.Value = cleanTemplate;
|
||||
setting.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public static string FormatRequest(string template, string title)
|
||||
{
|
||||
return template.Replace("{title}", title, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user