using System.Data; using Dapper; namespace PlotLine.Data; public enum StoryElementType { Character, Location, Asset, PlotThread, Chapter, Scene } public sealed record StoryElementReferenceScope(StoryElementType EntityType, int EntityID) { public bool AttachmentsOnly => EntityType is StoryElementType.PlotThread or StoryElementType.Chapter or StoryElementType.Scene; public string DetailsController => EntityType switch { StoryElementType.Character => "Characters", StoryElementType.Location => "Locations", StoryElementType.Asset => "StoryAssets", StoryElementType.PlotThread => "PlotThreads", _ => throw new ArgumentOutOfRangeException(nameof(EntityType)) }; } public sealed record StoryElementReference(int ReferenceID, string Kind, string Note, string? OriginalFileName, byte[] Version, string? Title, bool IsPrimary); public sealed record StoryElementReferenceAccess(int ProjectID, string EntityName, bool CanEdit, string? LegacyImagePath); public sealed record StoryElementReferenceList(StoryElementReferenceScope Scope, StoryElementReferenceAccess Access, IReadOnlyList Items); public sealed record StoryElementReferenceFile(string Kind, string StoragePath, string OriginalFileName); public sealed class StoryElementReferenceChange { public int ProjectID { get; init; } public bool Changed { get; init; } public string? StoragePath { get; init; } public bool QuotaExceeded { get; init; } } public sealed class StoryElementReferenceRepository(ISqlConnectionFactory connections) { private const string Procedure = "dbo.StoryElementReference_Access"; public async Task ListAsync(int userId, StoryElementReferenceScope scope) { using var connection = connections.CreateConnection(); using var results = await connection.QueryMultipleAsync(Procedure, new { UserID = userId, EntityType = scope.EntityType.ToString(), scope.EntityID, Action = "list" }, commandType: CommandType.StoredProcedure); var access = await results.ReadSingleOrDefaultAsync(); return access is null ? null : new(scope, access, (await results.ReadAsync()).AsList()); } public async Task FileAsync(int userId, StoryElementReferenceScope scope, int referenceId) { using var connection = connections.CreateConnection(); return await connection.QuerySingleOrDefaultAsync(Procedure, new { UserID = userId, EntityType = scope.EntityType.ToString(), scope.EntityID, Action = "file", ReferenceID = referenceId }, commandType: CommandType.StoredProcedure); } public async Task ChangeAsync(int userId, StoryElementReferenceScope scope, string action, int? referenceId, byte[]? version, string? kind, string? note, string? storagePath = null, string? originalFileName = null, long? fileSizeBytes = null, string? title = null, long? storageLimitBytes = null) { using var connection = connections.CreateConnection(); return await connection.QuerySingleOrDefaultAsync(Procedure, new { UserID = userId, EntityType = scope.EntityType.ToString(), scope.EntityID, Action = action, ReferenceID = referenceId, Version = version, Kind = kind, Note = note, StoragePath = storagePath, OriginalFileName = originalFileName, FileSizeBytes = fileSizeBytes, Title = title, StorageLimitBytes = storageLimitBytes }, commandType: CommandType.StoredProcedure); } }