PlotDirector/PlotLine/Data/MapStoryRepository.cs

34 lines
1.8 KiB
C#

using System.Data;
using Dapper;
using PlotLine.Models;
namespace PlotLine.Data;
public interface IMapStoryRepository
{
Task<MapStoryData?> 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<MapStoryData?> 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<bool>();
if (!exists) return null;
return new MapStoryData {
Scenes = (await rows.ReadAsync<MapStoryScene>()).AsList(), Characters = (await rows.ReadAsync<Character>()).AsList(),
Appearances = (await rows.ReadAsync<SceneCharacter>()).AsList(), Locations = (await rows.ReadAsync<LocationItem>()).AsList(),
Maps = (await rows.ReadAsync<StoryMap>()).AsList(), Features = (await rows.ReadAsync<MapFeature>()).AsList(),
Relationships = (await rows.ReadAsync<MapRelationship>()).AsList(), FloorPlans = (await rows.ReadAsync<FloorPlan>()).AsList(),
JourneyNotes = (await rows.ReadAsync<MapJourneyNote>()).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);
}
}