using System.Data; using System.Text.Json; using Dapper; using PlotLine.ViewModels; namespace PlotLine.Data; public sealed class LocationDetailsRepository(ISqlConnectionFactory connections) { public async Task> ReadUsageAsync(int projectId, int locationId) { using var db = connections.CreateConnection(); return (await db.QueryAsync("dbo.Location_DetailUsage", new { ProjectID = projectId, LocationID = locationId }, commandType: CommandType.StoredProcedure)).AsList(); } public async Task ReadSpatialAsync(int projectId, IReadOnlyList ancestors) { using var db = connections.CreateConnection(); using var rows = await db.QueryMultipleAsync("dbo.Location_DetailSpatialLinks", new { ProjectID = projectId, AncestorIDs = JsonSerializer.Serialize(ancestors) }, commandType: CommandType.StoredProcedure); var map = await rows.ReadSingleOrDefaultAsync(); var floor = await rows.ReadSingleOrDefaultAsync(); rows.Dispose(); if (floor is null) return new(map, null, null); using var plan = await db.QueryMultipleAsync("dbo.FloorPlan_GetEditor", new { floor.FloorPlanID }, commandType: CommandType.StoredProcedure); var header = await plan.ReadSingleOrDefaultAsync(); if (header?.ProjectID != projectId) return new(map, null, null); return new(map, floor, new FloorPlanEditorViewModel { Project = new() { ProjectID = projectId }, FloorPlan = header, Floors = (await plan.ReadAsync()).AsList(), Blocks = (await plan.ReadAsync()).AsList(), Transitions = (await plan.ReadAsync()).AsList() }); } public async Task WriteRelationshipAsync(int userId, LocationRelationshipEditViewModel model, DateTime? expectedUpdatedDate, bool archive) { using var db = connections.CreateConnection(); var parameters = new DynamicParameters(new { UserID = userId, model.ProjectID, model.LocationRelationshipID, model.FromLocationID, model.ToLocationID, model.LocationRelationshipTypeID, model.IsBidirectional, model.Strength, model.CanHearNormalSpeech, model.CanHearLoudNoise, model.CanSeeMovement, model.CanSeeClearly, model.CanTravelDirectly, model.Notes, Archive = archive }); parameters.Add("ExpectedUpdatedDate", expectedUpdatedDate, DbType.DateTime2, scale: 7); return await db.QuerySingleAsync("dbo.Location_DetailRelationshipWrite", parameters, commandType: CommandType.StoredProcedure); } }