99 lines
4.5 KiB
C#
99 lines
4.5 KiB
C#
using Dapper;
|
|
using PlotLine.Data;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
public interface IStoryIntelligenceDevelopmentResetService
|
|
{
|
|
Task<StoryIntelligenceDevelopmentResetResult> ResetOwnedProjectsAndImportsAsync(int userId, CancellationToken cancellationToken = default);
|
|
}
|
|
|
|
public sealed class StoryIntelligenceDevelopmentResetService(
|
|
ISqlConnectionFactory connectionFactory,
|
|
IProjectRepository projects,
|
|
ILogger<StoryIntelligenceDevelopmentResetService> logger,
|
|
IHostEnvironment environment) : IStoryIntelligenceDevelopmentResetService
|
|
{
|
|
public async Task<StoryIntelligenceDevelopmentResetResult> ResetOwnedProjectsAndImportsAsync(int userId, CancellationToken cancellationToken = default)
|
|
{
|
|
if (!environment.IsDevelopment()) throw new InvalidOperationException("Import reset is available only in Development.");
|
|
logger.LogWarning("Admin Story Intelligence reset started for user {UserId}.", userId);
|
|
|
|
using var connection = connectionFactory.CreateConnection();
|
|
var command = new CommandDefinition("dbo.StoryIntelligenceDevelopmentReset_Prepare", new { UserID = userId }, commandType: System.Data.CommandType.StoredProcedure, cancellationToken: cancellationToken, commandTimeout: 120);
|
|
var summary = await connection.QuerySingleAsync<PreparedResetSummary>(command);
|
|
|
|
foreach (var projectId in summary.ProjectIdList)
|
|
{
|
|
logger.LogWarning("Admin Story Intelligence reset hard-deleting archived project {ProjectID} for user {UserId}.", projectId, userId);
|
|
await projects.HardDeleteForOwnerAsync(projectId, userId);
|
|
}
|
|
|
|
var verificationCommand = new CommandDefinition("dbo.StoryIntelligenceDevelopmentReset_Verify", new { UserID = userId }, commandType: System.Data.CommandType.StoredProcedure, cancellationToken: cancellationToken, commandTimeout: 120);
|
|
var verification = await connection.QuerySingleAsync<ResetVerification>(verificationCommand);
|
|
var result = new StoryIntelligenceDevelopmentResetResult(
|
|
summary.ProjectIdList.Count,
|
|
summary.ImportSessionCount,
|
|
summary.RunCount,
|
|
verification.RemainingProjects,
|
|
verification.RemainingImports,
|
|
verification.RemainingRuns,
|
|
verification.PreservedIllustrationItems);
|
|
|
|
logger.LogWarning(
|
|
"Admin Story Intelligence reset completed for user {UserId}. DeletedProjects={DeletedProjects} DeletedImports={DeletedImports} DeletedRuns={DeletedRuns} PreservedIllustrationItems={PreservedIllustrationItems}.",
|
|
userId,
|
|
result.DeletedProjects,
|
|
result.DeletedImportSessions,
|
|
result.DeletedRuns,
|
|
result.PreservedIllustrationItems);
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private sealed class PreparedResetSummary
|
|
{
|
|
public string ProjectIds { get; init; } = string.Empty;
|
|
public int ImportSessionCount { get; init; }
|
|
public int RunCount { get; init; }
|
|
|
|
public IReadOnlyList<int> ProjectIdList => SplitProjectIds(ProjectIds);
|
|
}
|
|
|
|
private sealed class ResetVerification
|
|
{
|
|
public int RemainingProjects { get; init; }
|
|
public int RemainingImports { get; init; }
|
|
public int RemainingRuns { get; init; }
|
|
public int PreservedIllustrationItems { get; init; }
|
|
}
|
|
|
|
private static IReadOnlyList<int> SplitProjectIds(string projectIds)
|
|
=> string.IsNullOrWhiteSpace(projectIds)
|
|
? []
|
|
: projectIds.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
|
.Select(value => int.TryParse(value, out var id) ? id : 0)
|
|
.Where(id => id > 0)
|
|
.ToArray();
|
|
}
|
|
|
|
public sealed record StoryIntelligenceDevelopmentResetResult(
|
|
int DeletedProjects,
|
|
int DeletedImportSessions,
|
|
int DeletedRuns,
|
|
int RemainingProjects,
|
|
int RemainingImportSessions,
|
|
int RemainingRuns,
|
|
int PreservedIllustrationItems)
|
|
{
|
|
public bool Success => RemainingProjects == 0 && RemainingImportSessions == 0 && RemainingRuns == 0;
|
|
|
|
public string Message => Success
|
|
? $"Reset complete. Deleted {DeletedProjects:N0} project(s), {DeletedImportSessions:N0} import(s) and {DeletedRuns:N0} run(s). Preserved {PreservedIllustrationItems:N0} generated image record(s)."
|
|
: $"Reset finished with remaining data: {RemainingProjects:N0} project(s), {RemainingImportSessions:N0} import(s), {RemainingRuns:N0} run(s). Preserved {PreservedIllustrationItems:N0} generated image record(s).";
|
|
}
|