using System.Data; using Dapper; using PlotLine.Models; namespace PlotLine.Data; public interface IGeographicQueue { Task ClaimAsync(bool enabled); Task> ChainAsync(int locationId); Task CacheAsync(string requestKey); Task ReserveAsync(string provider,TimeSpan interval,string requestKey,Guid lease); Task ReleaseAsync(Guid lease,int cooldownSeconds=0); Task CompleteAsync(GeographicJob job,string status,string jobState,string? key=null,string? candidates=null,string? match=null,string? message=null,bool failure=false,int delay=0,bool relaxContext=false); } public sealed class GeographyRepository(ISqlConnectionFactory connections) : IGeographicQueue { public async Task> GetAsync(int projectId, int userId, int? locationId = null) { using var db = connections.CreateConnection(); return (await db.QueryAsync("dbo.Geography_Get", new { ProjectID=projectId, UserID=userId, LocationID=locationId }, commandType:CommandType.StoredProcedure)).ToList(); } public async Task ClaimAsync(bool enabled) { using var db=connections.CreateConnection(); return await db.QuerySingleOrDefaultAsync("dbo.Geography_Claim",new { Enabled=enabled },commandType:CommandType.StoredProcedure); } public async Task> ChainAsync(int locationId) { using var db=connections.CreateConnection(); return (await db.QueryAsync("dbo.Geography_Chain",new {LocationID=locationId},commandType:CommandType.StoredProcedure)).ToList(); } public async Task CacheAsync(string requestKey) { using var db=connections.CreateConnection(); return await db.QuerySingleOrDefaultAsync("dbo.Geography_Cache",new {RequestKey=requestKey},commandType:CommandType.StoredProcedure); } public async Task ReserveAsync(string provider,TimeSpan interval,string requestKey,Guid lease) { using var db=connections.CreateConnection(); return await db.QuerySingleAsync("dbo.Geography_Reserve",new {Provider=provider,RequestKey=requestKey,Lease=lease,IntervalMs=(int)Math.Clamp(interval.TotalMilliseconds,0,86400000)},commandType:CommandType.StoredProcedure); } public async Task CompleteAsync(GeographicJob job,string status,string jobState,string? key=null,string? candidates=null,string? match=null,string? message=null,bool failure=false,int delay=0,bool relaxContext=false) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_Complete",new {job.LocationID,job.Generation,job.Lease,Status=status,JobState=jobState,RequestKey=key,CandidatesJson=candidates,MatchJson=match,Message=message,Failure=failure,DelaySeconds=delay,RelaxContext=relaxContext},commandType:CommandType.StoredProcedure); } public async Task ChooseAsync(int projectId,int userId,int locationId,long generation,int candidateIndex) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_Choose",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,CandidateIndex=candidateIndex},commandType:CommandType.StoredProcedure); } public async Task RetryAsync(int projectId,int userId,int locationId,long generation,bool replaceConfirmed=false) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_Retry",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,ReplaceConfirmed=replaceConfirmed},commandType:CommandType.StoredProcedure); } public async Task ExcludeAsync(int projectId,int userId,int locationId,long generation,bool isExcluded) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_Exclude",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation,IsExcluded=isExcluded},commandType:CommandType.StoredProcedure); } public async Task SubmitAsync(int projectId,int userId,int locationId,long generation) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_SubmitHierarchy",new {ProjectID=projectId,UserID=userId,LocationID=locationId,Generation=generation},commandType:CommandType.StoredProcedure); } public async Task ReleaseAsync(Guid lease,int cooldownSeconds=0) { using var db=connections.CreateConnection(); await db.ExecuteAsync("dbo.Geography_ProviderRelease",new {Lease=lease,CooldownSeconds=cooldownSeconds},commandType:CommandType.StoredProcedure); } public async Task SubmitLocationsAsync(int projectId,int userId,IEnumerable requests) { using var db=connections.CreateConnection(); return await db.QuerySingleAsync("dbo.Geography_SubmitLocations",new {ProjectID=projectId,UserID=userId,Locations=System.Text.Json.JsonSerializer.Serialize(requests)},commandType:CommandType.StoredProcedure); } }