72 lines
4.7 KiB
C#
72 lines
4.7 KiB
C#
using System.Data;
|
|
using System.Text.Json;
|
|
using Dapper;
|
|
using PlotLine.Models;
|
|
namespace PlotLine.Data;
|
|
|
|
public sealed class MapRepository(ISqlConnectionFactory connections)
|
|
{
|
|
public async Task<IReadOnlyList<StoryMap>> ListAsync(int projectId, int userId)
|
|
{
|
|
using var db = connections.CreateConnection();
|
|
return (await db.QueryAsync<StoryMap>("dbo.StoryMap_List", new { ProjectID = projectId, UserID = userId }, commandType: CommandType.StoredProcedure)).ToList();
|
|
}
|
|
public async Task<MapDocument?> GetAsync(int projectId, int mapId, int userId)
|
|
{
|
|
using var db = connections.CreateConnection();
|
|
using var rows = await db.QueryMultipleAsync("dbo.StoryMap_Get", new { ProjectID = projectId, MapID = mapId, UserID = userId }, commandType: CommandType.StoredProcedure);
|
|
var map = await rows.ReadSingleOrDefaultAsync<StoryMap>();
|
|
var features = (await rows.ReadAsync<MapFeature>()).ToList();
|
|
var relationships = (await rows.ReadAsync<MapRelationship>()).ToList();
|
|
return map is null ? null : new() { Map = map, Features = features, Relationships = relationships };
|
|
}
|
|
public async Task<int> CreateAsync(StoryMap map, int userId)
|
|
{
|
|
using var db = connections.CreateConnection();
|
|
return await db.QuerySingleAsync<int>("dbo.StoryMap_Create", new { map.ProjectID, UserID = userId, map.Name, map.Kind, map.ParentMapID, map.ParentFeatureID }, commandType: CommandType.StoredProcedure);
|
|
}
|
|
public async Task SaveAsync(MapDocument document, int userId)
|
|
{
|
|
document.Map.AppearanceJson=JsonSerializer.Serialize(MapAppearanceJson.Read<MapCanvasAppearance>(document.Map.AppearanceJson),new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
|
document.Map.ReferenceJson=JsonSerializer.Serialize(MapAppearanceJson.Read<MapReferencePlacement>(document.Map.ReferenceJson),new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
|
using var db = connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.StoryMap_Save", new { document.Map.ProjectID, document.Map.MapID, UserID = userId, document.Map.Revision, document.Map.Name, document.Map.Kind, document.Map.AppearanceJson, document.Map.ReferenceJson,
|
|
Features = JsonSerializer.Serialize(document.Features), Relationships = JsonSerializer.Serialize(document.Relationships) }, commandType: CommandType.StoredProcedure);
|
|
}
|
|
public async Task<IReadOnlyList<MapReferenceImage>> ListImagesAsync(int projectId,int userId)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return (await db.QueryAsync<MapReferenceImage>("dbo.MapReferenceImage_List",new {ProjectID=projectId,UserID=userId},commandType:CommandType.StoredProcedure)).ToList();
|
|
}
|
|
public async Task<MapReferenceImage?> GetImageAsync(int projectId,int userId,Guid imageId)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleOrDefaultAsync<MapReferenceImage>("dbo.MapReferenceImage_Get",new {ProjectID=projectId,UserID=userId,ImageID=imageId},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task<MapReferenceImage?> FindImageAsync(int projectId,int userId,byte[] hash)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleOrDefaultAsync<MapReferenceImage>("dbo.MapReferenceImage_Find",new {ProjectID=projectId,UserID=userId,ContentHash=hash},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task<int> ImageStorageOwnerAsync(int projectId,int userId)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleAsync<int>("dbo.MapReferenceImage_Owner",new {ProjectID=projectId,UserID=userId},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task<MapReferenceImage> AddImageAsync(int projectId,int userId,MapReferenceImage image)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
return await db.QuerySingleAsync<MapReferenceImage>("dbo.MapReferenceImage_Create",new {ProjectID=projectId,UserID=userId,image.ImageID,image.OriginalFileName,image.Width,image.Height,image.ImageData},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task DeleteImageAsync(int projectId,int userId,Guid imageId)
|
|
{
|
|
using var db=connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.MapReferenceImage_DeleteUnused",new {ProjectID=projectId,UserID=userId,ImageID=imageId},commandType:CommandType.StoredProcedure);
|
|
}
|
|
public async Task DeleteAsync(int projectId, int mapId, int revision, int userId)
|
|
{
|
|
using var db = connections.CreateConnection();
|
|
await db.ExecuteAsync("dbo.StoryMap_Delete", new { ProjectID = projectId, MapID = mapId, Revision = revision, UserID = userId }, commandType: CommandType.StoredProcedure);
|
|
}
|
|
}
|