Merge pull request #11 from StewKI/feature-user-model-expansion

Expanded on user domain model
This commit is contained in:
Veljko
2026-02-10 13:00:48 +01:00
committed by GitHub
7 changed files with 87 additions and 10 deletions

View File

@@ -3,4 +3,9 @@ using AipsCore.Domain.Models.User.ValueObjects;
namespace AipsCore.Application.Models.User.Command.CreateUser; namespace AipsCore.Application.Models.User.Command.CreateUser;
public record CreateUserCommand(string Username, string Email) : ICommand<UserId>; public record CreateUserCommand(
string Username,
string Email,
DateTime CreatedAt,
DateTime DeletedAt)
: ICommand<UserId>;

View File

@@ -18,7 +18,7 @@ public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, UserI
public async Task<UserId> Handle(CreateUserCommand command, CancellationToken cancellationToken = default) public async Task<UserId> Handle(CreateUserCommand command, CancellationToken cancellationToken = default)
{ {
var user = Domain.Models.User.User.Create(command.Email, command.Username); var user = Domain.Models.User.User.Create(command.Email, command.Username, command.CreatedAt, command.DeletedAt);
await _userRepository.Save(user, cancellationToken); await _userRepository.Save(user, cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken); await _unitOfWork.SaveChangesAsync(cancellationToken);

View File

@@ -8,26 +8,36 @@ public class User
public UserId Id { get; private set; } public UserId Id { get; private set; }
public Email Email { get; private set; } public Email Email { get; private set; }
public Username Username { get; private set; } public Username Username { get; private set; }
public UserCreatedAt CreatedAt { get; private set; }
public UserDeletedAt DeletedAt { get; private set; }
public User(UserId id, Email email, Username username) public User(UserId id, Email email, Username username, UserCreatedAt createdAt, UserDeletedAt deletedAt)
{ {
Id = id; Id = id;
Email = email; Email = email;
Username = username; Username = username;
CreatedAt = createdAt;
DeletedAt = deletedAt;
} }
public static User Create(string id, string email, string username) public static User Create(string id, string email, string username, DateTime createdAt, DateTime? deletedAt)
{ {
var userIdVo = new UserId(id); var userIdVo = new UserId(id);
var usernameVo = new Username(username); var usernameVo = new Username(username);
var emailVo = new Email(email); var emailVo = new Email(email);
return new User( userIdVo, emailVo, usernameVo); var createdAtVo = new UserCreatedAt(createdAt);
var deletedAtVo = new UserDeletedAt(deletedAt);
return new User(userIdVo, emailVo, usernameVo, createdAtVo, deletedAtVo);
} }
public static User Create(string email, string username) public static User Create(string email, string username, DateTime createdAt, DateTime? deletedAt)
{ {
var usernameVo = new Username(username); var usernameVo = new Username(username);
var emailVo = new Email(email); var emailVo = new Email(email);
return new User( UserId.Any(), emailVo, usernameVo); var createdAtVo = new UserCreatedAt(createdAt);
var deletedAtVo = new UserDeletedAt(deletedAt);
return new User(UserId.Any(), emailVo, usernameVo, createdAtVo, deletedAtVo);
} }
} }

View File

@@ -0,0 +1,23 @@
using AipsCore.Domain.Abstract.Rule;
using AipsCore.Domain.Abstract.ValueObject;
using AipsCore.Domain.Common.Validation.Rules;
namespace AipsCore.Domain.Models.User.ValueObjects;
public record UserCreatedAt : AbstractValueObject
{
public DateTime CreatedAtValue { get; private set; }
public UserCreatedAt(DateTime CreatedAtValue)
{
this.CreatedAtValue = CreatedAtValue;
Validate();
}
protected override ICollection<IRule> GetValidationRules()
{
return [
new DateInPastRule(CreatedAtValue)
];
}
}

View File

@@ -0,0 +1,23 @@
using AipsCore.Domain.Abstract.Rule;
using AipsCore.Domain.Abstract.ValueObject;
using AipsCore.Domain.Common.Validation.Rules;
namespace AipsCore.Domain.Models.User.ValueObjects;
public record UserDeletedAt : AbstractValueObject
{
public DateTime? DeletedAtValue { get; private set; }
public UserDeletedAt(DateTime? DeletedAtValue)
{
this.DeletedAtValue = DeletedAtValue;
Validate();
}
protected override ICollection<IRule> GetValidationRules()
{
return [
new DateInPastRule(DeletedAtValue)
];
}
}

View File

@@ -7,6 +7,16 @@ public class User
{ {
[Key] [Key]
public Guid Id { get; set; } public Guid Id { get; set; }
[Required] [MaxLength(255)] public string Username { get; set; } = null!;
[Required] [MaxLength(255)] public string Email { get; set; } = null!; [Required]
[MaxLength(255)]
public string Username { get; set; } = null!;
[Required]
[MaxLength(255)]
public string Email { get; set; } = null!;
public DateTime CreatedAt { get; set; }
public DateTime? DeletedAt { get; set; }
} }

View File

@@ -22,7 +22,9 @@ public class UserRepository : IUserRepository
return Domain.Models.User.User.Create( return Domain.Models.User.User.Create(
userEntity.Id.ToString(), userEntity.Id.ToString(),
userEntity.Email, userEntity.Email,
userEntity.Username); userEntity.Username,
userEntity.CreatedAt,
userEntity.DeletedAt);
} }
public async Task Save(Domain.Models.User.User user, CancellationToken cancellationToken = default) public async Task Save(Domain.Models.User.User user, CancellationToken cancellationToken = default)
@@ -34,6 +36,8 @@ public class UserRepository : IUserRepository
{ {
userEntity.Email = user.Email.EmailValue; userEntity.Email = user.Email.EmailValue;
userEntity.Username = user.Username.UsernameValue; userEntity.Username = user.Username.UsernameValue;
userEntity.CreatedAt = user.CreatedAt.CreatedAtValue;
userEntity.DeletedAt = user.DeletedAt.DeletedAtValue;
_context.Users.Update(userEntity); _context.Users.Update(userEntity);
} }
@@ -44,6 +48,8 @@ public class UserRepository : IUserRepository
Id = new Guid(user.Id.IdValue), Id = new Guid(user.Id.IdValue),
Email = user.Email.EmailValue, Email = user.Email.EmailValue,
Username = user.Username.UsernameValue, Username = user.Username.UsernameValue,
CreatedAt = user.CreatedAt.CreatedAtValue,
DeletedAt = user.DeletedAt.DeletedAtValue
}; };
_context.Users.Add(userEntity); _context.Users.Add(userEntity);