PlotDirector/PlotLine/Data/StoryElementReferenceRepository.cs

68 lines
3.4 KiB
C#

using System.Data;
using Dapper;
namespace PlotLine.Data;
public enum StoryElementType { Character, Location, Asset }
public sealed record StoryElementReferenceScope(StoryElementType EntityType, int EntityID)
{
public string DetailsController => EntityType switch
{
StoryElementType.Character => "Characters",
StoryElementType.Location => "Locations",
StoryElementType.Asset => "StoryAssets",
_ => 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<StoryElementReference> 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<StoryElementReferenceList?> 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<StoryElementReferenceAccess>();
return access is null ? null : new(scope, access, (await results.ReadAsync<StoryElementReference>()).AsList());
}
public async Task<StoryElementReferenceFile?> FileAsync(int userId, StoryElementReferenceScope scope, int referenceId)
{
using var connection = connections.CreateConnection();
return await connection.QuerySingleOrDefaultAsync<StoryElementReferenceFile>(Procedure,
new { UserID = userId, EntityType = scope.EntityType.ToString(), scope.EntityID, Action = "file", ReferenceID = referenceId },
commandType: CommandType.StoredProcedure);
}
public async Task<StoryElementReferenceChange?> 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<StoryElementReferenceChange>(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);
}
}