225 lines
13 KiB
C#
225 lines
13 KiB
C#
using Dapper;
|
|
using PlotLine.Models;
|
|
|
|
namespace PlotLine.Data;
|
|
|
|
public interface IStoryMemoryRepository
|
|
{
|
|
Task ClearDerivedStateAsync(int importSessionId);
|
|
Task<IReadOnlySet<int>> ListProcessedSceneResultIdsAsync(int importSessionId, string processorVersion);
|
|
Task MarkSceneResultProcessedAsync(int importSessionId, int sceneResultId, string processorVersion, string snapshotHash);
|
|
Task<StoryMemoryCharacter> UpsertCharacterAsync(StoryMemoryCharacterSave request);
|
|
Task AddCharacterAliasAsync(int characterId, string alias, string normalisedAlias, string reason, int sceneResultId, decimal confidence);
|
|
Task AddCharacterAttributeAsync(int characterId, string attributeType, string value, decimal confidence, bool isExplicit, string evidenceSummary, int sceneResultId);
|
|
Task<StoryMemoryLocation> UpsertLocationAsync(StoryMemoryLocationSave request);
|
|
Task<StoryMemoryAsset> UpsertAssetAsync(StoryMemoryAssetSave request);
|
|
Task UpsertRelationshipAsync(int importSessionId, string sourceEntityType, int sourceEntityId, string targetEntityType, int targetEntityId, string relationshipType, decimal confidence, bool isExplicit, int sceneResultId);
|
|
Task<IReadOnlyList<StoryMemoryCharacter>> ListCharactersAsync(int importSessionId);
|
|
Task<IReadOnlyList<StoryMemoryCharacterAttribute>> ListCharacterAttributesAsync(int importSessionId);
|
|
Task<IReadOnlyList<StoryMemoryLocation>> ListLocationsAsync(int importSessionId);
|
|
Task<IReadOnlyList<StoryMemoryAsset>> ListAssetsAsync(int importSessionId);
|
|
Task<IReadOnlyList<StoryMemoryIllustrationAssignment>> ListAssignmentsAsync(int importSessionId);
|
|
Task<StoryMemoryIllustrationAssignment?> GetAssignmentAsync(int importSessionId, string entityType, int entityId);
|
|
Task UpsertAssignmentAsync(StoryMemoryIllustrationAssignmentSave request);
|
|
Task InvalidateAssignmentAsync(int assignmentId, string reason);
|
|
Task<StoryMemoryIllustrationDemand> UpsertDemandAsync(StoryMemoryIllustrationDemandSave request);
|
|
Task<IReadOnlyList<StoryMemoryIllustrationDemand>> ListDemandsAsync(int importSessionId);
|
|
Task<string> GetAppearancePreferenceAsync(int importSessionId);
|
|
Task<StoryMemoryImportContext> GetImportContextAsync(int importSessionId);
|
|
Task SetAppearancePreferenceAsync(int importSessionId, string preference);
|
|
Task SetImportContextAsync(int importSessionId, string appearancePreference, string? primaryAgeGroup, string? storyEra, string? storyLocation);
|
|
Task<StoryMemoryDiagnostics> GetDiagnosticsAsync(int importSessionId);
|
|
}
|
|
|
|
public sealed class StoryMemoryRepository(ISqlConnectionFactory connectionFactory) : IStoryMemoryRepository
|
|
{
|
|
public async Task ClearDerivedStateAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_ClearDerivedState",
|
|
new { ImportSessionID = importSessionId },commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<IReadOnlySet<int>> ListProcessedSceneResultIdsAsync(int importSessionId, string processorVersion)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
var ids = await connection.QueryAsync<int>(
|
|
"dbo.StoryMemory_ListProcessedSceneResultIds",
|
|
new { ImportSessionID = importSessionId, ProcessorVersion = processorVersion }, commandType: System.Data.CommandType.StoredProcedure);
|
|
return ids.ToHashSet();
|
|
}
|
|
|
|
public async Task MarkSceneResultProcessedAsync(int importSessionId, int sceneResultId, string processorVersion, string snapshotHash)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_MarkSceneResultProcessed",
|
|
new { ImportSessionID = importSessionId, SceneResultID = sceneResultId, ProcessorVersion = processorVersion, SnapshotHash = snapshotHash },commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<StoryMemoryCharacter> UpsertCharacterAsync(StoryMemoryCharacterSave request)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleAsync<StoryMemoryCharacter>(
|
|
"dbo.StoryMemory_UpsertCharacter",
|
|
request,commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task AddCharacterAliasAsync(int characterId, string alias, string normalisedAlias, string reason, int sceneResultId, decimal confidence)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_AddCharacterAlias",
|
|
new { StoryMemoryCharacterID = characterId, Alias = alias, NormalisedAlias = normalisedAlias, ResolutionReason = reason, SourceSceneResultID = sceneResultId, Confidence = confidence },commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task AddCharacterAttributeAsync(int characterId, string attributeType, string value, decimal confidence, bool isExplicit, string evidenceSummary, int sceneResultId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_AddCharacterAttribute",
|
|
new { StoryMemoryCharacterID = characterId, AttributeType = attributeType, NormalisedValue = value, Confidence = confidence, IsExplicit = isExplicit, EvidenceSummary = evidenceSummary, SourceSceneResultID = sceneResultId },commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<StoryMemoryLocation> UpsertLocationAsync(StoryMemoryLocationSave request)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleAsync<StoryMemoryLocation>(
|
|
"dbo.StoryMemory_UpsertLocation",
|
|
request,commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<StoryMemoryAsset> UpsertAssetAsync(StoryMemoryAssetSave request)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleAsync<StoryMemoryAsset>(
|
|
"dbo.StoryMemory_UpsertAsset",
|
|
request,commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task UpsertRelationshipAsync(int importSessionId, string sourceEntityType, int sourceEntityId, string targetEntityType, int targetEntityId, string relationshipType, decimal confidence, bool isExplicit, int sceneResultId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_UpsertRelationship",
|
|
new { ImportSessionID = importSessionId, SourceEntityType = sourceEntityType, SourceEntityID = sourceEntityId, TargetEntityType = targetEntityType, TargetEntityID = targetEntityId, RelationshipType = relationshipType, Confidence = confidence, IsExplicit = isExplicit, SceneResultID = sceneResultId },commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryCharacter>> ListCharactersAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryCharacter>("dbo.StoryMemory_ListCharacters", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryCharacterAttribute>> ListCharacterAttributesAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryCharacterAttribute>(
|
|
"dbo.StoryMemory_ListCharacterAttributes",
|
|
new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryLocation>> ListLocationsAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryLocation>("dbo.StoryMemory_ListLocations", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryAsset>> ListAssetsAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryAsset>("dbo.StoryMemory_ListAssets", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryIllustrationAssignment>> ListAssignmentsAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryIllustrationAssignment>("dbo.StoryMemory_ListAssignments", new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<StoryMemoryIllustrationAssignment?> GetAssignmentAsync(int importSessionId, string entityType, int entityId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleOrDefaultAsync<StoryMemoryIllustrationAssignment>(
|
|
"dbo.StoryMemory_GetAssignment",
|
|
new { ImportSessionID = importSessionId, EntityType = entityType, EntityID = entityId }, commandType: System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task UpsertAssignmentAsync(StoryMemoryIllustrationAssignmentSave request)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_UpsertAssignment",
|
|
request,commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task InvalidateAssignmentAsync(int assignmentId, string reason)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_InvalidateAssignment",
|
|
new { AssignmentID = assignmentId, Reason = reason }, commandType: System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<StoryMemoryIllustrationDemand> UpsertDemandAsync(StoryMemoryIllustrationDemandSave request)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleAsync<StoryMemoryIllustrationDemand>(
|
|
"dbo.StoryMemory_UpsertDemand",
|
|
request,commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<StoryMemoryIllustrationDemand>> ListDemandsAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return (await connection.QueryAsync<StoryMemoryIllustrationDemand>(
|
|
"dbo.StoryMemory_ListDemands",
|
|
new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)).ToList();
|
|
}
|
|
|
|
public async Task<string> GetAppearancePreferenceAsync(int importSessionId)
|
|
=> (await GetImportContextAsync(importSessionId)).AppearancePreference;
|
|
|
|
public async Task<StoryMemoryImportContext> GetImportContextAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleOrDefaultAsync<StoryMemoryImportContext>(
|
|
"dbo.StoryMemory_GetImportContext",
|
|
new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure)
|
|
?? new StoryMemoryImportContext(StoryMemoryAppearancePreferences.Balanced, string.Empty, string.Empty, string.Empty);
|
|
}
|
|
|
|
public async Task SetAppearancePreferenceAsync(int importSessionId, string preference)
|
|
=> await SetImportContextAsync(importSessionId, preference, null, null, null);
|
|
|
|
public async Task SetImportContextAsync(int importSessionId, string appearancePreference, string? primaryAgeGroup, string? storyEra, string? storyLocation)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
await connection.ExecuteAsync(
|
|
"dbo.StoryMemory_SetImportContext",
|
|
new
|
|
{
|
|
ImportSessionID = importSessionId,
|
|
AppearancePreference = appearancePreference,
|
|
PrimaryAgeGroup = TrimContext(primaryAgeGroup, 120),
|
|
StoryEra = TrimContext(storyEra, 120),
|
|
StoryLocation = TrimContext(storyLocation, 160)
|
|
},commandType:System.Data.CommandType.StoredProcedure);
|
|
}
|
|
|
|
private static string? TrimContext(string? value, int maxLength)
|
|
{
|
|
var clean = value?.Trim();
|
|
return string.IsNullOrWhiteSpace(clean) ? null : clean[..Math.Min(clean.Length, maxLength)];
|
|
}
|
|
|
|
public async Task<StoryMemoryDiagnostics> GetDiagnosticsAsync(int importSessionId)
|
|
{
|
|
using var connection = connectionFactory.CreateConnection();
|
|
return await connection.QuerySingleAsync<StoryMemoryDiagnostics>(
|
|
"dbo.StoryMemory_GetDiagnostics",
|
|
new { ImportSessionID = importSessionId }, commandType: System.Data.CommandType.StoredProcedure);
|
|
}
|
|
}
|