diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/CustomerDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/CustomerDataModel.cs new file mode 100644 index 0000000..0087a85 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/CustomerDataModel.cs @@ -0,0 +1,33 @@ +using System.Text.RegularExpressions; +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class CustomerDataModel(string id, string name, string phoneNumber) : IValidation +{ + public string Id { get; private set; } = id; + + public string Name { get; private set; } = name; + + public string PhoneNumber { get; private set; } = phoneNumber; + + 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 (Name.IsEmpty()) + throw new ValidationException("Field Name is empty"); + + if (PhoneNumber.IsEmpty()) + throw new ValidationException("Field PhoneNumber is empty"); + + if (!Regex.IsMatch(PhoneNumber, @"^((\+7|7|8)+([0-9]){10})$")) + throw new ValidationException("Field PhoneNumber is not a phone number"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PostDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..47c9608 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PostDataModel.cs @@ -0,0 +1,47 @@ +using TwoFromTheCaseContracts.Enums; +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class PostDataModel(string id, string postId, string postName, PostType postType, double salary, bool isActual, DateTime changeDate) : IValidation +{ + 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"); + } +} \ No newline at end of file diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PremisesDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PremisesDataModel.cs new file mode 100644 index 0000000..20406bd --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/PremisesDataModel.cs @@ -0,0 +1,40 @@ +using TwoFromTheCaseContracts.Enums; +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class PremisesDataModel(string id, string premisesName, PremisesType premisesType, string customerId, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string PremisesName { get; private set; } = premisesName; + + public PremisesType PremisesType { get; private set; } = premisesType; + + public string CustomerId { get; private set; } = customerId; + + 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 (PremisesName.IsEmpty()) + throw new ValidationException("Field PremisesName is empty"); + + if (PremisesType == PremisesType.None) + throw new ValidationException("Field PremisesType is empty"); + + if (CustomerId.IsEmpty()) + throw new ValidationException("Field CustomerId is empty"); + + if (!CustomerId.IsGuid()) + throw new ValidationException("The value in the field CustomerId is not a unique identifier"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/SalaryDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..f8ff497 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,26 @@ +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class SalaryDataModel(string workerId, DateTime salaryDate, double workerSalary) : IValidation +{ + public string WorkerId { get; private set; } = workerId; + + public DateTime SalaryDate { get; private set; } = salaryDate; + + public double Salary { get; private set; } = workerSalary; + + public void Validate() + { + if (WorkerId.IsEmpty()) + throw new ValidationException("Field WorkerId is empty"); + + if (!WorkerId.IsGuid()) + throw new ValidationException("The value in the field WorkerId is not a unique identifier"); + + if (Salary <= 0) + throw new ValidationException("Field Salary is less than or equal to 0"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkDataModel.cs new file mode 100644 index 0000000..710814c --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkDataModel.cs @@ -0,0 +1,41 @@ +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class WorkDataModel(string id, string premisesId, double price, bool isCancel, List workers) : IValidation +{ + public string Id { get; private set; } = id; + + public string PremisesId { get; private set; } = premisesId; + + public double Price { get; private set; } = price; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + + public bool IsCancel { get; private set; } = isCancel; + + public List Workers { get; private set; } = workers; + + 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 (PremisesId.IsEmpty()) + throw new ValidationException("Field PremisesId is empty"); + + if (!PremisesId.IsGuid()) + throw new ValidationException("The value in the field PremisesId is not a unique identifier"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + + if ((Workers?.Count ?? 0) == 0) + throw new ValidationException("The sale must include workers"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkHistoryDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkHistoryDataModel.cs new file mode 100644 index 0000000..d2c2f89 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkHistoryDataModel.cs @@ -0,0 +1,26 @@ +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class WorkHistoryDataModel(string WorkId, double oldPrice) : IValidation +{ + public string WorkId { get; private set; } = WorkId; + + public double OldPrice { get; private set; } = oldPrice; + + public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; + + public void Validate() + { + if (WorkId.IsEmpty()) + throw new ValidationException("Field WorkId is empty"); + + if (!WorkId.IsGuid()) + throw new ValidationException("The value in the field WorkId is not a unique identifier"); + + if (OldPrice <= 0) + throw new ValidationException("Field OldPrice is less than or equal to 0"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..221a63f --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,47 @@ +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class WorkerDataModel(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) // EmploymentDate.Year - BirthDate.Year + throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); + } +} \ No newline at end of file diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerWorkDataModel.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerWorkDataModel.cs new file mode 100644 index 0000000..2ffeda3 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/DataModels/WorkerWorkDataModel.cs @@ -0,0 +1,27 @@ +using TwoFromTheCaseContracts.Exceptions; +using TwoFromTheCaseContracts.Extensions; +using TwoFromTheCaseContracts.Infrastructure; + +namespace TwoFromTheCaseContracts.DataModels; + +public class WorkerWorkDataModel(string workerId, string workId) : IValidation +{ + public string WorkerId { get; private set; } = workerId; + + public string WorkId { get; private set; } = workId; + + public void Validate() + { + if (WorkerId.IsEmpty()) + throw new ValidationException("Field WorkerId is empty"); + + if (!WorkerId.IsGuid()) + throw new ValidationException("The value in the field WorkerId is not a unique identifier"); + + if (WorkId.IsEmpty()) + throw new ValidationException("Field WorkId is empty"); + + if (!WorkId.IsGuid()) + throw new ValidationException("The value in the field WorkId is not a unique identifier"); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PostType.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PostType.cs new file mode 100644 index 0000000..3596db4 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PostType.cs @@ -0,0 +1,11 @@ +namespace TwoFromTheCaseContracts.Enums; + +public enum PostType +{ + None = 0, + Plasterer = 1, + Painter = 2, + Carpenter = 3, + Plumber = 4, + Electrician = 5 +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PremisesType.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PremisesType.cs new file mode 100644 index 0000000..09bf298 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Enums/PremisesType.cs @@ -0,0 +1,8 @@ +namespace TwoFromTheCaseContracts.Enums; + +public enum PremisesType +{ + None = 0, + Residential = 1, + Commercial = 2 +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Exceptions/ValidationException.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..a38dbda --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Exceptions/ValidationException.cs @@ -0,0 +1,5 @@ +namespace TwoFromTheCaseContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Extensions/StringExtensions.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..37b0218 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Extensions/StringExtensions.cs @@ -0,0 +1,14 @@ +namespace TwoFromTheCaseContracts.Extensions; + +public static class StringExtensions +{ + public static bool IsEmpty(this string str) + { + return string.IsNullOrWhiteSpace(str); + } + + public static bool IsGuid(this string str) + { + return Guid.TryParse(str, out _); + } +} diff --git a/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Infrastructure/IValidation.cs b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..209b796 --- /dev/null +++ b/TwoFromTheCaseContracts/TwoFromTheCaseContracts/Infrastructure/IValidation.cs @@ -0,0 +1,6 @@ +namespace TwoFromTheCaseContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +}