users/src/Nocr.Users.AppServices/Users/User.cs
2025-07-21 16:38:22 +03:00

59 lines
1.4 KiB
C#

namespace Nocr.Users.AppServices.Users;
public sealed class User
{
public long Id { get; set; }
public string Username { get; private set; } = default!;
public bool BotBlocked { get; private set; }
private readonly List<UserIdentity> _identities = new();
public IReadOnlyCollection<UserIdentity> Identities => _identities;
/// <summary>
/// Used by ef.
/// </summary>
private User()
{
}
private User(string username, params UserIdentity[] identities)
{
Username = username;
// TODO: Check that there is no repeating identity types
if (identities.GroupBy(x => x.Type).SelectMany(g => g.Skip(1)).Any())
{
throw new InvalidOperationException($"All identities should be unique by {nameof(UserIdentity.Type)}");
}
_identities.AddRange(identities);
}
public static User Initialize(string username, params UserIdentity[] identities)
{
if (string.IsNullOrWhiteSpace(username))
{
throw new ArgumentNullException(nameof(username));
}
if (username.Length is < 5 or > 32)
{
throw new ArgumentException("Username length should be between 5 and 32 symbols", username);
}
return new User(username, identities);
}
public void MarkAsBlocked()
{
BotBlocked = true;
}
public void MarkAsUnblocked()
{
BotBlocked = false;
}
}