11 lines
2.6 KiB
C#
11 lines
2.6 KiB
C#
using System.Text.Json;
|
|
using PlotLine.Models;
|
|
using PlotLine.Services.Writer;
|
|
internal sealed class WriterAuthoringFixture:IWriterAuthoringService
|
|
{
|
|
private readonly object gate=new();private readonly Dictionary<string,AuthoringSection> rows=new();private int next=100;
|
|
public WriterAuthoringFixture(){foreach(var kind in new[]{"Chapter","Goals","Scene","Workflow","AddNote","AddChecklist"}){var id=kind.StartsWith("Add")?0:kind is "Chapter" or "Goals"?21:31;rows[$"{kind}-{id}"]=new(){Kind=kind,EntityID=id,Data=kind=="Scene"?"{\"ScenePurposeNotes\":\"A recorded scene intention\",\"SceneOutcomeNotes\":\"A recorded outcome\"}":kind=="Goals"?"{\"GoalSummary\":\"A recorded chapter intention\"}":"{}",Version=Guid.NewGuid().ToString("N")};}}
|
|
public Task<WriterAuthoringPage> Read(int actor,int book,int chapter,int? scene){lock(gate){if(book!=12||chapter!=21||scene is not(null or 31))throw new KeyNotFoundException();return Task.FromResult(new WriterAuthoringPage{BookID=12,ChapterID=21,SceneID=scene,Chapter=new(){BookID=12,ChapterID=21,ChapterTitle="The crossing",RevisionStatusName="Outlined"},Scene=scene.HasValue?new(){BookID=12,ChapterID=21,SceneID=31,SceneTitle="Arrival",RevisionStatusName="Outlined",PrimaryLocationName="Recorded harbour"}:null,Scenes=[new(){SceneID=31,ChapterID=21,BookID=12,SceneTitle="Arrival",RevisionStatusName="Outlined"}],Characters=scene.HasValue?[new(){CharacterName="Fixture character"}]:[],Sections=rows.Values.Where(s=>scene.HasValue||s.Kind is "Chapter" or "Goals").Select(s=>new AuthoringSection{BookID=12,ChapterID=21,SceneID=scene,Kind=s.Kind,EntityID=s.EntityID,Data=s.Data,Version=s.Version,Purposes=[("1","Introduce a change")],NoteTypes=[("1","Drafting note")]}).ToArray()});}}
|
|
public Task Save(int actor,int book,int chapter,int? scene,string kind,int entity,string version,IReadOnlyDictionary<string,string?> values){lock(gate){if(book!=12||chapter!=21||scene is not(null or 31))throw new KeyNotFoundException();var error=AuthoringSchema.Validate(kind,values);if(error!=null)throw new ArgumentException(error);var key=$"{kind}-{entity}";if(!rows.TryGetValue(key,out var row))throw new KeyNotFoundException();if(row.Version!=version)throw new AuthoringConflictException("Recorded information changed; your text is retained.");if(kind.StartsWith("Add")){var k=kind=="AddNote"?"Note":"Checklist";var id=next++;rows[$"{k}-{id}"]=new(){Kind=k,EntityID=id,Data=JsonSerializer.Serialize(values),Version=Guid.NewGuid().ToString("N")};row.Version=Guid.NewGuid().ToString("N");}else{row.Data=JsonSerializer.Serialize(values);row.Version=Guid.NewGuid().ToString("N");}return Task.CompletedTask;}}
|
|
}
|