PlotDirector/PlotLine/Data/LocationDetailsRepository.cs

58 lines
2.8 KiB
C#

using System.Data;
using System.Text.Json;
using Dapper;
using PlotLine.ViewModels;
namespace PlotLine.Data;
public sealed class LocationDetailsRepository(ISqlConnectionFactory connections)
{
public async Task<IReadOnlyList<LocationUsageEntry>> ReadUsageAsync(int projectId, int locationId)
{
using var db = connections.CreateConnection();
return (await db.QueryAsync<LocationUsageEntry>("dbo.Location_DetailUsage",
new { ProjectID = projectId, LocationID = locationId }, commandType: CommandType.StoredProcedure)).AsList();
}
public async Task<LocationSpatialPreview> ReadSpatialAsync(int projectId, IReadOnlyList<int> 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<LocationMapLink>();
var floor = await rows.ReadSingleOrDefaultAsync<LocationFloorLink>();
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<FloorPlanEditViewModel>();
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<FloorPlanFloorEditViewModel>()).AsList(),
Blocks = (await plan.ReadAsync<FloorPlanBlockEditViewModel>()).AsList(),
Transitions = (await plan.ReadAsync<FloorPlanTransitionEditViewModel>()).AsList()
});
}
public async Task<int> 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<int>("dbo.Location_DetailRelationshipWrite", parameters,
commandType: CommandType.StoredProcedure);
}
}