diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/ClientDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/ClientDataModel.cs new file mode 100644 index 0000000..8669f11 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/ClientDataModel.cs @@ -0,0 +1,33 @@ +using System.Text.RegularExpressions; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class ClientDataModel(string id, string fio, string email) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fio; + + public string Email { get; private set; } = email; + + public void Validate() + { + if (Id.IsEmpty()) + throw new ValidationException("Field Id is empty"); + + if (!Id.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + + if (FIO.IsEmpty()) + throw new ValidationException("Field FIO is empty"); + + if (Email.IsEmpty()) + throw new ValidationException("Field Email is empty"); + + if (!Regex.IsMatch(Email, @"^\S+@\S+\.\S+$")) + throw new ValidationException("Field Email is not an email address"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs new file mode 100644 index 0000000..fe8ddaa --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/ComponentDataModel.cs @@ -0,0 +1,38 @@ +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class ComponentDataModel(string id, string componentName, + ComponentType componentType, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string ComponentName { get; private set; } = componentName; + + public ComponentType ComponentType { get; private set; } = componentType; + + public double Price { get; private set; } = price; + + 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 value in the field Id is not a unique identifier"); + + if (ComponentName.IsEmpty()) + throw new ValidationException("Field ComponentName is empty"); + + if (ComponentType == ComponentType.None) + throw new ValidationException("Field ComponentType is empty"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs new file mode 100644 index 0000000..6e1f24e --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/EmployeeDataModel.cs @@ -0,0 +1,47 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class EmployeeDataModel(string id, string fio, string postId, DateTime birthDate, DateTime employmentDate, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fio; + + 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 value in the field Id is not a unique identifier"); + + if (FIO.IsEmpty()) + throw new ValidationException("Field FIO is empty"); + + if (PostId.IsEmpty()) + throw new ValidationException("Field PostId is empty"); + + if (!PostId.IsGuid()) + throw new ValidationException("The value in the field PostId 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) + throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs new file mode 100644 index 0000000..baf7f7d --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationComponentDataModel.cs @@ -0,0 +1,33 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class InstallationComponentDataModel(string installationId, + string componentId, int count) : IValidation +{ + public string InstallationId { get; private set; } = installationId; + + public string ComponentId { get; private set; } = componentId; + + public int Count { get; private set; } = count; + + public void Validate() + { + if (InstallationId.IsEmpty()) + throw new ValidationException("Field InstallationId is empty"); + + if (!InstallationId.IsGuid()) + throw new ValidationException("The value in the field InstallationId is not a unique identifier"); + + if (ComponentId.IsEmpty()) + throw new ValidationException("Field ComponentId is empty"); + + if (!ComponentId.IsGuid()) + throw new ValidationException("The value in the field ComponentId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs new file mode 100644 index 0000000..9e39d32 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/InstallationDataModel.cs @@ -0,0 +1,55 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class InstallationDataModel(string id, string employeeId, string? clientId, + DateTime installationDate, double installationPrice, double sum, bool isCanceled, + List components) : IValidation +{ + public string Id { get; private set; } = id; + + public string EmployeeId { get; private set; } = employeeId; + + public string? ClientId { get; private set; } = clientId; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + + public DateTime InstallationDate { get; private set; } = installationDate; + + public double InstallationPrice { get; private set; } = installationPrice; + + public double Sum { get; private set; } = sum; + + public bool IsCanceled { get; private set; } = isCanceled; + + public List Components { get; private set; } = components; + + public void Validate() + { + if (Id.IsEmpty()) + throw new ValidationException("Field Id is empty"); + + if (!Id.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + + if (EmployeeId.IsEmpty()) + throw new ValidationException("Field EmployeeId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field EmployeeId is not a unique identifier"); + + if (!ClientId?.IsGuid() ?? !ClientId?.IsEmpty() ?? false) + throw new ValidationException("The value in the field ClientId is not a unique identifier"); + + if (InstallationPrice <= 0) + throw new ValidationException("Field InstallationPrice is less than or equal to 0"); + + if (Sum <= 0) + throw new ValidationException("Field Sum is less than or equal to 0"); + + if ((Components?.Count ?? 0) == 0) + throw new ValidationException("The sale must include components"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..6025af1 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/PostDataModel.cs @@ -0,0 +1,46 @@ +using TheCyclopsContracts.Enums; +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; + +namespace TheCyclopsContracts.DataModels; + +public class PostDataModel(string id, string PostId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) +{ + public string Id { get; private set; } = id; + + public string PostId { get; private set; } = PostId; + + public string PostName { get; private set; } = postName; + + public PostType PostType { get; private set; } = postType; + + public double Salary { get; private set; } = salary; + + public bool IsActual { get; private set; } = isActual; + + public DateTime ChangeDate { get; private set; } = changeDate; + + public void Validate() + { + if (Id.IsEmpty()) + throw new ValidationException("Field Id is empty"); + + if (!Id.IsGuid()) + throw new ValidationException("The value in the field Id is not a unique identifier"); + + if (PostId.IsEmpty()) + throw new ValidationException("Field PostId is empty"); + + if (!PostId.IsGuid()) + throw new ValidationException("The value in the field PostId is not a unique identifier"); + + if (PostName.IsEmpty()) + throw new ValidationException("Field PostName is empty"); + + if (PostType == PostType.None) + throw new ValidationException("Field PostType is empty"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is empty"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs b/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..d45c4c4 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,26 @@ +using TheCyclopsContracts.Exceptions; +using TheCyclopsContracts.Extensions; +using TheCyclopsContracts.Infrastructure; + +namespace TheCyclopsContracts.DataModels; + +public class SalaryDataModel(string employeeId, DateTime salaryDate, double employeeSalary) : IValidation +{ + public string EmployeeId { get; private set; } = employeeId; + + public DateTime SalaryDate { get; private set; } = salaryDate; + + public double Salary { get; private set; } = employeeSalary; + + public void Validate() + { + if (EmployeeId.IsEmpty()) + throw new ValidationException("Field EmployeeId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field EmployeeId is not a unique identifier"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is less than or equal to 0"); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs b/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs new file mode 100644 index 0000000..ca2cb65 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Enums/ComponentType.cs @@ -0,0 +1,11 @@ +namespace TheCyclopsContracts.Enums; + +public enum ComponentType +{ + None = 0, + Camera = 1, + Sensor = 2, + ContropPanel = 3, + Cable = 4, + Other = 5 +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs b/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs new file mode 100644 index 0000000..51e142a --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Enums/PostType.cs @@ -0,0 +1,10 @@ +namespace TheCyclopsContracts.Enums; + +public enum PostType +{ + None = 0, + Designer = 1, + Installer = 2, + Seller = 3, + Administrator = 4 +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..b601647 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Exceptions/ValidationException.cs @@ -0,0 +1,5 @@ +namespace TheCyclopsContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtension.cs b/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtension.cs new file mode 100644 index 0000000..5617a8f --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Extensions/StringExtension.cs @@ -0,0 +1,14 @@ +namespace TheCyclopsContracts.Extensions; + +public static class StringExtension +{ + public static bool IsEmpty(this string str) + { + return string.IsNullOrEmpty(str); + } + + public static bool IsGuid(this string str) + { + return Guid.TryParse(str, out _); + } +} diff --git a/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs b/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..ec8eb55 --- /dev/null +++ b/TheCyclopsProject/TheCyclopsContracts/Infrastructure/IValidation.cs @@ -0,0 +1,6 @@ +namespace TheCyclopsContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +}