PlotDirector/PlotLine/Data/ProjectBackupRepository.cs

306 lines
20 KiB
C#

using System.Data;
using Dapper;
using PlotLine.Models;
namespace PlotLine.Data;
public interface IProjectBackupRepository
{
Task<ProjectBackupExport?> GetProjectBackupAsync(int projectId, int? userId = null);
}
public sealed class ProjectBackupRepository(ISqlConnectionFactory connectionFactory) : IProjectBackupRepository
{
public async Task<ProjectBackupExport?> GetProjectBackupAsync(int projectId, int? userId = null)
{
using var connection = connectionFactory.CreateConnection();
using var result = await connection.QueryMultipleAsync(
"dbo.ProjectBackup_Export",
new { ProjectID = projectId, UserID = userId },
commandType: CommandType.StoredProcedure,
commandTimeout: 180);
var project = await result.ReadSingleOrDefaultAsync<Project>();
var data = new ProjectBackupData
{
Books = (await result.ReadAsync<Book>()).ToList(),
Chapters = (await result.ReadAsync<Chapter>()).ToList(),
Scenes = (await result.ReadAsync<Scene>()).ToList(),
ScenePurposes = (await result.ReadAsync<ProjectBackupScenePurpose>()).ToList(),
SceneOutcomes = (await result.ReadAsync<ProjectBackupSceneOutcome>()).ToList(),
SceneMetricTypes = (await result.ReadAsync<SceneMetricType>()).ToList(),
SceneMetricValues = (await result.ReadAsync<SceneMetricValue>()).ToList(),
TimelineSettings = (await result.ReadAsync<ProjectTimelineSettings>()).ToList(),
TimelineMetricSettings = (await result.ReadAsync<ProjectTimelineMetricSetting>()).ToList(),
TimelineViewPresets = (await result.ReadAsync<TimelineViewPreset>()).ToList(),
PlotLines = (await result.ReadAsync<PlotLineItem>()).ToList(),
PlotThreads = (await result.ReadAsync<PlotThread>()).ToList(),
ThreadEvents = (await result.ReadAsync<ThreadEvent>()).ToList(),
StoryAssets = (await result.ReadAsync<StoryAsset>()).ToList(),
AssetStates = (await result.ReadAsync<AssetState>()).ToList(),
AssetEvents = (await result.ReadAsync<AssetEvent>()).ToList(),
AssetDependencies = (await result.ReadAsync<AssetDependency>()).ToList(),
AssetCustodyEvents = (await result.ReadAsync<AssetCustodyEvent>()).ToList(),
AssetCustodyCharacters = (await result.ReadAsync<ProjectBackupAssetCustodyCharacter>()).ToList(),
Characters = (await result.ReadAsync<Character>()).ToList(),
SceneCharacters = (await result.ReadAsync<SceneCharacter>()).ToList(),
CharacterAttributeTypes = (await result.ReadAsync<CharacterAttributeType>()).ToList(),
CharacterAttributeEvents = (await result.ReadAsync<CharacterAttributeEvent>()).ToList(),
CharacterKnowledge = (await result.ReadAsync<CharacterKnowledgeItem>()).ToList(),
CharacterRelationships = (await result.ReadAsync<CharacterRelationship>()).ToList(),
RelationshipEvents = (await result.ReadAsync<RelationshipEvent>()).ToList(),
Locations = (await result.ReadAsync<LocationItem>()).ToList(),
LocationRelationships = (await result.ReadAsync<LocationRelationship>()).ToList(),
FloorPlans = (await result.ReadAsync<FloorPlan>()).ToList(),
FloorPlanFloors = (await result.ReadAsync<FloorPlanFloor>()).ToList(),
FloorPlanBlocks = (await result.ReadAsync<FloorPlanBlock>()).ToList(),
FloorPlanTransitions = (await result.ReadAsync<FloorPlanTransition>()).ToList(),
SceneAssetLocations = (await result.ReadAsync<SceneAssetLocation>()).ToList(),
SceneFloorPlanOccupancy = (await result.ReadAsync<SceneFloorPlanOccupancy>()).ToList(),
SceneDependencies = (await result.ReadAsync<SceneDependency>()).ToList(),
ValidationRuns = (await result.ReadAsync<ProjectBackupValidationRun>()).ToList(),
ContinuityWarnings = (await result.ReadAsync<ContinuityWarning>()).ToList(),
SceneNotes = (await result.ReadAsync<SceneNote>()).ToList(),
SceneWorkflow = (await result.ReadAsync<SceneWorkflow>()).ToList(),
SceneChecklistItems = (await result.ReadAsync<SceneChecklistItem>()).ToList(),
SceneAttachments = (await result.ReadAsync<SceneAttachment>()).ToList(),
ChapterGoals = (await result.ReadAsync<ChapterGoal>()).ToList(),
Scenarios = (await result.ReadAsync<Scenario>()).ToList(),
ScenarioSceneOrder = (await result.ReadAsync<ProjectBackupScenarioSceneOrder>()).ToList(),
ScenarioValidationRuns = (await result.ReadAsync<ProjectBackupScenarioValidationRun>()).ToList(),
ScenarioWarnings = (await result.ReadAsync<ScenarioWarning>()).ToList(),
GenreMetricPresets = (await result.ReadAsync<GenreMetricPreset>()).ToList(),
GenreMetricPresetMetrics = (await result.ReadAsync<ProjectBackupGenreMetricPresetMetric>()).ToList(),
ProjectAccess = (await result.ReadAsync<ProjectBackupAccessRecord>()).ToList(),
UserFiles = (await result.ReadAsync<UserFile>()).ToList()
};
if (project is null)
{
return null;
}
using (var mapRows = await connection.QueryMultipleAsync("dbo.StoryMap_Backup", new { ProjectID = projectId, UserID = userId }, commandType: CommandType.StoredProcedure))
{
data.StoryMaps = (await mapRows.ReadAsync<StoryMap>()).ToList();
data.MapFeatures = (await mapRows.ReadAsync<MapFeature>()).ToList();
data.MapRelationships = (await mapRows.ReadAsync<MapRelationship>()).ToList();
data.MapJourneyNotes = (await mapRows.ReadAsync<MapJourneyNote>()).ToList();
data.MapReferenceImages = (await mapRows.ReadAsync<MapReferenceImage>()).ToList();
}
using (var state = await connection.QueryMultipleAsync("dbo.ProjectBackup_PersistentState_List", new { ProjectID = projectId }, commandType: CommandType.StoredProcedure, commandTimeout: 180))
{
data.ManuscriptDocuments = (await state.ReadAsync<ManuscriptDocumentModel>()).ToList();
data.SceneManuscriptSources = (await state.ReadAsync<ProjectBackupSceneManuscriptSource>()).ToList();
data.ManuscriptStructuralChapterSources = (await state.ReadAsync<ProjectBackupStructuralChapterSource>()).ToList();
data.WritingPlans = (await state.ReadAsync<WritingPlan>()).ToList();
data.WritingScheduleItems = (await state.ReadAsync<WritingScheduleItem>()).ToList();
data.CharacterAliases = (await state.ReadAsync<CharacterAlias>()).ToList();
data.AssetAliases = (await state.ReadAsync<AssetAlias>()).ToList();
data.LocationAliases = (await state.ReadAsync<LocationAlias>()).ToList();
data.AuthorPlotSeeds = (await state.ReadAsync<AuthorPlotSeed>()).ToList();
data.ContinuityWarningAcknowledgements = (await state.ReadAsync<ContinuityWarningAcknowledgement>()).ToList();
data.PlotEventTargetPlotLines = (await state.ReadAsync<ProjectBackupPlotEventTarget>()).ToList();
data.WordCompanionSceneAssets = (await state.ReadAsync<ProjectBackupCompanionSceneAsset>()).ToList();
data.WordCompanionSceneLocations = (await state.ReadAsync<ProjectBackupCompanionSceneLocation>()).ToList();
data.CharacterImages = (await state.ReadAsync<CharacterImage>()).ToList();
data.OnboardingManuscriptBuilds = (await state.ReadAsync<ProjectBackupOnboardingManuscriptBuilds>()).ToList();
data.OnboardingManuscriptBuildChapterMap = (await state.ReadAsync<ProjectBackupOnboardingManuscriptBuildChapterMap>()).ToList();
data.OnboardingManuscriptBuildSceneMap = (await state.ReadAsync<ProjectBackupOnboardingManuscriptBuildSceneMap>()).ToList();
data.OnboardingManuscriptBuildCharacterMap = (await state.ReadAsync<ProjectBackupOnboardingManuscriptBuildCharacterMap>()).ToList();
data.SceneAssetSuggestions = (await state.ReadAsync<ProjectBackupSceneAssetSuggestions>()).ToList();
data.SceneCharacterSuggestions = (await state.ReadAsync<ProjectBackupSceneCharacterSuggestions>()).ToList();
data.SceneLocationSuggestions = (await state.ReadAsync<ProjectBackupSceneLocationSuggestions>()).ToList();
data.SceneTextStatistics = (await state.ReadAsync<ProjectBackupSceneTextStatistics>()).ToList();
data.CoreImportBasicLocationReviews = (await state.ReadAsync<ProjectBackupCoreImportBasicLocationReviews>()).ToList();
data.CoreImportBasicLocationCandidates = (await state.ReadAsync<ProjectBackupCoreImportBasicLocationCandidates>()).ToList();
data.CoreImportBasicLocationEvidence = (await state.ReadAsync<ProjectBackupCoreImportBasicLocationEvidence>()).ToList();
data.StoryIntelligenceRuns = (await state.ReadAsync<ProjectBackupStoryIntelligenceRuns>()).ToList();
data.StoryIntelligenceChapterResults = (await state.ReadAsync<ProjectBackupStoryIntelligenceChapterResults>()).ToList();
data.StoryIntelligenceSceneResults = (await state.ReadAsync<ProjectBackupStoryIntelligenceSceneResults>()).ToList();
data.StoryIntelligenceImportCommits = (await state.ReadAsync<ProjectBackupStoryIntelligenceImportCommits>()).ToList();
data.StoryIntelligenceBookPipelines = (await state.ReadAsync<ProjectBackupStoryIntelligenceBookPipelines>()).ToList();
data.StoryMemoryCharacters = (await state.ReadAsync<ProjectBackupStoryMemoryCharacters>()).ToList();
data.StoryMemoryCharacterAliases = (await state.ReadAsync<ProjectBackupStoryMemoryCharacterAliases>()).ToList();
data.StoryMemoryCharacterAttributes = (await state.ReadAsync<ProjectBackupStoryMemoryCharacterAttributes>()).ToList();
data.StoryMemoryLocations = (await state.ReadAsync<ProjectBackupStoryMemoryLocations>()).ToList();
data.StoryMemoryAssets = (await state.ReadAsync<ProjectBackupStoryMemoryAssets>()).ToList();
data.StoryMemoryRelationships = (await state.ReadAsync<ProjectBackupStoryMemoryRelationships>()).ToList();
data.StoryMemoryProcessedSceneResults = (await state.ReadAsync<ProjectBackupStoryMemoryProcessedSceneResults>()).ToList();
data.StoryMemoryImportPreferences = (await state.ReadAsync<ProjectBackupStoryMemoryImportPreferences>()).ToList();
data.CharacterIntelligenceProfiles = (await state.ReadAsync<ProjectBackupCharacterIntelligenceProfiles>()).ToList();
data.CharacterIntelligenceEvidenceEvents = (await state.ReadAsync<ProjectBackupCharacterIntelligenceEvidenceEvents>()).ToList();
data.StoryIntelligenceReviewCandidateGenerations = (await state.ReadAsync<ProjectBackupStoryIntelligenceReviewCandidateGenerations>()).ToList();
data.StoryIntelligenceReviewCandidates = (await state.ReadAsync<ProjectBackupStoryIntelligenceReviewCandidates>()).ToList();
data.StoryIntelligenceReviewCandidateEvidence = (await state.ReadAsync<ProjectBackupStoryIntelligenceReviewCandidateEvidence>()).ToList();
data.CharacterEnrichmentRuns = (await state.ReadAsync<ProjectBackupCharacterEnrichmentRuns>()).ToList();
data.CharacterEnrichmentEvidence = (await state.ReadAsync<ProjectBackupCharacterEnrichmentEvidence>()).ToList();
data.WholeBookPlotSynthesisRuns = (await state.ReadAsync<ProjectBackupWholeBookPlotSynthesisRuns>()).ToList();
data.StoryIntelligenceNarrativeCandidates = (await state.ReadAsync<ProjectBackupStoryIntelligenceNarrativeCandidates>()).ToList();
data.StoryIntelligenceNarrativeCandidateEvidence = (await state.ReadAsync<ProjectBackupStoryIntelligenceNarrativeCandidateEvidence>()).ToList();
data.StoryIntelligenceNarrativeCandidateEvents = (await state.ReadAsync<ProjectBackupStoryIntelligenceNarrativeCandidateEvents>()).ToList();
data.StoryMemoryIllustrationAssignments = (await state.ReadAsync<ProjectBackupStoryMemoryIllustrationAssignments>()).ToList();
data.StoryMemoryIllustrationDemands = (await state.ReadAsync<ProjectBackupStoryMemoryIllustrationDemands>()).ToList();
data.StoryIntelligenceIllustrationDemands = (await state.ReadAsync<ProjectBackupStoryIntelligenceIllustrationDemands>()).ToList();
}
var bookMetadata = (await connection.QueryAsync<Book>(
"dbo.ProjectBackup_BookMetadata_List",
new { ProjectID = projectId },
commandType: CommandType.StoredProcedure)).ToDictionary(x => x.BookID);
foreach (var book in data.Books)
{
if (!bookMetadata.TryGetValue(book.BookID, out var metadata))
{
continue;
}
book.UsesExplicitScenes = metadata.UsesExplicitScenes;
book.StoryEra = metadata.StoryEra;
book.PrimaryLocaleCode = metadata.PrimaryLocaleCode;
book.Subtitle = metadata.Subtitle;
book.Tagline = metadata.Tagline;
book.ShortDescription = metadata.ShortDescription;
book.Status = metadata.Status;
book.TargetWordCount = metadata.TargetWordCount;
book.WritingStartDate = metadata.WritingStartDate;
book.TargetCompletionDate = metadata.TargetCompletionDate;
book.PublicationDate = metadata.PublicationDate;
book.CoverThumbnailPath = metadata.CoverThumbnailPath;
book.CoverStandardPath = metadata.CoverStandardPath;
}
return new ProjectBackupExport
{
ExportedAtUtc = DateTime.UtcNow,
Project = project,
Data = data,
Diagnostics = new ProjectBackupDiagnostics
{
ProjectId = projectId,
IncludedEntitySets =
[
nameof(data.WholeBookPlotSynthesisRuns),
nameof(data.StoryIntelligenceNarrativeCandidates),
nameof(data.StoryIntelligenceNarrativeCandidateEvidence),
nameof(data.StoryIntelligenceNarrativeCandidateEvents),
nameof(data.StoryMemoryIllustrationAssignments),
nameof(data.StoryMemoryIllustrationDemands),
nameof(data.StoryIntelligenceIllustrationDemands),
nameof(data.CharacterEnrichmentRuns),
nameof(data.CharacterEnrichmentEvidence),
nameof(data.StoryIntelligenceReviewCandidateGenerations),
nameof(data.StoryIntelligenceReviewCandidates),
nameof(data.StoryIntelligenceReviewCandidateEvidence),
nameof(data.StoryIntelligenceBookPipelines),
nameof(data.StoryMemoryCharacters),
nameof(data.StoryMemoryCharacterAliases),
nameof(data.StoryMemoryCharacterAttributes),
nameof(data.StoryMemoryLocations),
nameof(data.StoryMemoryAssets),
nameof(data.StoryMemoryRelationships),
nameof(data.StoryMemoryProcessedSceneResults),
nameof(data.StoryMemoryImportPreferences),
nameof(data.CharacterIntelligenceProfiles),
nameof(data.CharacterIntelligenceEvidenceEvents),
nameof(data.StoryIntelligenceRuns),
nameof(data.StoryIntelligenceChapterResults),
nameof(data.StoryIntelligenceSceneResults),
nameof(data.StoryIntelligenceImportCommits),
nameof(data.CoreImportBasicLocationReviews),
nameof(data.CoreImportBasicLocationCandidates),
nameof(data.CoreImportBasicLocationEvidence),
nameof(data.SceneAssetSuggestions),
nameof(data.SceneCharacterSuggestions),
nameof(data.SceneLocationSuggestions),
nameof(data.SceneTextStatistics),
nameof(data.OnboardingManuscriptBuilds),
nameof(data.OnboardingManuscriptBuildChapterMap),
nameof(data.OnboardingManuscriptBuildSceneMap),
nameof(data.OnboardingManuscriptBuildCharacterMap),
nameof(data.CharacterImages),
nameof(data.WordCompanionSceneAssets),
nameof(data.WordCompanionSceneLocations),
nameof(data.ManuscriptDocuments),
nameof(data.SceneManuscriptSources),
nameof(data.ManuscriptStructuralChapterSources),
nameof(data.WritingPlans),
nameof(data.WritingScheduleItems),
nameof(data.CharacterAliases),
nameof(data.AssetAliases),
nameof(data.LocationAliases),
nameof(data.AuthorPlotSeeds),
nameof(data.ContinuityWarningAcknowledgements),
nameof(data.PlotEventTargetPlotLines),
nameof(data.Books),
nameof(data.Chapters),
nameof(data.Scenes),
nameof(data.ScenePurposes),
nameof(data.SceneOutcomes),
nameof(data.SceneMetricTypes),
nameof(data.SceneMetricValues),
nameof(data.TimelineSettings),
nameof(data.TimelineMetricSettings),
nameof(data.TimelineViewPresets),
nameof(data.PlotLines),
nameof(data.PlotThreads),
nameof(data.ThreadEvents),
nameof(data.StoryAssets),
nameof(data.AssetStates),
nameof(data.AssetEvents),
nameof(data.AssetDependencies),
nameof(data.AssetCustodyEvents),
nameof(data.AssetCustodyCharacters),
nameof(data.Characters),
nameof(data.SceneCharacters),
nameof(data.CharacterAttributeTypes),
nameof(data.CharacterAttributeEvents),
nameof(data.CharacterKnowledge),
nameof(data.CharacterRelationships),
nameof(data.RelationshipEvents),
nameof(data.Locations),
nameof(data.LocationRelationships),
nameof(data.MapJourneyNotes),
nameof(data.StoryMaps),
nameof(data.MapReferenceImages),
nameof(data.MapFeatures),
nameof(data.MapRelationships),
nameof(data.FloorPlans),
nameof(data.FloorPlanFloors),
nameof(data.FloorPlanBlocks),
nameof(data.FloorPlanTransitions),
nameof(data.SceneAssetLocations),
nameof(data.SceneFloorPlanOccupancy),
nameof(data.SceneDependencies),
nameof(data.ValidationRuns),
nameof(data.ContinuityWarnings),
nameof(data.SceneNotes),
nameof(data.SceneWorkflow),
nameof(data.SceneChecklistItems),
nameof(data.SceneAttachments),
nameof(data.ChapterGoals),
nameof(data.Scenarios),
nameof(data.ScenarioSceneOrder),
nameof(data.ScenarioValidationRuns),
nameof(data.ScenarioWarnings),
nameof(data.GenreMetricPresets),
nameof(data.GenreMetricPresetMetrics),
nameof(data.ProjectAccess),
nameof(data.UserFiles)
],
Note = "Original database IDs are included for diagnostics. Project access records are exported for reference only; importing a project backup creates a new project owned by the importing user and does not recreate collaborators. Account onboarding and analysis consent/job records stay in their original account and are not recreated or rewound by project transfer. Existing project activity history is retained on same-project restore; copies do not recreate it. Shared illustration catalogue entries, approval/usage state and library files require a separate administrator backup and are never recreated or rewound by project transfer. Project illustration assignment/demand history is included; copied assignments become inactive and catalogue links are detached."
}
};
}
}