2024-08-04 18:44:33 -05:00
|
|
|
|
using LANCommander.Server.Data;
|
|
|
|
|
|
using LANCommander.Server.Data.Models;
|
|
|
|
|
|
using LANCommander.Server.Models;
|
2024-10-07 18:04:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using AutoMapper;
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using LANCommander.SDK.Extensions;
|
2025-02-04 02:31:23 -06:00
|
|
|
|
using LANCommander.Server.Services.Extensions;
|
2025-02-09 18:53:40 -06:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-02-01 02:21:34 -06:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-30 17:44:10 -05:00
|
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2023-01-15 03:11:51 -06:00
|
|
|
|
|
2024-08-04 18:44:33 -05:00
|
|
|
|
namespace LANCommander.Server.Services
|
2023-01-15 03:11:51 -06:00
|
|
|
|
{
|
2025-02-01 02:21:34 -06:00
|
|
|
|
public sealed class ScriptService(
|
|
|
|
|
|
ILogger<ScriptService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2025-02-01 02:21:34 -06:00
|
|
|
|
IFusionCache cache,
|
|
|
|
|
|
IMapper mapper,
|
2025-02-09 18:53:40 -06:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
2025-11-16 12:31:25 -06:00
|
|
|
|
IDbContextFactory<DatabaseContext> contextFactory) : BaseDatabaseService<Script>(logger, settingsProvider, cache, mapper, httpContextAccessor, contextFactory)
|
2023-01-15 03:11:51 -06:00
|
|
|
|
{
|
2025-02-04 02:31:23 -06:00
|
|
|
|
public override async Task<Script> AddAsync(Script script)
|
2025-02-01 02:21:34 -06:00
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
|
|
|
|
|
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
|
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
2025-02-23 08:49:43 -06:00
|
|
|
|
return await base.AddAsync(script, async context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Redistributable);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Server);
|
|
|
|
|
|
});
|
2025-02-04 02:31:23 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override async Task<Script> UpdateAsync(Script script)
|
|
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
|
|
|
|
|
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
|
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
return await base.UpdateAsync(script, async context =>
|
2025-02-02 14:58:07 -06:00
|
|
|
|
{
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Game);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Redistributable);
|
|
|
|
|
|
await context.UpdateRelationshipAsync(s => s.Server);
|
|
|
|
|
|
});
|
2025-02-01 02:21:34 -06:00
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
public override async Task DeleteAsync(Script script)
|
|
|
|
|
|
{
|
2025-02-05 01:32:00 -06:00
|
|
|
|
using var context = await contextFactory.CreateDbContextAsync();
|
|
|
|
|
|
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(script?.GameId);
|
|
|
|
|
|
|
|
|
|
|
|
if (script.RedistributableId?.IsNullOrEmpty() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var games = await context
|
|
|
|
|
|
.Games
|
|
|
|
|
|
.Include(g => g.Redistributables)
|
|
|
|
|
|
.Where(g => g.Redistributables.Any(r => r.Id == script.RedistributableId))
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var game in games)
|
|
|
|
|
|
await cache.ExpireGameCacheAsync(game.Id);
|
|
|
|
|
|
}
|
2025-02-04 02:31:23 -06:00
|
|
|
|
|
|
|
|
|
|
await base.DeleteAsync(script);
|
|
|
|
|
|
}
|
2025-02-01 02:21:34 -06:00
|
|
|
|
|
2023-01-26 20:42:33 -06:00
|
|
|
|
public static IEnumerable<Snippet> GetSnippets()
|
|
|
|
|
|
{
|
|
|
|
|
|
var files = Directory.GetFiles(@"Snippets", "*.ps1", SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
|
|
|
|
return files.Select(f =>
|
|
|
|
|
|
{
|
2023-12-04 18:30:34 +01:00
|
|
|
|
var split = f.Split(Path.DirectorySeparatorChar);
|
2023-01-26 20:42:33 -06:00
|
|
|
|
|
|
|
|
|
|
return new Snippet()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = Path.GetFileNameWithoutExtension(f),
|
|
|
|
|
|
Group = split[1],
|
|
|
|
|
|
Content = File.ReadAllText(f)
|
|
|
|
|
|
};
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2023-01-15 03:11:51 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|