Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPContracts/DataModels/UserDataModel.cs
2025-05-27 00:09:01 +04:00

50 lines
1.7 KiB
C#

using System.Text.RegularExpressions;
using YAPContracts.Enums;
using YAPContracts.Extentions;
using YAPContracts.Infrastructure;
namespace YAPContracts.DataModels
{
public class UserDataModel(string id, string login, string email, string passwordHash, UserType role) : IValidation
{
public string Id { get; private set; } = id;
public string Login { get; private set; } = login;
public string Email { get; private set; } = email;
public string PasswordHash { get; private set; } = passwordHash;
public UserType Role { get; private set; } = role; // Worker, Storekeeper
public void Validate()
{
if (Id.IsEmpty())
{
throw new Exceptions.ValidationException("Id is empty");
}
if (!Id.IsGuid())
{
throw new Exceptions.ValidationException("Id value is not valid");
}
if (Login.IsEmpty())
{
throw new Exceptions.ValidationException("Login is empty");
}
if (!Regex.IsMatch(Login, @"^[a-zA-Z0-9_-]+$"))
{
throw new Exceptions.ValidationException("Login value is not valid");
}
if (Email.IsEmpty())
{
throw new Exceptions.ValidationException("Email is empty");
}
if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"))
{
throw new Exceptions.ValidationException("Email value is not valid");
}
if (PasswordHash.IsEmpty())
{
throw new Exceptions.ValidationException("PasswordHash is empty");
}
}
}
}