120 lines
3.4 KiB
C#
120 lines
3.4 KiB
C#
namespace LibraryApp.Domain.Entities;
|
|
|
|
public class Member
|
|
{
|
|
public MemberId Id { get; private set; }
|
|
public MemberName Name { get; private set; }
|
|
public List<MemberEmail> Emails { get; private set; } = new();
|
|
public List<MemberPhone> Phones { get; private set; } = new();
|
|
public int PrimaryEmailIndex { get; private set; } = 0;
|
|
public int PrimaryPhoneIndex { get; private set; } = 0;
|
|
|
|
public Member(MemberName name, MemberEmail email, MemberPhone phone)
|
|
: this(new MemberId(string.Empty), name, email, phone)
|
|
{ }
|
|
public Member(MemberId id, MemberName name, MemberEmail email, MemberPhone phone)
|
|
{
|
|
|
|
Id = id;
|
|
Name = name;
|
|
Emails.Add(email);
|
|
Phones.Add(phone);
|
|
}
|
|
|
|
public Member UpdateName(string newName)
|
|
{
|
|
Name = new (newName);
|
|
return this;
|
|
}
|
|
public Member AddEmail(string email)
|
|
{
|
|
Emails.Add(new MemberEmail(email));
|
|
return this;
|
|
}
|
|
public Member AddPhone(string phone)
|
|
{
|
|
Phones.Add(new MemberPhone(phone));
|
|
return this;
|
|
}
|
|
public Member SetPrimaryEmail(MemberEmail email)
|
|
{
|
|
var index = Emails.FindIndex(e => e.Email == email.Email);
|
|
if(index == -1)
|
|
throw new ArgumentException("Email not found in member's email list.", nameof(email));
|
|
PrimaryEmailIndex = index;
|
|
return this;
|
|
}
|
|
public Member SetPrimaryPhone(MemberPhone phone)
|
|
{
|
|
var index = Phones.FindIndex(p => p.Phone == phone.Phone);
|
|
if(index == -1)
|
|
throw new ArgumentException("Phone not found in member's phone list.", nameof(phone));
|
|
PrimaryPhoneIndex = index;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
public record MemberId : DbId
|
|
{
|
|
public MemberId(string id) : base(id) {}
|
|
}
|
|
|
|
public record MemberEmail
|
|
{
|
|
public string Email { get; init; }
|
|
|
|
public MemberEmail(string email)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(email))
|
|
throw new ArgumentException("Email cannot be empty.");
|
|
|
|
if(!email.Contains("@"))
|
|
throw new ArgumentException("Email must contain '@'.");
|
|
|
|
if(!email.Contains("."))
|
|
throw new ArgumentException("Email must contain '.'.");
|
|
|
|
if(email.StartsWith("@") || email.EndsWith("@"))
|
|
throw new ArgumentException("Email cannot start or end with '@'.");
|
|
|
|
var atCount = email.Count(c => c == '@');
|
|
if(atCount > 1)
|
|
throw new ArgumentException("Email cannot contain more than one '@'.");
|
|
|
|
var dotCount = email.Count(c => c == '.');
|
|
if(dotCount > 1)
|
|
throw new ArgumentException("Email cannot contain more than one '.'.");
|
|
|
|
Email = email;
|
|
}
|
|
}
|
|
|
|
public record MemberName
|
|
{
|
|
public string Name { get; init; }
|
|
|
|
public MemberName(string name)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(name))
|
|
{
|
|
throw new ArgumentException("Member name cannot be empty.");
|
|
}
|
|
Name = name;
|
|
}
|
|
}
|
|
|
|
public record MemberPhone
|
|
{
|
|
private const string VALID_PHONE_PATTERN = @"^\+?[1-9]\d{1,14}$";
|
|
public string Phone { get; init; }
|
|
|
|
public MemberPhone(string phone)
|
|
{
|
|
if(string.IsNullOrWhiteSpace(phone))
|
|
throw new ArgumentException("Phone number cannot be empty.");
|
|
if(!System.Text.RegularExpressions.Regex.IsMatch(phone, VALID_PHONE_PATTERN))
|
|
throw new ArgumentException("Phone number is not in a valid format.");
|
|
Phone = phone;
|
|
}
|
|
}
|