| | | 1 | | using Enjoy.Domain.Shared.Errors; |
| | | 2 | | using Enjoy.Domain.Shared.Results; |
| | | 3 | | |
| | | 4 | | namespace Enjoy.Domain.Users.ValueObjects; |
| | | 5 | | |
| | | 6 | | public readonly record struct Role |
| | | 7 | | { |
| | | 8 | | public const string User = nameof(User); |
| | | 9 | | public const string Admin = nameof(Admin); |
| | | 10 | | |
| | 1 | 11 | | public static readonly IReadOnlyCollection<string> All = [User, Admin]; |
| | | 12 | | |
| | 1 | 13 | | private static readonly HashSet<string> ValidValues = |
| | 1 | 14 | | new(All, StringComparer.OrdinalIgnoreCase); |
| | | 15 | | |
| | 9 | 16 | | public string Value { get; } |
| | | 17 | | |
| | 63 | 18 | | private Role(string value) => Value = value; |
| | | 19 | | |
| | | 20 | | public static Result<Role> Create(string? value) |
| | 69 | 21 | | { |
| | 69 | 22 | | if (string.IsNullOrWhiteSpace(value)) |
| | 3 | 23 | | return Result<Role>.Failure(Error.Role.IsNullOrEmpty); |
| | | 24 | | |
| | 66 | 25 | | if (!ValidValues.TryGetValue(value.Trim(), out var canonical)) |
| | 7 | 26 | | return Result<Role>.Failure(Error.Role.Invalid); |
| | | 27 | | |
| | 59 | 28 | | return Result<Role>.Success(new Role(canonical)); |
| | 69 | 29 | | } |
| | | 30 | | |
| | 2 | 31 | | public static Role CreateUser() => new(User); |
| | 2 | 32 | | public static Role CreateAdmin() => new(Admin); |
| | | 33 | | |
| | 1 | 34 | | public override string ToString() => Value; |
| | | 35 | | |
| | 1 | 36 | | public static implicit operator string(Role role) => role.Value; |
| | | 37 | | } |