using MagicCarpetContracts.Exceptions; using MagicCarpetContracts.Extensions; using MagicCarpetContracts.Infrastructure; using MagicCarpetContracts.Resources; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Localization; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace MagicCarpetContracts.DataModels; internal class EmployeeDataModel(string id, string fio, string email, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation { private readonly PostDataModel? _post; public string Id { get; private set; } = id; public string FIO { get; private set; } = fio; public string Email { get; private set; } = email; public string PostId { get; private set; } = postId; public DateTime BirthDate { get; private set; } = birthDate.ToUniversalTime(); public DateTime EmploymentDate { get; private set; } = employmentDate.ToUniversalTime(); public bool IsDeleted { get; private set; } = isDeleted; public string PostName => _post?.PostName ?? string.Empty; public EmployeeDataModel(string id, string fio, string email, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted, PostDataModel post) : this(id, fio, email, postId, birthDate, employmentDate, isDeleted) { _post = post; } public EmployeeDataModel(string id, string fio, string email, string postId, DateTime birthDate, DateTime employmentDate) : this(id, fio, email, postId, birthDate, employmentDate, false) { } public void Validate(IStringLocalizer localizer) { if (Id.IsEmpty()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Id")); if (!Id.IsGuid()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "Id")); if (FIO.IsEmpty()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "FIO")); if (Email.IsEmpty()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmptyField"], "Email")); if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) throw new ValidationException(localizer["ValidationExceptionMessageIncorrectEmail"]); if (PostId.IsEmpty()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "PostId")); if (!PostId.IsGuid()) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageNotAId"], "PostId")); if (BirthDate.Date > DateTime.Now.AddYears(-18).Date) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageMinorsBirthDate"], BirthDate.ToShortDateString())); if (EmploymentDate.Date < BirthDate.Date) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageEmploymentDateAndBirthDate"], EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString())); if ((EmploymentDate - BirthDate).TotalDays / 365 < 18) throw new ValidationException(string.Format(localizer["ValidationExceptionMessageMinorsEmploymentDate"], EmploymentDate.ToShortDateString(), BirthDate.ToShortDateString())); } }