user create command and user repo

This commit is contained in:
2026-02-04 22:18:05 +01:00
parent b73fd8eb05
commit 01f25fb093
10 changed files with 136 additions and 5 deletions

View File

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

View File

@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
namespace AipsCore.Infrastructure.Entities;
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,53 @@
using AipsCore.Domain.Common.ValueObjects;
using AipsCore.Domain.Models.User;
using AipsCore.Domain.Models.User.External;
using AipsCore.Domain.Models.User.ValueObjects;
using AipsCore.Infrastructure.Db;
namespace AipsCore.Infrastructure.Repositories;
public class UserRepository : IUserRepository
{
private readonly AipsDbContext _context;
public UserRepository(AipsDbContext context)
{
_context = context;
}
public async Task<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 User.Create(
userEntity.Id.ToString(),
userEntity.Email,
userEntity.Username);
}
public async Task Save(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 Entities.User()
{
Id = new Guid(user.Id.IdValue),
Email = user.Email.EmailValue,
Username = user.Username.UsernameValue,
};
_context.Users.Add(userEntity);
}
}
}