| | | 1 | | using MechanicsSoftware.Domain.Enums; |
| | | 2 | | using MechanicsSoftware.Domain.Exceptions; |
| | | 3 | | using MechanicsSoftware.Domain.ValueObjects; |
| | | 4 | | |
| | | 5 | | namespace MechanicsSoftware.Domain.Entities; |
| | | 6 | | |
| | | 7 | | public sealed class Customer : Entity<Guid> |
| | | 8 | | { |
| | | 9 | | public string Name { get; private set; } = null!; |
| | | 10 | | public TaxId Document { get; private set; } = null!; |
| | | 11 | | public Email Email { get; private set; } = null!; |
| | | 12 | | public string Phone { get; private set; } = null!; |
| | | 13 | | |
| | 0 | 14 | | private Customer() { } |
| | | 15 | | |
| | 76 | 16 | | private Customer(Guid id, string name, TaxId taxId, Email email, string phone) |
| | 76 | 17 | | { |
| | 76 | 18 | | Id = id; |
| | 76 | 19 | | Name = name; |
| | 76 | 20 | | Document = taxId; |
| | 76 | 21 | | Email = email; |
| | 76 | 22 | | Phone = phone; |
| | 76 | 23 | | } |
| | | 24 | | |
| | | 25 | | public static Customer Create(Guid id, string name, string taxId, PersonType personType, string email, string phone) |
| | 94 | 26 | | { |
| | 94 | 27 | | ValidateName(name); |
| | 86 | 28 | | ValidatePhone(phone); |
| | | 29 | | |
| | 80 | 30 | | return new Customer( |
| | 80 | 31 | | id, |
| | 80 | 32 | | name.Trim(), |
| | 80 | 33 | | new TaxId(taxId, personType), |
| | 80 | 34 | | new Email(email), |
| | 80 | 35 | | phone.Trim() |
| | 80 | 36 | | ); |
| | 76 | 37 | | } |
| | | 38 | | |
| | | 39 | | public void Update(string name, string email, string phone) |
| | 10 | 40 | | { |
| | 10 | 41 | | ValidateName(name); |
| | 8 | 42 | | ValidatePhone(phone); |
| | | 43 | | |
| | 8 | 44 | | Name = name.Trim(); |
| | 8 | 45 | | Email = new Email(email); |
| | 6 | 46 | | Phone = phone.Trim(); |
| | 6 | 47 | | } |
| | | 48 | | |
| | | 49 | | private static void ValidateName(string name) |
| | 104 | 50 | | { |
| | 104 | 51 | | if (string.IsNullOrWhiteSpace(name)) |
| | 4 | 52 | | throw new DomainException("Customer name is required."); |
| | | 53 | | |
| | 100 | 54 | | if (name.Trim().Length > 100) |
| | 6 | 55 | | throw new DomainException("Customer name must not exceed 100 characters."); |
| | 94 | 56 | | } |
| | | 57 | | |
| | | 58 | | private static void ValidatePhone(string phone) |
| | 94 | 59 | | { |
| | 94 | 60 | | if (string.IsNullOrWhiteSpace(phone)) |
| | 4 | 61 | | throw new DomainException("Phone is required."); |
| | | 62 | | |
| | 90 | 63 | | var digits = new string(phone.Where(char.IsDigit).ToArray()); |
| | | 64 | | |
| | 90 | 65 | | if (digits.Length < 8 || digits.Length > 15) |
| | 2 | 66 | | throw new DomainException("Phone must have between 8 and 15 digits (with area code)."); |
| | 88 | 67 | | } |
| | | 68 | | } |