Files
probearbeit/api/Domain/Person.cs
T
2026-08-31 14:13:22 +02:00

55 lines
1.9 KiB
C#

using System.ComponentModel.DataAnnotations;
namespace api.Domain;
public sealed class Person
{
[Key]
public string Username { get; private set; }
public string RealName { get; private set; }
public DateOnly Birthday { get; private set; }
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 ArgumentException("Username must be 3-32 chars.");
if (realName.Length == 0) throw new ArgumentException("Name cannot be empty");
if (!IsAtLeast16(birthday) || birthday >= DateOnly.FromDateTime(DateTime.Today)) throw new ArgumentException("Person must be at least 16 years old.");
return new Person(username, realName, birthday);
}
public void Update(string realName, DateOnly birthday)
{
realName = realName.Trim();
if (realName.Length == 0) throw new ArgumentException("Name cannot be empty");
if (!IsAtLeast16(birthday) || birthday >= DateOnly.FromDateTime(DateTime.Today))
throw new ArgumentException("Person must be at least 16 years old.");
RealName = realName;
Birthday = 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);
}