infrastructure restructure

This commit is contained in:
2026-02-05 19:33:52 +01:00
parent 82d44c230f
commit 50e87335f4
5 changed files with 16 additions and 19 deletions

View File

@@ -0,0 +1,8 @@
using Microsoft.EntityFrameworkCore;
namespace AipsCore.Infrastructure.Persistence.Db;
public class AipsDbContext : DbContext
{
public DbSet<User.User> Users { get; set; }
}

View File

@@ -0,0 +1,18 @@
using AipsCore.Domain.Abstract;
namespace AipsCore.Infrastructure.Persistence.Db;
public class EfUnitOfWork : IUnitOfWork
{
private readonly AipsDbContext _dbContext;
public EfUnitOfWork(AipsDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
await _dbContext.SaveChangesAsync(cancellationToken);
}
}

View File

@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
namespace AipsCore.Infrastructure.Persistence.User;
public class User
{
[Key]
public Guid Id { get; set; }
[Required] [MaxLength(255)] public string Username { get; set; } = null!;
[Required] [MaxLength(255)] public string Email { get; set; } = null!;
}

View File

@@ -0,0 +1,51 @@
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Persistence.Db;
namespace AipsCore.Infrastructure.Persistence.User;
public class UserRepository : IUserRepository
{
private readonly AipsDbContext _context;
public UserRepository(AipsDbContext context)
{
_context = context;
}
public async Task<Domain.Models.User.User?> Get(UserId userId, CancellationToken cancellationToken = default)
{
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);
}
public async Task Save(Domain.Models.User.User user, CancellationToken cancellationToken = default)
{
var userEntity = await _context.Users.FindAsync([new Guid(user.Id.IdValue), cancellationToken], cancellationToken: cancellationToken);
if (userEntity is not null)
{
userEntity.Email = user.Email.EmailValue;
userEntity.Username = user.Username.UsernameValue;
_context.Users.Update(userEntity);
}
else
{
userEntity = new User()
{
Id = new Guid(user.Id.IdValue),
Email = user.Email.EmailValue,
Username = user.Username.UsernameValue,
};
_context.Users.Add(userEntity);
}
}
}