PlotDirector/PlotLine/Data/InvestigationRepository.cs

20 lines
3.1 KiB
C#

using System.Data;
using System.Text.Json;
using Dapper;
using PlotLine.Models.Investigations;
using PlotLine.Services.Investigations;
namespace PlotLine.Data;
public interface IInvestigationRepository
{Task Delete(int user,int project,int book,Guid id);Task<Catalogue?> Catalogue(string? version=null);Task Publish(string json,string actor);Task<IReadOnlyList<Investigation>> List(int user,int book);Task<Investigation?> Read(int user,int book,Guid id);Task Save(Investigation row,int expected,string operation,Evaluation result);Task<string[]> Replay(int user,int book,Guid id);}
public sealed class InvestigationRepository(ISqlConnectionFactory factory):IInvestigationRepository
{
public async Task Delete(int user,int project,int book,Guid id){using var db=factory.CreateConnection();await db.ExecuteAsync("dbo.WriterInvestigationDelete",new{UserID=user,ProjectID=project,BookID=book,ID=id},commandType:CommandType.StoredProcedure);}
public async Task<Catalogue?> Catalogue(string? version=null){using var db=factory.CreateConnection();var json=await db.QuerySingleOrDefaultAsync<string>("dbo.WriterInvestigationCatalogue",new{Action="read",Version=version},commandType:CommandType.StoredProcedure);return json==null?null:CatalogueValidator.Parse(json);}
public async Task Publish(string json,string actor){var c=CatalogueValidator.Parse(json);using var db=factory.CreateConnection();await db.ExecuteAsync("dbo.WriterInvestigationCatalogue",new{Action="publish",c.Version,Json=json,Actor=actor},commandType:CommandType.StoredProcedure);}
public async Task<IReadOnlyList<Investigation>> List(int user,int book){using var db=factory.CreateConnection();return (await db.QueryAsync<string>("dbo.WriterInvestigation",new{Action="list",UserID=user,BookID=book},commandType:CommandType.StoredProcedure)).Select(Parse).ToArray();}
public async Task<Investigation?> Read(int user,int book,Guid id){using var db=factory.CreateConnection();try{var json=await db.QuerySingleOrDefaultAsync<string>("dbo.WriterInvestigation",new{Action="read",UserID=user,BookID=book,ID=id},commandType:CommandType.StoredProcedure);return json==null?null:Parse(json);}catch(Microsoft.Data.SqlClient.SqlException e)when(e.Number==51000){return null;}}
public async Task Save(Investigation row,int expected,string operation,Evaluation result){using var db=factory.CreateConnection();row.Version=expected+1;await db.ExecuteAsync("dbo.WriterInvestigation",new{Action="save",row.UserID,row.ProjectID,row.BookID,row.ID,row.CatalogueVersion,ExpectedVersion=expected,Json=JsonSerializer.Serialize(row,CatalogueValidator.Json),Event=JsonSerializer.Serialize(new{operation,state=row,evaluation=result},CatalogueValidator.Json)},commandType:CommandType.StoredProcedure);}
public async Task<string[]> Replay(int user,int book,Guid id){using var db=factory.CreateConnection();return(await db.QueryAsync<string>("dbo.WriterInvestigation",new{Action="events",UserID=user,BookID=book,ID=id},commandType:CommandType.StoredProcedure)).ToArray();}
static Investigation Parse(string json)=>JsonSerializer.Deserialize<Investigation>(json,CatalogueValidator.Json)!;
}