LANCommander/LANCommander.Server.Services/GameSaveService.cs
Pat Hartl add770b227 Refactor Settings
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 => { ... })
2025-11-16 12:31:25 -06:00

80 lines
2.9 KiB
C#

using AutoMapper;
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
using LANCommander.Helpers;
using LANCommander.SDK.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Services
{
public sealed class GameSaveService(
ILogger<GameSaveService> logger,
SettingsProvider<Settings.Settings> settingsProvider,
IFusionCache cache,
IMapper mapper,
IHttpContextAccessor httpContextAccessor,
IDbContextFactory<DatabaseContext> contextFactory,
StorageLocationService storageLocationService) : BaseDatabaseService<GameSave>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
{
public override async Task<GameSave> AddAsync(GameSave entity)
{
return await base.AddAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(gs => gs.Game);
await context.UpdateRelationshipAsync(gs => gs.StorageLocation);
await context.UpdateRelationshipAsync(gs => gs.User);
});
}
public override async Task<GameSave> UpdateAsync(GameSave entity)
{
return await base.UpdateAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(gs => gs.Game);
await context.UpdateRelationshipAsync(gs => gs.StorageLocation);
await context.UpdateRelationshipAsync(gs => gs.User);
});
}
public override async Task DeleteAsync(GameSave entity)
{
FileHelpers.DeleteIfExists(await GetSavePathAsync(entity.Id));
await base.DeleteAsync(entity);
}
public async Task<string> GetSavePathAsync(Guid gameId, Guid userId)
{
var save = await SortBy(gs => gs.CreatedOn, Data.Enums.SortDirection.Descending).FirstOrDefaultAsync(gs => gs.GameId == gameId && gs.UserId == userId);
if (save == null)
return null;
return GetSavePath(save);
}
public async Task<string> GetSavePathAsync(Guid id)
{
// Use get with predicate to avoid async
var save = await FirstOrDefaultAsync(gs => gs.Id == id);
if (save == null)
return null;
return GetSavePath(save);
}
public string GetSavePath(GameSave save)
{
return Path.Combine(save.StorageLocation.Path, save.UserId.ToString(), save.GameId.ToString(), $"{save.Id}");
}
public async Task<StorageLocation> GetDefaultStorageLocationAsync()
{
return await storageLocationService.FirstOrDefaultAsync(l => l.Type == StorageLocationType.Save && l.Default);
}
}
}