RT state managment and rectangle support

This commit is contained in:
2026-02-16 16:14:25 +01:00
parent 0f0418dee3
commit d9caeb2209
21 changed files with 459 additions and 5 deletions

View File

@@ -0,0 +1,52 @@
using AipsCore.Application.Abstract;
using AipsCore.Application.Models.Whiteboard.Query.GetWhiteboardInfoRT;
using AipsCore.Domain.Models.Shape.Enums;
using AipsRT.Model.Whiteboard.Shapes.Map;
namespace AipsRT.Model.Whiteboard;
public class GetWhiteboardService
{
private readonly IDispatcher _dispatcher;
public GetWhiteboardService(IDispatcher dispatcher)
{
_dispatcher = dispatcher;
}
public async Task<Whiteboard> GetWhiteboard(Guid whiteboardId)
{
var query = new GetWhiteboardInfoRTQuery(whiteboardId);
return Map(await _dispatcher.Execute(query));
}
private static Whiteboard Map(AipsCore.Infrastructure.Persistence.Whiteboard.Whiteboard entity)
{
var whiteboard = new Whiteboard()
{
WhiteboardId = entity.Id,
OwnerId = entity.OwnerId,
};
foreach (var shape in entity.Shapes)
{
switch (shape.Type)
{
case ShapeType.Rectangle:
whiteboard.AddRectangle(shape.ToRectangle());
break;
case ShapeType.Arrow:
whiteboard.AddArrow(shape.ToArrow());
break;
case ShapeType.Line:
whiteboard.AddLine(shape.ToLine());
break;
case ShapeType.Text:
whiteboard.AddTextShape(shape.ToTextShape());
break;
}
}
return whiteboard;
}
}

View File

@@ -0,0 +1,10 @@
using AipsRT.Model.Whiteboard.Structs;
namespace AipsRT.Model.Whiteboard.Shapes;
public class Arrow : Shape
{
public Position EndPosition { get; set; }
public int Thickness { get; set; }
}

View File

@@ -0,0 +1,10 @@
using AipsRT.Model.Whiteboard.Structs;
namespace AipsRT.Model.Whiteboard.Shapes;
public class Line : Shape
{
public Position EndPosition { get; set; }
public int Thickness { get; set; }
}

View File

@@ -0,0 +1,57 @@
using AipsRT.Model.Whiteboard.Structs;
namespace AipsRT.Model.Whiteboard.Shapes.Map;
public static class ShapeMappingExtensions
{
extension(AipsCore.Infrastructure.Persistence.Shape.Shape shape)
{
public Rectangle ToRectangle()
{
return new Rectangle()
{
Id = shape.Id,
Position = new Position(shape.PositionX, shape.PositionY),
Color = shape.Color,
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
BorderThickness = shape.Thickness!.Value,
};
}
public Arrow ToArrow()
{
return new Arrow()
{
Id = shape.Id,
Position = new Position(shape.PositionX, shape.PositionY),
Color = shape.Color,
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
Thickness = shape.Thickness!.Value,
};
}
public Line ToLine()
{
return new Line()
{
Id = shape.Id,
Position = new Position(shape.PositionX, shape.PositionY),
Color = shape.Color,
EndPosition = new Position(shape.EndPositionX!.Value, shape.EndPositionY!.Value),
Thickness = shape.Thickness!.Value,
};
}
public TextShape ToTextShape()
{
return new TextShape()
{
Id = shape.Id,
Position = new Position(shape.PositionX, shape.PositionY),
Color = shape.Color,
TextValue = shape.TextValue!,
TextSize = shape.TextSize!.Value
};
}
}
}

View File

@@ -0,0 +1,10 @@
using AipsRT.Model.Whiteboard.Structs;
namespace AipsRT.Model.Whiteboard.Shapes;
public class Rectangle : Shape
{
public Position EndPosition { get; set; }
public int BorderThickness { get; set; }
}

View File

@@ -0,0 +1,14 @@
using AipsRT.Model.Whiteboard.Structs;
namespace AipsRT.Model.Whiteboard.Shapes;
public abstract class Shape
{
public Guid Id { get; set; }
public Guid OwnerId { get; set; }
public Position Position { get; set; }
public string Color { get; set; }
}

View File

@@ -0,0 +1,10 @@
using AipsRT.Model.Whiteboard.Shapes;
namespace AipsRT.Model.Whiteboard.Shapes;
public class TextShape : Shape
{
public string TextValue { get; set; }
public int TextSize { get; set; }
}

View File

@@ -0,0 +1,13 @@
namespace AipsRT.Model.Whiteboard.Structs;
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public Position(int x, int y)
{
X = x;
Y = y;
}
}

View File

@@ -0,0 +1,41 @@
using AipsRT.Model.Whiteboard.Shapes;
namespace AipsRT.Model.Whiteboard;
public class Whiteboard
{
public Guid WhiteboardId { get; set; }
public Guid OwnerId { get; set; }
public List<Shape> Shapes { get; } = [];
public List<Rectangle> Rectangles { get; } = [];
public List<Arrow> Arrows { get; } = [];
public List<Line> Lines { get; } = [];
public List<TextShape> TextShapes { get; } = [];
public void AddRectangle(Rectangle shape)
{
Shapes.Add(shape);
Rectangles.Add(shape);
}
public void AddArrow(Arrow shape)
{
Shapes.Add(shape);
Arrows.Add(shape);
}
public void AddLine(Line shape)
{
Shapes.Add(shape);
Lines.Add(shape);
}
public void AddTextShape(TextShape shape)
{
Shapes.Add(shape);
TextShapes.Add(shape);
}
}

View File

@@ -0,0 +1,59 @@
using System.Collections.Concurrent;
using AipsCore.Application.Abstract;
namespace AipsRT.Model.Whiteboard;
public class WhiteboardManager
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ConcurrentDictionary<Guid, Whiteboard> _whiteboards = new();
private readonly ConcurrentDictionary<Guid, Guid> _userInWhiteboards = new();
public WhiteboardManager(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory;
}
public async Task AddWhiteboard(Guid whiteboardId)
{
var getWhiteboardService = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<GetWhiteboardService>();
var whiteboard = await getWhiteboardService.GetWhiteboard(whiteboardId);
_whiteboards[whiteboardId] = whiteboard;
}
public bool WhiteboardExists(Guid whiteboardId)
{
return _whiteboards.ContainsKey(whiteboardId);
}
public void RemoveWhiteboard(Guid whiteboardId)
{
_whiteboards.TryRemove(whiteboardId, out _);
}
public Whiteboard? GetWhiteboard(Guid whiteboardId)
{
return _whiteboards.GetValueOrDefault(whiteboardId);
}
public void AddUserToWhiteboard(Guid userId, Guid whiteboardId)
{
_userInWhiteboards[userId] = whiteboardId;
}
public Guid GetUserWhiteboard(Guid userId)
{
return _userInWhiteboards[userId];
}
public void RemoveUserFromWhiteboard(Guid userId, Guid whiteboardId)
{
_userInWhiteboards.TryRemove(whiteboardId, out _);
}
public Whiteboard? GetWhiteboardForUser(Guid userId)
{
return GetWhiteboard(GetUserWhiteboard(userId));
}
}