Files
probearbeit/api/Domain/Person.cs
T
2026-08-30 16:55:53 +02:00

41 lines
1.3 KiB
C#

namespace api.Domain;
public sealed class Person
{
public string Username { get; }
public string RealName { get; }
public DateOnly Birthday { get; }
private Person(string username, string realName, DateOnly birthday)
{
Username = username;
RealName = realName;
Birthday = birthday;
}
public static Person Create(string username, string realName, DateOnly birthday)
{
username = NormalizeUsername(username);
realName = realName.Trim();
// TODO: Error Handling
if (username.Length is < 3 or > 32) throw new Exception();
if (realName.Length == 0) throw new Exception();
if (birthday >= DateOnly.FromDateTime(DateTime.Today)) throw new Exception();
if (!IsAtLeast16(birthday)) throw new Exception();
return new Person(username, realName, birthday);
}
// Konstruktor zum Erzeugen von Nutzerobjekten aus der Datenbank
internal static Person FromStorage(string username, string realName, DateOnly birthday)
=> new(username, realName, birthday);
public static string NormalizeUsername(string username)
=> username.Trim().ToLowerInvariant();
public static bool IsAtLeast16(DateOnly birthday)
=> birthday.AddYears(16) <= DateOnly.FromDateTime(DateTime.Today);
}