2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using LANCommander.Server.Data;
|
2024-10-16 01:54:51 -05:00
|
|
|
|
using LANCommander.Server.Data.Models;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-16 01:54:51 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2024-10-16 01:54:51 -05:00
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Server.Services
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class UserCustomFieldService(
|
|
|
|
|
|
ILogger<UserCustomFieldService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory) : BaseDatabaseService<UserCustomField>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2024-10-16 01:54:51 -05:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public override async Task<UserCustomField> AddAsync(UserCustomField entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ucf => ucf.User);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public override async Task<UserCustomField> UpdateAsync(UserCustomField entity)
|
|
|
|
|
|
{
|
2025-02-02 14:58:07 -06:00
|
|
|
|
return await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(ucf => ucf.User);
|
|
|
|
|
|
});
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
2024-10-16 01:54:51 -05:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<UserCustomField> GetAsync(Guid userId, string name)
|
2024-10-16 01:54:51 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
return await FirstOrDefaultAsync(cf => cf.UserId == userId && cf.Name == name);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task UpdateAsync(Guid userId, string name, string value)
|
2024-10-16 01:54:51 -05:00
|
|
|
|
{
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
var existing = await GetAsync(userId, name);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
|
|
|
|
|
|
if (existing.Value == value)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (existing == null)
|
|
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await AddAsync(new UserCustomField
|
2024-10-16 01:54:51 -05:00
|
|
|
|
{
|
|
|
|
|
|
Name = name,
|
|
|
|
|
|
Value = value
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (!String.IsNullOrWhiteSpace(value))
|
|
|
|
|
|
{
|
|
|
|
|
|
existing.Value = value;
|
|
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await UpdateAsync(existing);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
await DeleteAsync(userId, name);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task DeleteAsync(Guid userId, string name)
|
2024-10-16 01:54:51 -05:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
var existing = await GetAsync(userId, name);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
|
2025-02-01 02:21:34 -06:00
|
|
|
|
await DeleteAsync(existing);
|
2024-10-16 01:54:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|