| | | 1 | | using Enjoy.Domain.Shared.Errors; |
| | | 2 | | using Enjoy.Domain.Shared.Extensions; |
| | | 3 | | using Enjoy.Domain.Shared.Results; |
| | | 4 | | |
| | | 5 | | namespace Enjoy.Domain.Topics.ValueObjects; |
| | | 6 | | |
| | | 7 | | public readonly record struct TopicName |
| | | 8 | | { |
| | 12 | 9 | | public string Value { get; } |
| | | 10 | | |
| | | 11 | | public const int MinLength = 2; |
| | | 12 | | public const int MaxLength = 100; |
| | | 13 | | |
| | 13 | 14 | | private TopicName(string value) => Value = value; |
| | | 15 | | |
| | | 16 | | public static Result<TopicName> Create(string? value) |
| | 22 | 17 | | { |
| | 22 | 18 | | if (string.IsNullOrWhiteSpace(value)) |
| | 6 | 19 | | return Result<TopicName>.Failure(Error.TopicName.IsNullOrEmpty); |
| | | 20 | | |
| | 16 | 21 | | var normalized = value.Trim().CapitalizeWords(); |
| | 16 | 22 | | List<Error> errors = []; |
| | 16 | 23 | | if (normalized.Length < MinLength) |
| | 2 | 24 | | errors.Add(Error.TopicName.TooShort(MinLength)); |
| | 16 | 25 | | if (normalized.Length > MaxLength) |
| | 1 | 26 | | errors.Add(Error.TopicName.TooLong(MaxLength)); |
| | | 27 | | |
| | 16 | 28 | | return errors.IsEmpty() |
| | 16 | 29 | | ? Result<TopicName>.Success(new TopicName(normalized)) |
| | 16 | 30 | | : Result<TopicName>.Failure(errors); |
| | 22 | 31 | | } |
| | | 32 | | |
| | 1 | 33 | | public override string ToString() => Value; |
| | | 34 | | |
| | 1 | 35 | | public static implicit operator string(TopicName name) => name.Value; |
| | | 36 | | } |