LANCommander/LANCommander.Server.Services/GameSaveService.cs

85 lines
3.2 KiB
C#
Raw Permalink Normal View History

using AutoMapper;
using LANCommander.Server.Data;
2024-08-04 18:44:33 -05:00
using LANCommander.Server.Data.Models;
2023-01-17 17:57:12 -06:00
using LANCommander.Helpers;
using LANCommander.SDK.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ZiggyCreatures.Caching.Fusion;
using LANCommander.SDK;
2023-01-17 17:57:12 -06:00
2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Services
2023-01-17 17:57:12 -06:00
{
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)
2023-01-17 17:57:12 -06:00
{
2025-02-23 08:49:43 -06:00
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);
});
}
2025-02-02 14:58:07 -06:00
public override async Task<GameSave> UpdateAsync(GameSave entity)
{
2025-02-02 14:58:07 -06:00
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)
2023-01-17 17:57:12 -06:00
{
FileHelpers.DeleteIfExists(await GetSavePathAsync(entity.Id));
2023-01-17 17:57:12 -06:00
await base.DeleteAsync(entity);
2023-01-17 17:57:12 -06:00
}
public async Task<string?> GetSavePathAsync(Guid gameId, Guid userId)
2023-01-17 17:57:12 -06:00
{
2024-12-20 11:35:19 -06:00
var save = await SortBy(gs => gs.CreatedOn, Data.Enums.SortDirection.Descending).FirstOrDefaultAsync(gs => gs.GameId == gameId && gs.UserId == userId);
2023-01-17 17:57:12 -06:00
if (save == null)
return null;
WIP fix for MySQL connection concurrency issues This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL: - The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no. - Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory. - A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function. - Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml` - The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution. - The application can now log to Seq when using debug build - Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future. - Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled. - All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation. - Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
return GetSavePath(save);
2023-01-17 17:57:12 -06:00
}
public async Task<string?> GetSavePathAsync(Guid id)
2023-01-17 17:57:12 -06:00
{
// Use get with predicate to avoid async
var save = await FirstOrDefaultAsync(gs => gs.Id == id);
2023-01-17 17:57:12 -06:00
if (save == null)
return null;
2023-01-17 17:57:12 -06:00
return GetSavePath(save);
}
public string GetSavePath(GameSave save)
{
var gameId = save.GameId ?? throw new InvalidOperationException($"No game ID is available for save {save.Id}");
2025-12-19 14:51:06 +11:00
return Path.IsPathRooted(save.StorageLocation.Path) ?
Path.Combine(save.StorageLocation.Path, save.UserId.ToString(), gameId.ToString(), $"{save.Id}") :
Path.Combine(AppPaths.GetConfigDirectory(), save.StorageLocation.Path, save.UserId.ToString(), gameId.ToString(), $"{save.Id}");
2023-01-17 17:57:12 -06:00
}
public async Task<StorageLocation> GetDefaultStorageLocationAsync()
{
return await storageLocationService.FirstOrDefaultAsync(l => l.Type == StorageLocationType.Save && l.Default);
}
2023-01-17 17:57:12 -06:00
}
}