using System.Data; using Dapper; using PlotLine.Models; namespace PlotLine.Data; public interface IMapStoryRepository { Task GetAsync(int projectId, int mapId, int userId); Task SaveJourneyAsync(int projectId, int userId, MapJourneyNote note); } public sealed class MapStoryRepository(ISqlConnectionFactory connections) : IMapStoryRepository { public async Task GetAsync(int projectId, int mapId, int userId) { using var db = connections.CreateConnection(); using var rows = await db.QueryMultipleAsync("dbo.StoryMap_Story", new { ProjectID = projectId, MapID = mapId, UserID = userId }, commandType: CommandType.StoredProcedure); var exists = await rows.ReadSingleAsync(); if (!exists) return null; return new MapStoryData { Scenes = (await rows.ReadAsync()).AsList(), Characters = (await rows.ReadAsync()).AsList(), Appearances = (await rows.ReadAsync()).AsList(), Locations = (await rows.ReadAsync()).AsList(), Maps = (await rows.ReadAsync()).AsList(), Features = (await rows.ReadAsync()).AsList(), Relationships = (await rows.ReadAsync()).AsList(), FloorPlans = (await rows.ReadAsync()).AsList(), JourneyNotes = (await rows.ReadAsync()).AsList() }; } public async Task SaveJourneyAsync(int projectId, int userId, MapJourneyNote note) { using var db = connections.CreateConnection(); await db.ExecuteAsync("dbo.StoryMap_JourneySave", new { ProjectID = projectId, UserID = userId, note.MapID, note.CharacterID, note.FromSceneID, note.ToSceneID, note.RouteFeatureID, note.TravelMethod, note.TypicalMinutes }, commandType: CommandType.StoredProcedure); } }