| | | 1 | | using Enjoy.Domain.Shared.Errors; |
| | | 2 | | using Enjoy.Domain.Shared.Extensions; |
| | | 3 | | using Enjoy.Domain.Shared.Messaging; |
| | | 4 | | using Enjoy.Domain.Shared.Primitives; |
| | | 5 | | using Enjoy.Domain.Shared.Results; |
| | | 6 | | using Enjoy.Domain.Users.Events; |
| | | 7 | | using Enjoy.Domain.Users.ValueObjects; |
| | | 8 | | |
| | | 9 | | namespace Enjoy.Domain.Users.Entities; |
| | | 10 | | |
| | | 11 | | public sealed class User : AggregateRoot, IAuditableEntity |
| | | 12 | | { |
| | 50 | 13 | | public static string NewId() => $"usr_{Guid.CreateVersion7()}"; |
| | | 14 | | |
| | 58 | 15 | | public Name Name { get; private set; } |
| | 58 | 16 | | public Email Email { get; private set; } |
| | 55 | 17 | | public PasswordHash PasswordHash { get; private set; } |
| | 58 | 18 | | public Role Role { get; private set; } |
| | | 19 | | |
| | 3 | 20 | | public DateTime CreatedOnUtc { get; set; } |
| | 3 | 21 | | public DateTime? ModifiedOnUtc { get; set; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Parameterless constructor required by Entity Framework Core. |
| | | 25 | | /// </summary> |
| | 3 | 26 | | private User() : base() { } |
| | | 27 | | |
| | 47 | 28 | | private User(string id, Name name, Email email, PasswordHash passwordHash, Role role) : base(id) |
| | 47 | 29 | | { |
| | 47 | 30 | | Name = name; |
| | 47 | 31 | | Email = email; |
| | 47 | 32 | | PasswordHash = passwordHash; |
| | 47 | 33 | | Role = role; |
| | 47 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static Result<User> Create(string name, string email, string passwordHash, string role) |
| | 53 | 37 | | { |
| | 53 | 38 | | List<Error> errors = []; |
| | | 39 | | |
| | 53 | 40 | | var nameResult = Name.Create(name); |
| | 53 | 41 | | if (nameResult.IsFailure) |
| | 3 | 42 | | errors.AddRange(nameResult.Errors); |
| | | 43 | | |
| | 53 | 44 | | var emailResult = Email.Create(email); |
| | 53 | 45 | | if (emailResult.IsFailure) |
| | 2 | 46 | | errors.AddRange(emailResult.Errors); |
| | | 47 | | |
| | 53 | 48 | | var passwordHashResult = PasswordHash.Create(passwordHash); |
| | 53 | 49 | | if (passwordHashResult.IsFailure) |
| | 2 | 50 | | errors.AddRange(passwordHashResult.Errors); |
| | | 51 | | |
| | 53 | 52 | | var roleResult = Role.Create(role); |
| | 53 | 53 | | if (roleResult.IsFailure) |
| | 2 | 54 | | errors.AddRange(roleResult.Errors); |
| | | 55 | | |
| | 53 | 56 | | if (!errors.IsEmpty()) |
| | 6 | 57 | | return Result<User>.Failure(errors); |
| | | 58 | | |
| | 47 | 59 | | User user = new( |
| | 47 | 60 | | NewId(), |
| | 47 | 61 | | nameResult.Value, |
| | 47 | 62 | | emailResult.Value, |
| | 47 | 63 | | passwordHashResult.Value, |
| | 47 | 64 | | roleResult.Value); |
| | 47 | 65 | | user.RaiseDomainEvent(new UserRegisteredDomainEvent(DomainEvent.NewId(), user.Id)); |
| | 47 | 66 | | return Result<User>.Success(user); |
| | 53 | 67 | | } |
| | | 68 | | |
| | | 69 | | public Result ChangeName(string name) |
| | 6 | 70 | | { |
| | 6 | 71 | | var nameResult = Name.Create(name); |
| | 6 | 72 | | if (nameResult.IsFailure) |
| | 2 | 73 | | return Result.Failure(nameResult.Errors); |
| | 4 | 74 | | if (Name == nameResult.Value) |
| | 1 | 75 | | return Result.Success(); |
| | | 76 | | |
| | 3 | 77 | | Name = nameResult.Value; |
| | 3 | 78 | | RaiseDomainEvent(new UserNameChangedDomainEvent(DomainEvent.NewId(), Id)); |
| | 3 | 79 | | return Result.Success(); |
| | 6 | 80 | | } |
| | | 81 | | |
| | | 82 | | public Result ChangeEmail(string email) |
| | 6 | 83 | | { |
| | 6 | 84 | | var emailResult = Email.Create(email); |
| | 6 | 85 | | if (emailResult.IsFailure) |
| | 2 | 86 | | return Result.Failure(emailResult.Errors); |
| | 4 | 87 | | if (Email == emailResult.Value) |
| | 1 | 88 | | return Result.Success(); |
| | | 89 | | |
| | 3 | 90 | | Email = emailResult.Value; |
| | 3 | 91 | | RaiseDomainEvent(new UserEmailChangedDomainEvent(DomainEvent.NewId(), Id)); |
| | 3 | 92 | | return Result.Success(); |
| | 6 | 93 | | } |
| | | 94 | | |
| | | 95 | | public Result SetPasswordHash(string passwordHash) |
| | 5 | 96 | | { |
| | 5 | 97 | | var result = PasswordHash.Create(passwordHash); |
| | 5 | 98 | | if (result.IsFailure) |
| | 2 | 99 | | return Result.Failure(result.Errors); |
| | 3 | 100 | | if (PasswordHash.Value == result.Value.Value) |
| | 1 | 101 | | return Result.Success(); |
| | | 102 | | |
| | 2 | 103 | | PasswordHash = result.Value; |
| | 2 | 104 | | RaiseDomainEvent(new UserPasswordChangedDomainEvent(DomainEvent.NewId(), Id)); |
| | 2 | 105 | | return Result.Success(); |
| | 5 | 106 | | } |
| | | 107 | | |
| | | 108 | | public Result ChangeRole(string role) |
| | 5 | 109 | | { |
| | 5 | 110 | | var roleResult = Role.Create(role); |
| | 5 | 111 | | if (roleResult.IsFailure) |
| | 2 | 112 | | return Result.Failure(roleResult.Errors); |
| | 3 | 113 | | if (Role == roleResult.Value) |
| | 1 | 114 | | return Result.Success(); |
| | | 115 | | |
| | 2 | 116 | | Role = roleResult.Value; |
| | 2 | 117 | | RaiseDomainEvent(new UserRoleChangedDomainEvent(DomainEvent.NewId(), Id, Role.Value)); |
| | 2 | 118 | | return Result.Success(); |
| | 5 | 119 | | } |
| | | 120 | | } |