using System.Data; using Dapper; using PlotLine.Models; namespace PlotLine.Data; public interface IOnboardingRepository { Task GetAsync(int userId); Task GetOrCreateAsync(int userId); Task SetCurrentStepAsync(int userId, string currentStep); Task SaveWritingJourneyAsync(int userId, string writingJourney); Task SaveWritingSoftwareAsync(int userId, string writingSoftware); Task ClearWritingSoftwareAsync(int userId); Task SaveProjectAsync(int userId, int projectId); Task SaveBookAsync(int userId, int bookId); Task MarkCompletedAsync(int userId); Task CompleteCoreImportAsync(int userId, int onboardingId, int bookId, Guid previewId); Task ShouldShowWordCompanionHeaderAsync(int userId); } public sealed class OnboardingRepository(ISqlConnectionFactory connectionFactory, IHttpContextAccessor? httpContextAccessor = null) : IOnboardingRepository { private readonly System.Collections.Concurrent.ConcurrentDictionary>> reads = new(); public Task GetAsync(int userId) { // Only reuse within this scoped repository and one GET request. Never retain state across requests/jobs. if (httpContextAccessor?.HttpContext is not { } context || !HttpMethods.IsGet(context.Request.Method) || reads.Count >= 8) return ReadAsync(userId); return reads.GetOrAdd(userId, id => new(() => ReadAsync(id))).Value; } private async Task ReadAsync(int userId) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleOrDefaultAsync( "dbo.UserOnboarding_Get", new { UserID = userId }, commandType: CommandType.StoredProcedure); } public async Task GetOrCreateAsync(int userId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_GetOrCreate", new { UserID = userId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task SetCurrentStepAsync(int userId, string currentStep) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_SetCurrentStep", new { UserID = userId, CurrentStep = currentStep }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task SaveWritingJourneyAsync(int userId, string writingJourney) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_SaveWritingJourney", new { UserID = userId, WritingJourney = writingJourney }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task SaveWritingSoftwareAsync(int userId, string writingSoftware) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_SaveWritingSoftware", new { UserID = userId, WritingSoftware = writingSoftware }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task ClearWritingSoftwareAsync(int userId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_ClearWritingSoftware", new { UserID = userId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task SaveProjectAsync(int userId, int projectId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_SaveProject", new { UserID = userId, ProjectID = projectId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task SaveBookAsync(int userId, int bookId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_SaveBook", new { UserID = userId, BookID = bookId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task MarkCompletedAsync(int userId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_MarkCompleted", new { UserID = userId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task CompleteCoreImportAsync(int userId, int onboardingId, int bookId, Guid previewId) { reads.TryRemove(userId, out _); using var connection = connectionFactory.CreateConnection(); var result = await connection.QuerySingleAsync( "dbo.UserOnboarding_CompleteCoreImport", new { UserID = userId, OnboardingID = onboardingId, BookID = bookId, PreviewID = previewId }, commandType: CommandType.StoredProcedure); reads.TryRemove(userId, out _); return result; } public async Task ShouldShowWordCompanionHeaderAsync(int userId) { using var connection = connectionFactory.CreateConnection(); return await connection.QuerySingleAsync( "dbo.UserOnboarding_ShouldShowWordCompanionHeader", new { UserID = userId }, commandType: CommandType.StoredProcedure); } }