LANCommander/LANCommander.Server.Services/LibraryService.cs

105 lines
3.6 KiB
C#
Raw Permalink Normal View History

using System.Configuration;
using LANCommander.Server.Data;
using LANCommander.Server.Data.Models;
using Microsoft.Extensions.Logging;
using AutoMapper;
2025-02-09 01:37:31 -06:00
using LANCommander.Server.Services.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using ZiggyCreatures.Caching.Fusion;
namespace LANCommander.Server.Services
{
public sealed class LibraryService(
ILogger<LibraryService> logger,
SettingsProvider<Settings.Settings> settingsProvider,
IFusionCache cache,
IMapper mapper,
IHttpContextAccessor httpContextAccessor,
IDbContextFactory<DatabaseContext> contextFactory,
GameService gameService,
UserService userService) : BaseDatabaseService<Library>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
{
2025-02-23 08:49:43 -06:00
public override async Task<Library> AddAsync(Library entity)
{
return await base.AddAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(l => l.Games);
await context.UpdateRelationshipAsync(l => l.User);
});
}
2025-02-02 14:58:07 -06:00
public override async Task<Library> UpdateAsync(Library entity)
{
2025-02-02 14:58:07 -06:00
return await base.UpdateAsync(entity, async context =>
{
await context.UpdateRelationshipAsync(l => l.Games);
await context.UpdateRelationshipAsync(l => l.User);
});
}
public async Task<Library> GetByUserIdAsync(Guid userId)
{
var library = await Include(l => l.Games).FirstOrDefaultAsync(l => l.User.Id == userId);
if (library == null)
{
var user = await userService.GetAsync(userId);
if (user == null)
throw new Exception("User not found with ID " + userId.ToString());
library = await AddAsync(new Library { UserId = userId });
}
return library;
}
public async Task AddToLibraryAsync(Guid userId, Guid gameId)
{
var game = await gameService.GetAsync(gameId);
var library = await GetByUserIdAsync(userId);
library.Games.Add(game);
await UpdateAsync(library);
2025-02-09 01:37:31 -06:00
await cache.RemoveByTagAsync([$"Library/{userId}"]);
if (game.BaseGame != null && !library.Games.Any(g => g.Id == game.BaseGame.Id) && game.BaseGame.Id != game.Id)
await AddToLibraryAsync(userId, game.BaseGame.Id);
}
public Task RemoveFromLibraryAsync(Guid userId, Guid gameId)
{
return RemoveFromLibraryAsync(userId, gameId, addonIds: []);
}
public async Task RemoveFromLibraryAsync(Guid userId, Guid gameId, Guid[] addonIds)
{
var library = await GetByUserIdAsync(userId);
var game = library.Games.FirstOrDefault(g => g.Id == gameId);
if (game != null && game.DependentGames != null && game.DependentGames.Any())
{
addonIds ??= [];
foreach (var dependentGame in game.DependentGames)
{
if (dependentGame.IsAddon || addonIds.Contains(dependentGame.Id))
{
if (library.Games.Any(g => g.Id == dependentGame.Id) && dependentGame.Id != game.Id)
library.Games.Remove(dependentGame);
}
}
}
library.Games.Remove(game!);
await UpdateAsync(library);
2025-02-09 01:37:31 -06:00
await cache.RemoveByTagAsync([$"Library/{userId}"]);
}
}
}