2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using LANCommander.SDK.Enums;
|
2024-09-04 16:48:04 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
2024-08-04 18:44:33 -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-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class KeyService(
|
|
|
|
|
|
ILogger<KeyService> 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<Key>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
2025-02-23 08:49:43 -06:00
|
|
|
|
public override async Task<Key> AddAsync(Key entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(k => k.ClaimedByUser);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(k => k.Game);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-02 14:58:07 -06:00
|
|
|
|
public override async Task<Key> UpdateAsync(Key entity)
|
2025-02-01 02:21:34 -06:00
|
|
|
|
{
|
2025-02-02 14:58:07 -06:00
|
|
|
|
return await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(k => k.ClaimedByUser);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(k => k.Game);
|
|
|
|
|
|
});
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<Key> AllocateAsync(Key key, User user)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
key.ClaimedByUser = user;
|
2024-10-23 23:19:25 -05:00
|
|
|
|
key.ClaimedOn = DateTime.UtcNow;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
key.AllocationMethod = KeyAllocationMethod.UserAccount;
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
key = await UpdateAsync(key);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<Key> AllocateAsync(Key key, string macAddress)
|
2023-01-09 01:05:40 -06:00
|
|
|
|
{
|
|
|
|
|
|
key.ClaimedByMacAddress = macAddress;
|
2024-10-23 23:19:25 -05:00
|
|
|
|
key.ClaimedOn = DateTime.UtcNow;
|
2023-01-09 01:05:40 -06:00
|
|
|
|
key.AllocationMethod = KeyAllocationMethod.MacAddress;
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
key = await UpdateAsync(key);
|
2023-01-09 01:05:40 -06:00
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
2023-01-15 20:45:37 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<Key> ReleaseAsync(Guid id)
|
2023-01-15 20:45:37 -06:00
|
|
|
|
{
|
2024-11-12 18:32:46 -06:00
|
|
|
|
var key = await GetAsync(id);
|
2023-01-15 20:45:37 -06:00
|
|
|
|
|
|
|
|
|
|
if (key == null)
|
2023-02-11 21:39:06 -06:00
|
|
|
|
return null;
|
2023-01-15 20:45:37 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return await ReleaseAsync(key);
|
2023-02-11 21:39:06 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
public async Task<Key> ReleaseAsync(Key key)
|
2023-02-11 21:39:06 -06:00
|
|
|
|
{
|
2026-04-12 19:58:47 -05:00
|
|
|
|
key.ClaimedByUser = null;
|
|
|
|
|
|
key.ClaimedByMacAddress = null;
|
|
|
|
|
|
key.ClaimedOn = null;
|
|
|
|
|
|
key.AllocationMethod = null;
|
2023-01-15 20:45:37 -06:00
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
|
return await UpdateAsync(key);
|
2023-01-15 20:45:37 -06:00
|
|
|
|
}
|
2023-01-09 01:05:40 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|