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

@@ -0,0 +1,55 @@
using AipsCore.Domain.Abstract;
using AipsCore.Domain.Common.ValueObjects;
using AipsCore.Infrastructure.Persistence.Db;
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Infrastructure.Persistence.Abstract;
public abstract class AbstractRepository<TEntity, TId, TPersistenceEntity> : IAbstractRepository<TEntity, TId>
where TEntity : DomainEntity<TId>
where TId : DomainId
where TPersistenceEntity : class
{
protected readonly AipsDbContext Context;
protected readonly DbSet<TPersistenceEntity> DbSet;
protected AbstractRepository(AipsDbContext context)
{
Context = context;
DbSet = Context.Set<TPersistenceEntity>();
}
public async Task<TEntity?> GetByIdAsync(TId id, CancellationToken cancellationToken = default)
{
var persistenceEntity = await DbSet.FindAsync([new Guid(id.IdValue)], cancellationToken);
return persistenceEntity != null ? MapToDomainEntity(persistenceEntity) : null;
}
public async Task SaveAsync(TEntity entity, CancellationToken cancellationToken = default)
{
var persistenceEntity = await DbSet.FindAsync([new Guid(entity.Id.IdValue)], cancellationToken);
if (persistenceEntity == null)
{
persistenceEntity = MapToPersistenceEntity(entity);
await DbSet.AddAsync(persistenceEntity, cancellationToken);
}
else
{
UpdatePersistenceEntity(persistenceEntity, entity);
DbSet.Update(persistenceEntity);
}
}
public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default)
{
var persistenceEntity = MapToPersistenceEntity(entity);
await DbSet.AddAsync(persistenceEntity, cancellationToken);
}
protected abstract TEntity MapToDomainEntity(TPersistenceEntity persistenceEntity);
protected abstract TPersistenceEntity MapToPersistenceEntity(TEntity domainEntity);
protected abstract void UpdatePersistenceEntity(TPersistenceEntity persistenceEntity, TEntity domainEntity);
}

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(),
@@ -31,7 +31,7 @@ public static partial class ShapeMappers
shape.Thickness!.Value);
}
public static Line EntityToLine(Shape shape)
private static Line PersistenceEntityToLine(Shape shape)
{
return Line.Create(
shape.Id.ToString(),
@@ -43,7 +43,7 @@ public static partial class ShapeMappers
shape.Thickness!.Value);
}
public static Arrow EntityToArrow(Shape shape)
private static Arrow PersistenceEntityToArrow(Shape shape)
{
return Arrow.Create(
shape.Id.ToString(),
@@ -55,7 +55,7 @@ public static partial class ShapeMappers
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,
@@ -38,9 +38,9 @@ public static partial class ShapeMappers
};
}
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,
@@ -55,9 +55,9 @@ public static partial class ShapeMappers
};
}
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,
@@ -72,9 +72,9 @@ public static partial class ShapeMappers
};
}
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)
: base(context)
{
_context = context;
}
public async Task<Domain.Models.Shape.Shape?> Get(ShapeId id, CancellationToken cancellationToken = default)
protected override Domain.Models.Shape.Shape MapToDomainEntity(Shape persistenceEntity)
{
var entity = await _context.Shapes.FindAsync([new Guid(id.Value)], cancellationToken);
if (entity is null) return null;
return ShapeMappers.EntityToModel(entity);
return ShapeMappers.MapToDomainEntity(persistenceEntity);
}
public async Task Add(Domain.Models.Shape.Shape shape, CancellationToken cancellationToken = default)
protected override Shape MapToPersistenceEntity(Domain.Models.Shape.Shape domainEntity)
{
var entity = await _context.Shapes.FindAsync([new Guid(shape.Id.Value)], cancellationToken);
if (entity is not null)
{
ShapeMappers.UpdateEntity(entity, shape);
}
else
{
var newEntity = ShapeMappers.ModelToEntity(shape);
await _context.Shapes.AddAsync(newEntity, cancellationToken);
}
return ShapeMappers.MapToPersistenceEntity(domainEntity);
}
protected override void UpdatePersistenceEntity(Shape persistenceEntity, Domain.Models.Shape.Shape domainEntity)
{
ShapeMappers.UpdatePersistenceEntity(persistenceEntity, domainEntity);
}
}

View File

@@ -1,58 +1,45 @@
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Persistence.Abstract;
using AipsCore.Infrastructure.Persistence.Db;
namespace AipsCore.Infrastructure.Persistence.User;
public class UserRepository : IUserRepository
public class UserRepository : AbstractRepository<Domain.Models.User.User, UserId, User>, IUserRepository
{
private readonly AipsDbContext _context;
public UserRepository(AipsDbContext context)
: base(context)
{
_context = context;
}
public async Task<Domain.Models.User.User?> Get(UserId userId, CancellationToken cancellationToken = default)
protected override Domain.Models.User.User MapToDomainEntity(User persistenceEntity)
{
var userEntity = await _context.Users.FindAsync([new Guid(userId.IdValue), cancellationToken], cancellationToken: cancellationToken);
if (userEntity is null) return null;
return Domain.Models.User.User.Create(
userEntity.Id.ToString(),
userEntity.Email,
userEntity.Username,
userEntity.CreatedAt,
userEntity.DeletedAt);
persistenceEntity.Id.ToString(),
persistenceEntity.Email,
persistenceEntity.Username,
persistenceEntity.CreatedAt,
persistenceEntity.DeletedAt
);
}
public async Task Save(Domain.Models.User.User user, CancellationToken cancellationToken = default)
protected override User MapToPersistenceEntity(Domain.Models.User.User domainEntity)
{
// ReSharper disable once MethodSupportsCancellation
var userEntity = await _context.Users.FindAsync(new Guid(user.Id.IdValue));
if (userEntity is not null)
return new User
{
userEntity.Email = user.Email.EmailValue;
userEntity.Username = user.Username.UsernameValue;
userEntity.CreatedAt = user.CreatedAt.CreatedAtValue;
userEntity.DeletedAt = user.DeletedAt.DeletedAtValue;
Id = new Guid(domainEntity.Id.IdValue),
Email = domainEntity.Email.EmailValue,
Username = domainEntity.Username.UsernameValue,
CreatedAt = domainEntity.CreatedAt.CreatedAtValue,
DeletedAt = domainEntity.DeletedAt.DeletedAtValue
};
}
_context.Users.Update(userEntity);
}
else
{
userEntity = new User()
{
Id = new Guid(user.Id.IdValue),
Email = user.Email.EmailValue,
Username = user.Username.UsernameValue,
CreatedAt = user.CreatedAt.CreatedAtValue,
DeletedAt = user.DeletedAt.DeletedAtValue
};
_context.Users.Add(userEntity);
}
protected override void UpdatePersistenceEntity(User persistenceEntity, Domain.Models.User.User domainEntity)
{
persistenceEntity.Email = domainEntity.Email.EmailValue;
persistenceEntity.Username = domainEntity.Username.UsernameValue;
persistenceEntity.CreatedAt = domainEntity.CreatedAt.CreatedAtValue;
persistenceEntity.DeletedAt = domainEntity.DeletedAt.DeletedAtValue;
}
}

View File

@@ -1,76 +1,64 @@
using AipsCore.Domain.Models.Whiteboard.External;
using AipsCore.Domain.Models.Whiteboard.ValueObjects;
using AipsCore.Infrastructure.Persistence.Abstract;
using AipsCore.Infrastructure.Persistence.Db;
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Infrastructure.Persistence.Whiteboard;
public class WhiteboardRepository : IWhiteboardRepository
public class WhiteboardRepository
: AbstractRepository<Domain.Models.Whiteboard.Whiteboard, WhiteboardId, Whiteboard>, IWhiteboardRepository
{
private readonly AipsDbContext _context;
public WhiteboardRepository(AipsDbContext context)
: base(context)
{
_context = context;
}
public async Task<Domain.Models.Whiteboard.Whiteboard?> Get(WhiteboardId whiteboardId, CancellationToken cancellationToken = default)
protected override Domain.Models.Whiteboard.Whiteboard MapToDomainEntity(Whiteboard persistenceEntity)
{
var whiteboardEntity = await _context.Whiteboards.FindAsync([new Guid(whiteboardId.IdValue), cancellationToken], cancellationToken: cancellationToken);
if (whiteboardEntity is null) return null;
return Domain.Models.Whiteboard.Whiteboard.Create(
whiteboardEntity.Id.ToString(),
whiteboardEntity.OwnerId.ToString(),
whiteboardEntity.Code,
whiteboardEntity.Title,
whiteboardEntity.CreatedAt,
whiteboardEntity.DeletedAt,
whiteboardEntity.MaxParticipants,
whiteboardEntity.JoinPolicy,
whiteboardEntity.State);
persistenceEntity.Id.ToString(),
persistenceEntity.OwnerId.ToString(),
persistenceEntity.Code,
persistenceEntity.Title,
persistenceEntity.CreatedAt,
persistenceEntity.DeletedAt,
persistenceEntity.MaxParticipants,
persistenceEntity.JoinPolicy,
persistenceEntity.State
);
}
public async Task Save(Domain.Models.Whiteboard.Whiteboard whiteboard, CancellationToken cancellationToken = default)
protected override Whiteboard MapToPersistenceEntity(Domain.Models.Whiteboard.Whiteboard domainEntity)
{
var whiteboardEntity = await _context.Whiteboards.FindAsync(new Guid(whiteboard.Id.IdValue));
if (whiteboardEntity is not null)
return new Whiteboard
{
whiteboardEntity.OwnerId = new Guid(whiteboard.WhiteboardOwnerId.IdValue);
whiteboardEntity.Code = whiteboard.Code.CodeValue;
whiteboardEntity.Title = whiteboard.Title.TitleValue;
whiteboardEntity.CreatedAt = whiteboard.CreatedAt.CreatedAtValue;
whiteboardEntity.DeletedAt = whiteboard.DeletedAt.DeletedAtValue;
whiteboardEntity.MaxParticipants = whiteboard.MaxParticipants.MaxParticipantsValue;
whiteboardEntity.JoinPolicy = whiteboard.JoinPolicy;
whiteboardEntity.State = whiteboard.State;
Id = new Guid(domainEntity.Id.IdValue),
OwnerId = new Guid(domainEntity.WhiteboardOwnerId.IdValue),
Code = domainEntity.Code.CodeValue,
Title = domainEntity.Title.TitleValue,
CreatedAt = domainEntity.CreatedAt.CreatedAtValue,
DeletedAt = domainEntity.DeletedAt.DeletedAtValue,
MaxParticipants = domainEntity.MaxParticipants.MaxParticipantsValue,
JoinPolicy = domainEntity.JoinPolicy,
State = domainEntity.State
};
}
_context.Whiteboards.Update(whiteboardEntity);
}
else
{
whiteboardEntity = new Whiteboard()
{
Id = new Guid(whiteboard.Id.IdValue),
OwnerId = new Guid(whiteboard.WhiteboardOwnerId.IdValue),
Code = whiteboard.Code.CodeValue,
Title = whiteboard.Title.TitleValue,
CreatedAt = whiteboard.CreatedAt.CreatedAtValue,
DeletedAt = whiteboard.DeletedAt.DeletedAtValue,
MaxParticipants = whiteboard.MaxParticipants.MaxParticipantsValue,
JoinPolicy = whiteboard.JoinPolicy,
State = whiteboard.State
};
_context.Whiteboards.Add(whiteboardEntity);
}
protected override void UpdatePersistenceEntity(Whiteboard persistenceEntity, Domain.Models.Whiteboard.Whiteboard domainEntity)
{
persistenceEntity.Code = domainEntity.Code.CodeValue;
persistenceEntity.Title = domainEntity.Title.TitleValue;
persistenceEntity.CreatedAt = domainEntity.CreatedAt.CreatedAtValue;
persistenceEntity.DeletedAt = domainEntity.DeletedAt.DeletedAtValue;
persistenceEntity.MaxParticipants = domainEntity.MaxParticipants.MaxParticipantsValue;
persistenceEntity.JoinPolicy = domainEntity.JoinPolicy;
persistenceEntity.State = domainEntity.State;
}
public async Task<bool> WhiteboardCodeExists(WhiteboardCode whiteboardCode)
{
var codeExists = await _context.Whiteboards.AnyAsync(w => w.Code == whiteboardCode.CodeValue);
return codeExists;
return await Context.Whiteboards.AnyAsync(w => w.Code == whiteboardCode.CodeValue);
}
}

View File

@@ -1,63 +1,52 @@
using AipsCore.Domain.Models.WhiteboardMembership.External;
using AipsCore.Domain.Models.WhiteboardMembership.ValueObjects;
using AipsCore.Infrastructure.Persistence.Abstract;
using AipsCore.Infrastructure.Persistence.Db;
namespace AipsCore.Infrastructure.Persistence.WhiteboardMembership;
public class WhiteboardMembershipRepository : IWhiteboardMembershipRepository
public class WhiteboardMembershipRepository
: AbstractRepository<Domain.Models.WhiteboardMembership.WhiteboardMembership, WhiteboardMembershipId, WhiteboardMembership>,
IWhiteboardMembershipRepository
{
private readonly AipsDbContext _context;
public WhiteboardMembershipRepository(AipsDbContext context)
: base(context)
{
_context = context;
}
public async Task<Domain.Models.WhiteboardMembership.WhiteboardMembership?> Get(WhiteboardMembershipId whiteboardMembershipId, CancellationToken cancellationToken = default)
protected override Domain.Models.WhiteboardMembership.WhiteboardMembership MapToDomainEntity(WhiteboardMembership persistenceEntity)
{
var whiteboardMembershipEntity = await _context.WhiteboardMemberships.FindAsync(new Guid(whiteboardMembershipId.IdValue));
if (whiteboardMembershipEntity is null) return null;
return Domain.Models.WhiteboardMembership.WhiteboardMembership.Create(
whiteboardMembershipEntity.Id.ToString(),
whiteboardMembershipEntity.WhiteboardId.ToString(),
whiteboardMembershipEntity.UserId.ToString(),
whiteboardMembershipEntity.IsBanned,
whiteboardMembershipEntity.EditingEnabled,
whiteboardMembershipEntity.CanJoin,
whiteboardMembershipEntity.LastInteractedAt);
persistenceEntity.Id.ToString(),
persistenceEntity.WhiteboardId.ToString(),
persistenceEntity.UserId.ToString(),
persistenceEntity.IsBanned,
persistenceEntity.EditingEnabled,
persistenceEntity.CanJoin,
persistenceEntity.LastInteractedAt
);
}
public async Task Save(Domain.Models.WhiteboardMembership.WhiteboardMembership whiteboardMembership, CancellationToken cancellationToken = default)
protected override WhiteboardMembership MapToPersistenceEntity(Domain.Models.WhiteboardMembership.WhiteboardMembership domainEntity)
{
var whiteboardMembershipEntity = await _context.WhiteboardMemberships.FindAsync(new Guid(whiteboardMembership.Id.IdValue));
if (whiteboardMembershipEntity is not null)
return new WhiteboardMembership
{
whiteboardMembershipEntity.IsBanned = whiteboardMembership.IsBanned.IsBannedValue;
whiteboardMembershipEntity.EditingEnabled = whiteboardMembership.EditingEnabled.EditingEnabledValue;
whiteboardMembershipEntity.CanJoin = whiteboardMembership.CanJoin.CanJoinValue;
whiteboardMembershipEntity.LastInteractedAt = whiteboardMembership.LastInteractedAt.LastInteractedAtValue;
Id = new Guid(domainEntity.Id.IdValue),
WhiteboardId = new Guid(domainEntity.WhiteboardId.IdValue),
UserId = new Guid(domainEntity.UserId.IdValue),
IsBanned = domainEntity.IsBanned.IsBannedValue,
EditingEnabled = domainEntity.EditingEnabled.EditingEnabledValue,
CanJoin = domainEntity.CanJoin.CanJoinValue,
LastInteractedAt = domainEntity.LastInteractedAt.LastInteractedAtValue
};
}
_context.Update(whiteboardMembershipEntity);
}
else
{
whiteboardMembershipEntity = new WhiteboardMembership()
{
Id = new Guid(whiteboardMembership.Id.IdValue),
WhiteboardId = new Guid(whiteboardMembership.WhiteboardId.IdValue),
Whiteboard = null,
UserId = new Guid(whiteboardMembership.UserId.IdValue),
User = null,
IsBanned = whiteboardMembership.IsBanned.IsBannedValue,
EditingEnabled = whiteboardMembership.EditingEnabled.EditingEnabledValue,
CanJoin = whiteboardMembership.CanJoin.CanJoinValue,
LastInteractedAt = whiteboardMembership.LastInteractedAt.LastInteractedAtValue
};
_context.Add(whiteboardMembershipEntity);
}
protected override void UpdatePersistenceEntity(WhiteboardMembership persistenceEntity, Domain.Models.WhiteboardMembership.WhiteboardMembership domainEntity)
{
persistenceEntity.IsBanned = domainEntity.IsBanned.IsBannedValue;
persistenceEntity.EditingEnabled = domainEntity.EditingEnabled.EditingEnabledValue;
persistenceEntity.CanJoin = domainEntity.CanJoin.CanJoinValue;
persistenceEntity.LastInteractedAt = domainEntity.LastInteractedAt.LastInteractedAtValue;
}
}