Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
80 lines
2.6 KiB
C#
80 lines
2.6 KiB
C#
using AutoMapper;
|
|
using LANCommander.Server.Data;
|
|
using LANCommander.Server.Data.Models;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using ZiggyCreatures.Caching.Fusion;
|
|
|
|
namespace LANCommander.Server.Services
|
|
{
|
|
public sealed class UserCustomFieldService(
|
|
ILogger<UserCustomFieldService> logger,
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
|
IFusionCache cache,
|
|
IMapper mapper,
|
|
IHttpContextAccessor httpContextAccessor,
|
|
IDbContextFactory<DatabaseContext> contextFactory) : BaseDatabaseService<UserCustomField>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
|
{
|
|
public override async Task<UserCustomField> AddAsync(UserCustomField entity)
|
|
{
|
|
return await base.AddAsync(entity, async context =>
|
|
{
|
|
await context.UpdateRelationshipAsync(ucf => ucf.User);
|
|
});
|
|
}
|
|
|
|
public override async Task<UserCustomField> UpdateAsync(UserCustomField entity)
|
|
{
|
|
return await base.UpdateAsync(entity, async context =>
|
|
{
|
|
await context.UpdateRelationshipAsync(ucf => ucf.User);
|
|
});
|
|
}
|
|
|
|
public async Task<UserCustomField> GetAsync(Guid userId, string name)
|
|
{
|
|
return await FirstOrDefaultAsync(cf => cf.UserId == userId && cf.Name == name);
|
|
}
|
|
|
|
public async Task UpdateAsync(Guid userId, string name, string value)
|
|
{
|
|
if (name.Length > 64)
|
|
throw new ArgumentException("Field name must be 64 characters or shorter");
|
|
|
|
if (value.Length > 1024)
|
|
throw new ArgumentException("Field value must be 1024 characters or less");
|
|
|
|
var existing = await GetAsync(userId, name);
|
|
|
|
if (existing.Value == value)
|
|
return;
|
|
|
|
if (existing == null)
|
|
{
|
|
await AddAsync(new UserCustomField
|
|
{
|
|
Name = name,
|
|
Value = value
|
|
});
|
|
}
|
|
else if (!String.IsNullOrWhiteSpace(value))
|
|
{
|
|
existing.Value = value;
|
|
|
|
await UpdateAsync(existing);
|
|
}
|
|
else
|
|
{
|
|
await DeleteAsync(userId, name);
|
|
}
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid userId, string name)
|
|
{
|
|
var existing = await GetAsync(userId, name);
|
|
|
|
await DeleteAsync(existing);
|
|
}
|
|
}
|
|
}
|