added constructors and checks
This commit is contained in:
1 parent
d6f863316f
commit
de1f87b3e4
1 file changed
+32
-2
+32
-2
@@ -2,10 +2,40 @@ namespace api.Domain;
|
||||
|
||||
public sealed class Person
|
||||
{
|
||||
public String Username { get; }
|
||||
public string Username { get; }
|
||||
|
||||
public String RealName { 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);
|
||||
}
|
||||
Reference in new issue
Block a user