PlotDirector/PlotLine/Services/NarrativeFocus.cs

66 lines
3.9 KiB
C#

using System.Data;
using Dapper;
using PlotLine.Data;
namespace PlotLine.Services;
public sealed record NarrativePosition
{
public int ChapterID { get; init; }
public string ChapterTitle { get; init; } = "";
public decimal ChapterNumber { get; init; }
public int? SceneID { get; init; }
public string? SceneTitle { get; init; }
public decimal? SceneNumber { get; init; }
public long? BookSceneIndex { get; init; }
public string Label => $"Ch {ChapterNumber:0.##}: {ChapterTitle}" + (SceneID.HasValue ? $" · Scene {SceneNumber:0.##}: {SceneTitle}" : "");
}
public sealed record NarrativeRecent(int ChapterID, int? SceneID, long Sequence);
public sealed record NarrativeFocusState
{
public int ProjectID { get; init; }
public int BookID { get; init; }
public int? ChapterID { get; init; }
public int? SceneID { get; init; }
public Guid? ParagraphID{get;init;}public Guid? ManuscriptID{get;init;}public Guid? OriginWindowID{get;init;}
public long Version { get; init; }
public IReadOnlyList<NarrativePosition> Positions { get; init; } = [];
public IReadOnlyList<NarrativeRecent> Recent { get; init; } = [];
public NarrativePosition? Position => Positions.FirstOrDefault(p => p.ChapterID == ChapterID && (!SceneID.HasValue || p.SceneID == SceneID));
public string Label => Position is {} p ? SceneID.HasValue ? p.Label : $"Ch {p.ChapterNumber:0.##}: {p.ChapterTitle}" : "Choose chapter or scene";
}
public sealed class NarrativeFocusConflictException() : Exception("Current focus changed in another window. Review it and try again.");
public interface INarrativeFocus
{
Task<NarrativeFocusState?> Open(int user, int? chapter, int? scene, int? expectedProject=null, int? expectedBook=null);
Task<NarrativeFocusState> Read(int user, int project, int book);
Task<NarrativeFocusState> Set(int user, int project, int book, int? chapter, int? scene, long version, int direction = 0);
}
public sealed class NarrativeFocus(ISqlConnectionFactory factory) : INarrativeFocus
{
public async Task<NarrativeFocusState?> Open(int user,int? chapter,int? scene,int? expectedProject=null,int? expectedBook=null)
{
NarrativeFocusState? target;
using(var db=factory.CreateConnection())
target=await db.QuerySingleOrDefaultAsync<NarrativeFocusState>("dbo.NarrativeFocus_Locate",new{UserID=user,ChapterID=chapter,SceneID=scene},commandType:CommandType.StoredProcedure);
if(target is null || expectedProject.HasValue&&target.ProjectID!=expectedProject || expectedBook.HasValue&&target.BookID!=expectedBook)return null;
var current=await Read(user,target.ProjectID,target.BookID);
return await Set(user,target.ProjectID,target.BookID,target.ChapterID,target.SceneID,current.Version);
}
public async Task<NarrativeFocusState> Read(int user, int project, int book)
{
using var db = factory.CreateConnection();
using var rows = await db.QueryMultipleAsync("dbo.NarrativeFocus_Read", new { UserID=user, ProjectID=project, BookID=book }, commandType:CommandType.StoredProcedure);
var state = await rows.ReadSingleAsync<NarrativeFocusState>();
return state with { Positions=(await rows.ReadAsync<NarrativePosition>()).ToArray(), Recent=(await rows.ReadAsync<NarrativeRecent>()).ToArray() };
}
public async Task<NarrativeFocusState> Set(int user,int project,int book,int? chapter,int? scene,long version,int direction=0)
{
using(var db = factory.CreateConnection())
{
try{await db.ExecuteAsync("dbo.NarrativeFocus_Set", new { UserID=user,ProjectID=project,BookID=book,ChapterID=chapter,SceneID=scene,ExpectedVersion=version,Direction=direction },commandType:CommandType.StoredProcedure);}
catch(Microsoft.Data.SqlClient.SqlException e)when(e.Number==51001){throw new NarrativeFocusConflictException();}
}
return await Read(user,project,book);
}
}