using System.Data; using System.Security.Cryptography; using System.Text.Json; using Dapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using PlotLine.Data; using PlotLine.Models; using PlotLine.Services; using PlotLine.Services.Context; namespace PlotLine.Controllers; [Authorize] public sealed class LocationWorkspaceController(ISqlConnectionFactory connections, ICurrentUserService user, IProjectRepository projects, ContextWorkspaceNotifications notifications, IDataProtectionProvider protection) : Controller { public sealed record Alias(int LocationID, string Value); public sealed record Link(int LocationID, int ID, string Name, string Kind); public sealed record Usage(int LocationID, int SceneCount); public sealed record Option(int ID, string Name); public sealed record Change(int Id, string Value, string Version); public sealed record Assignment(int ProjectId, string Attribute, Change[] Changes); public sealed record Receipt(int User, int ProjectId, string Attribute, Change[] Changes, DateTime Expires); public sealed record UndoRequest(string Token); public sealed record CreateRequest(int ProjectId, string Name, int? ParentId); private IDataProtector Protector => protection.CreateProtector("LocationWorkspace.Undo.v1"); [HttpGet] public async Task Directory(int projectId) { if (user.UserId is not int author) return Unauthorized(); var project = await projects.GetForUserAsync(projectId, author); if (project is null) return NotFound(); using var db = connections.CreateConnection(); using var result = await db.QueryMultipleAsync("dbo.Location_WorkspaceRead", new { ProjectID = projectId, UserID = author }, commandType: CommandType.StoredProcedure); var records = (await result.ReadAsync()).ToArray(); var aliases = (await result.ReadAsync()).ToLookup(a => a.LocationID, a => a.Alias); var maps = (await result.ReadAsync()).ToLookup(a => a.LocationID); var floors = (await result.ReadAsync()).ToLookup(a => a.LocationID); var usage = (await result.ReadAsync()).ToDictionary(a => a.LocationID, a => a.SceneCount); var legacyTypes = (await result.ReadAsync