using LANCommander.Server.Data; using LANCommander.Server.Data.Models; using LANCommander.Server.Services.Extensions; using AutoMapper; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; using ZiggyCreatures.Caching.Fusion; namespace LANCommander.Server.Services { public sealed class RedistributableService( ILogger logger, IFusionCache cache, IMapper mapper, IHttpContextAccessor httpContextAccessor, IDbContextFactory contextFactory) : BaseDatabaseService(logger, cache, mapper, httpContextAccessor, contextFactory) { public override async Task AddAsync(Redistributable entity) { await cache.ExpireGameCacheAsync(); return await base.AddAsync(entity, async context => { await context.UpdateRelationshipAsync(r => r.Archives); await context.UpdateRelationshipAsync(r => r.Games); await context.UpdateRelationshipAsync(r => r.Pages); await context.UpdateRelationshipAsync(r => r.Scripts); }); } public override async Task UpdateAsync(Redistributable entity) { if (entity.Games != null && entity.Games.Any()) { foreach (var game in entity.Games) { await cache.ExpireGameCacheAsync(game?.Id); } } return await base.UpdateAsync(entity, async context => { await context.UpdateRelationshipAsync(r => r.Archives); await context.UpdateRelationshipAsync(r => r.Games); await context.UpdateRelationshipAsync(r => r.Pages); await context.UpdateRelationshipAsync(r => r.Scripts); }); } public async Task GetManifestAsync(Guid manifestId) { var redistributable = await AsNoTracking() .AsSplitQuery() .Query(q => { return q .Include(r => r.Archives) .Include(r => r.Scripts); }) .GetAsync(manifestId); return redistributable; } } }