Infrastructure repositories now inherit AbstractRepository and implement concrete mapping logic

This commit is contained in:
Veljko Tosic
2026-02-11 02:25:14 +01:00
parent c23e489db2
commit e7abe1eaed
8 changed files with 208 additions and 200 deletions

View File

@@ -8,19 +8,19 @@ namespace AipsCore.Infrastructure.Persistence.Shape.Mappers;
public static partial class ShapeMappers
{
public static Domain.Models.Shape.Shape EntityToModel(Shape shape)
public static Domain.Models.Shape.Shape MapToDomainEntity(Shape shape)
{
return shape.Type switch
{
ShapeType.Rectangle => EntityToRectangle(shape),
ShapeType.Line => EntityToLine(shape),
ShapeType.Arrow => EntityToArrow(shape),
ShapeType.Text => EntityToTextShape(shape),
ShapeType.Rectangle => PersistenceEntityToRectangle(shape),
ShapeType.Line => PersistenceEntityToLine(shape),
ShapeType.Arrow => PersistenceEntityToArrow(shape),
ShapeType.Text => PersistenceEntityToTextShape(shape),
_ => throw new ArgumentOutOfRangeException()
};
}
public static Rectangle EntityToRectangle(Shape shape)
private static Rectangle PersistenceEntityToRectangle(Shape shape)
{
return Rectangle.Create(
shape.Id.ToString(),
@@ -30,8 +30,8 @@ public static partial class ShapeMappers
shape.EndPositionX!.Value, shape.EndPositionY!.Value,
shape.Thickness!.Value);
}
public static Line EntityToLine(Shape shape)
private static Line PersistenceEntityToLine(Shape shape)
{
return Line.Create(
shape.Id.ToString(),
@@ -42,8 +42,8 @@ public static partial class ShapeMappers
shape.EndPositionX!.Value, shape.EndPositionY!.Value,
shape.Thickness!.Value);
}
public static Arrow EntityToArrow(Shape shape)
private static Arrow PersistenceEntityToArrow(Shape shape)
{
return Arrow.Create(
shape.Id.ToString(),
@@ -54,8 +54,8 @@ public static partial class ShapeMappers
shape.EndPositionX!.Value, shape.EndPositionY!.Value,
shape.Thickness!.Value);
}
public static TextShape EntityToTextShape(Shape shape)
private static TextShape PersistenceEntityToTextShape(Shape shape)
{
return TextShape.Create(
shape.Id.ToString(),

View File

@@ -9,21 +9,21 @@ namespace AipsCore.Infrastructure.Persistence.Shape.Mappers;
public static partial class ShapeMappers
{
public static Shape ModelToEntity(Domain.Models.Shape.Shape model)
public static Shape MapToPersistenceEntity(Domain.Models.Shape.Shape model)
{
return model.ShapeType switch
{
ShapeType.Rectangle => RectangleToEntity((Rectangle)model),
ShapeType.Line => LineToEntity((Line)model),
ShapeType.Arrow => ArrowToEntity((Arrow)model),
ShapeType.Text => TextShapeToEntity((TextShape)model),
ShapeType.Rectangle => RectangleToPersistenceEntity((Rectangle)model),
ShapeType.Line => LineToPersistenceEntity((Line)model),
ShapeType.Arrow => ArrowToPersistenceEntity((Arrow)model),
ShapeType.Text => TextShapeToPersistenceEntity((TextShape)model),
_ => throw new ArgumentOutOfRangeException()
};
}
public static Shape RectangleToEntity(Rectangle rectangle)
private static Shape RectangleToPersistenceEntity(Rectangle rectangle)
{
return new Shape()
return new Shape
{
Id = new Guid(rectangle.Id.Value),
Type = rectangle.ShapeType,
@@ -37,10 +37,10 @@ public static partial class ShapeMappers
Thickness = rectangle.BorderThickness.Value,
};
}
public static Shape LineToEntity(Line line)
private static Shape LineToPersistenceEntity(Line line)
{
return new Shape()
return new Shape
{
Id = new Guid(line.Id.Value),
Type = line.ShapeType,
@@ -54,10 +54,10 @@ public static partial class ShapeMappers
Thickness = line.Thickness.Value,
};
}
public static Shape ArrowToEntity(Arrow arrow)
private static Shape ArrowToPersistenceEntity(Arrow arrow)
{
return new Shape()
return new Shape
{
Id = new Guid(arrow.Id.Value),
Type = arrow.ShapeType,
@@ -71,10 +71,10 @@ public static partial class ShapeMappers
Thickness = arrow.Thickness.Value,
};
}
public static Shape TextShapeToEntity(TextShape textShape)
private static Shape TextShapeToPersistenceEntity(TextShape textShape)
{
return new Shape()
return new Shape
{
Id = new Guid(textShape.Id.Value),
Type = textShape.ShapeType,

View File

@@ -8,7 +8,7 @@ namespace AipsCore.Infrastructure.Persistence.Shape.Mappers;
public static partial class ShapeMappers
{
public static void UpdateEntity(Shape entity, Domain.Models.Shape.Shape model)
public static void UpdatePersistenceEntity(Shape entity, Domain.Models.Shape.Shape model)
{
entity.WhiteboardId = new Guid(model.WhiteboardId.IdValue);
entity.PositionX = model.Position.X;
@@ -32,28 +32,28 @@ public static partial class ShapeMappers
};
}
public static void UpdateEntityFromRectangle(Shape entity, Rectangle rectangle)
private static void UpdateEntityFromRectangle(Shape entity, Rectangle rectangle)
{
entity.EndPositionX = rectangle.EndPosition.X;
entity.EndPositionY = rectangle.EndPosition.Y;
entity.Thickness = rectangle.BorderThickness.Value;
}
public static void UpdateEntityFromLine(Shape entity, Line line)
private static void UpdateEntityFromLine(Shape entity, Line line)
{
entity.EndPositionX = line.EndPosition.X;
entity.EndPositionY = line.EndPosition.Y;
entity.Thickness = line.Thickness.Value;
}
public static void UpdateEntityFromArrow(Shape entity, Arrow arrow)
private static void UpdateEntityFromArrow(Shape entity, Arrow arrow)
{
entity.EndPositionX = arrow.EndPosition.X;
entity.EndPositionY = arrow.EndPosition.Y;
entity.Thickness = arrow.Thickness.Value;
}
public static void UpdateEntityFromTextShape(Shape entity, TextShape textShape)
private static void UpdateEntityFromTextShape(Shape entity, TextShape textShape)
{
entity.TextValue = textShape.TextShapeValue.Text;
entity.TextSize = textShape.TextShapeSize.Size;

View File

@@ -1,42 +1,31 @@
using AipsCore.Domain.Models.Shape.External;
using AipsCore.Domain.Models.Shape.ValueObjects;
using AipsCore.Infrastructure.Persistence.Abstract;
using AipsCore.Infrastructure.Persistence.Db;
using AipsCore.Infrastructure.Persistence.Shape.Mappers;
namespace AipsCore.Infrastructure.Persistence.Shape;
public class ShapeRepository : IShapeRepository
public class ShapeRepository : AbstractRepository<Domain.Models.Shape.Shape, ShapeId, Shape>, IShapeRepository
{
private readonly AipsDbContext _context;
public ShapeRepository(AipsDbContext context)
public ShapeRepository(AipsDbContext context)
: base(context)
{
_context = context;
}
public async Task<Domain.Models.Shape.Shape?> Get(ShapeId id, CancellationToken cancellationToken = default)
{
var entity = await _context.Shapes.FindAsync([new Guid(id.Value)], cancellationToken);
if (entity is null) return null;
return ShapeMappers.EntityToModel(entity);
}
public async Task Add(Domain.Models.Shape.Shape shape, CancellationToken cancellationToken = default)
protected override Domain.Models.Shape.Shape MapToDomainEntity(Shape persistenceEntity)
{
var entity = await _context.Shapes.FindAsync([new Guid(shape.Id.Value)], cancellationToken);
return ShapeMappers.MapToDomainEntity(persistenceEntity);
}
if (entity is not null)
{
ShapeMappers.UpdateEntity(entity, shape);
}
else
{
var newEntity = ShapeMappers.ModelToEntity(shape);
await _context.Shapes.AddAsync(newEntity, cancellationToken);
}
protected override Shape MapToPersistenceEntity(Domain.Models.Shape.Shape domainEntity)
{
return ShapeMappers.MapToPersistenceEntity(domainEntity);
}
protected override void UpdatePersistenceEntity(Shape persistenceEntity, Domain.Models.Shape.Shape domainEntity)
{
ShapeMappers.UpdatePersistenceEntity(persistenceEntity, domainEntity);
}
}