2026-07-02 18:09:14 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
2025-03-29 17:58:10 -05:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2026-07-02 20:01:04 -05:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2025-03-29 17:58:10 -05:00
|
|
|
|
using Action = LANCommander.Server.Data.Models.Action;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Server.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public sealed class ActionService(
|
|
|
|
|
|
ILogger<ActionService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-03-29 17:58:10 -05:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2026-06-30 19:32:06 -05:00
|
|
|
|
GameVersionService gameVersionService,
|
2026-07-02 18:09:14 -05:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory) : BaseDatabaseService<Action>(logger, settingsProvider, cache, httpContextAccessor, contextFactory)
|
2025-03-29 17:58:10 -05:00
|
|
|
|
{
|
|
|
|
|
|
public override async Task<Action> AddAsync(Action entity)
|
|
|
|
|
|
{
|
2026-06-30 19:32:06 -05:00
|
|
|
|
// Actions for a game are version-scoped. Attach new actions to the game's current
|
|
|
|
|
|
// version here so callers don't have to resolve and pass it in themselves.
|
|
|
|
|
|
if (entity.GameId.HasValue && entity.GameId != Guid.Empty
|
|
|
|
|
|
&& (entity.GameVersionId == null || entity.GameVersionId == Guid.Empty))
|
|
|
|
|
|
entity.GameVersionId = await gameVersionService.GetOrCreateLatestIdAsync(entity.GameId.Value);
|
|
|
|
|
|
|
2026-07-02 20:01:04 -05:00
|
|
|
|
await cache.ExpireGameCacheAsync();
|
|
|
|
|
|
|
2025-03-29 17:58:10 -05:00
|
|
|
|
return await base.AddAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Server);
|
2026-02-08 02:24:53 -06:00
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Tool);
|
2025-03-29 17:58:10 -05:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async override Task<Action> UpdateAsync(Action entity)
|
|
|
|
|
|
{
|
2026-07-02 20:01:04 -05:00
|
|
|
|
await cache.ExpireGameCacheAsync();
|
|
|
|
|
|
|
2025-03-29 17:58:10 -05:00
|
|
|
|
return await base.UpdateAsync(entity, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Server);
|
2026-02-08 02:24:53 -06:00
|
|
|
|
await context.UpdateRelationshipAsync(a => a.Tool);
|
2025-03-29 17:58:10 -05:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2026-07-02 20:01:04 -05:00
|
|
|
|
|
|
|
|
|
|
public override async Task DeleteAsync(Action entity)
|
|
|
|
|
|
{
|
|
|
|
|
|
await cache.ExpireGameCacheAsync();
|
|
|
|
|
|
|
|
|
|
|
|
await base.DeleteAsync(entity);
|
|
|
|
|
|
}
|
2025-03-29 17:58:10 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|