using System.Text.RegularExpressions; using SnowMaidenContracts.Exceptions; using SnowMaidenContracts.Extensions; using SnowMaidenContracts.Infrastructure; namespace SnowMaidenContracts.DataModels; // 1. Работник (Worker -> 2. Sale) фигурирует затем в продажах, // имеет определенную должность (Post -> Worker), имеет отношение к зачислению з/п (Worker -> Salary) public class WorkerDataModel(string id, string fio, string postId, string phoneNumber, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation { public string Id { get; private set; } = id; public string FIO { get; private set; } = fio; public string PhoneNumber { get; private set; } = phoneNumber; public string PostId { get; private set; } = postId; public DateTime BirthDate { get; private set; } = birthDate; public DateTime EmploymentDate { get; private set; } = employmentDate; public bool IsDeleted { get; private set; } = isDeleted; public void Validate() { if (Id.IsEmpty()) throw new ValidationException("Field ID is empty"); if (!Id.IsGuid()) throw new ValidationException("The ID value is NOT a unique identifier"); if (FIO.IsEmpty()) throw new ValidationException("Field FIO is empty"); if (PostId.IsEmpty()) throw new ValidationException("Field Post ID is empty"); if (!PostId.IsGuid()) throw new ValidationException("The Post ID value is not a unique identifier"); if (BirthDate.Date > DateTime.Now.AddYears(-16).Date) throw new ValidationException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()})"); if (EmploymentDate.Date < BirthDate.Date) throw new ValidationException("The date of employment cannot be less than the date of birth"); if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); if (PhoneNumber.IsEmpty()) throw new ValidationException("Field PhoneNumber is empty"); if (!Regex.IsMatch(PhoneNumber, @"^(8|\+7)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{2}[-.\s]?\d{2}")) // * throw new ValidationException("Field PhoneNumber is NOT a phone number"); } }