using System.Data; using System.Text.Json; using Dapper; using PlotLine.Models; namespace PlotLine.Data; public interface IParagraphManuscriptRepository { Task Position(int user,int book,Guid manuscript,Guid session,long revision,int position); Task Get(int user,int book); Task SetMode(int user,int book,long expected,string mode); Task Save(int user,ParagraphManuscript state,long expected,Guid request); Task SaveChapter(int user,ParagraphManuscript state,long expected,Guid request,int chapter)=>Save(user,state,expected,request); Task Capture(int user,int book); Task Status(int user,int book); Task> Events(int user,int book); } public sealed class ParagraphManuscriptRepository(ISqlConnectionFactory factory):IParagraphManuscriptRepository { public async Task Position(int user,int book,Guid manuscript,Guid session,long revision,int position){using var db=factory.CreateConnection();return await db.QuerySingleOrDefaultAsync("dbo.ParagraphManuscript",new{Action="position",UserID=user,BookID=book,ManuscriptID=manuscript,SessionID=session,WordRevision=revision,Position=position},commandType:CommandType.StoredProcedure);} public async Task Get(int user,int book){using var db=factory.CreateConnection();var json=await db.QuerySingleOrDefaultAsync("dbo.ParagraphManuscript",new{Action="get",UserID=user,BookID=book},commandType:CommandType.StoredProcedure);return json==null?null:JsonSerializer.Deserialize(json);} public async Task SetMode(int user,int book,long expected,string mode){using var db=factory.CreateConnection();var json=await db.QuerySingleAsync("dbo.ParagraphManuscript",new{Action="mode",UserID=user,BookID=book,ExpectedRevision=expected,Mode=mode},commandType:CommandType.StoredProcedure);return JsonSerializer.Deserialize(json)!;} public async Task Save(int user,ParagraphManuscript state,long expected,Guid request){using var db=factory.CreateConnection();var json=await db.QuerySingleAsync("dbo.ParagraphManuscript",new{Action="save",UserID=user,BookID=state.BookId,ExpectedRevision=expected,RequestID=request,Payload=JsonSerializer.Serialize(state)},commandType:CommandType.StoredProcedure);return JsonSerializer.Deserialize(json)!;} public async Task SaveChapter(int user,ParagraphManuscript state,long expected,Guid request,int chapter){using var db=factory.CreateConnection();var json=await db.QuerySingleAsync("dbo.ParagraphManuscript",new{Action="save",UserID=user,BookID=state.BookId,ExpectedRevision=expected,RequestID=request,ChangedChapterID=chapter,Payload=JsonSerializer.Serialize(state)},commandType:CommandType.StoredProcedure);return JsonSerializer.Deserialize(json)!;} public async Task Capture(int user,int book){using var db=factory.CreateConnection();var json=await db.QuerySingleOrDefaultAsync("dbo.ParagraphManuscript",new{Action="capture",UserID=user,BookID=book},commandType:CommandType.StoredProcedure);return json==null?null:JsonSerializer.Deserialize(json);} public async Task Status(int user,int book){using var db=factory.CreateConnection();return await db.QuerySingleOrDefaultAsync("dbo.ParagraphManuscript",new{Action="status",UserID=user,BookID=book},commandType:CommandType.StoredProcedure);} public async Task> Events(int user,int book){using var db=factory.CreateConnection();return(await db.QueryAsync("dbo.ParagraphManuscript",new{Action="events",UserID=user,BookID=book},commandType:CommandType.StoredProcedure)).ToList();} }