From 0d2982c77029cb3bdcbebac99ca4b728613e9d24 Mon Sep 17 00:00:00 2001 From: I1nur Date: Sun, 2 Feb 2025 21:00:54 +0400 Subject: [PATCH] feature: add models --- .../DataModels/BuyerDataModel.cs | 41 ++++++++++++++ .../DataModels/PostDataModel.cs | 53 +++++++++++++++++++ .../DataModels/ProgramDataModel.cs | 50 +++++++++++++++++ .../DataModels/ProgramHistoryDataModel.cs | 32 +++++++++++ .../DataModels/SalaryDataModel.cs | 31 +++++++++++ .../DataModels/SaleDataModel.cs | 52 ++++++++++++++++++ .../DataModels/SaleProgramDataModel.cs | 37 +++++++++++++ .../DataModels/WorkerDataModel.cs | 47 ++++++++++++++++ .../DataModels/СompanyDataModel.cs | 31 +++++++++++ .../Enums/PostType.cs | 15 ++++++ .../Enums/ProgramType.cs | 17 ++++++ .../Exceptions/ValidatonException.cs | 11 ++++ .../Extensions/StringExceptions.cs | 20 +++++++ .../Infrastucture/IValidation.cs | 12 +++++ 14 files changed, 449 insertions(+) create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/BuyerDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/PostDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramHistoryDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SalaryDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleProgramDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/WorkerDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/СompanyDataModel.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/PostType.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/ProgramType.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/Exceptions/ValidatonException.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/Extensions/StringExceptions.cs create mode 100644 SoftwareInstallationContracts/SoftwareInstallationContracts/Infrastucture/IValidation.cs diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/BuyerDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..9ee1970 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/BuyerDataModel.cs @@ -0,0 +1,41 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class BuyerDataModel(string id, string fio, string phoneNumber) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fio; + + public string PhoneNumber { get; private set; } = phoneNumber; + + public void Validate() + { + if (id.isEmpty()) + throw new ValidatonException("Field Id is empty"); + + if (!id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (FIO.isEmpty()) + throw new ValidatonException("Field FIO is empty"); + + if (!Regex.IsMatch(FIO, @"/^([а-яa-zё\-]{2,})++([ ][а-яa-zё\-]{2,})++(?:[ ][а-яa-zё\-]{2,})?$")) + throw new ValidatonException("Field FIO is not a fio"); + + if (PhoneNumber.isEmpty()) + throw new ValidatonException("Field PhoneNumber is empty"); + + if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\- ]?)?[\d\- ]{7,10}$")) + throw new ValidatonException("Field PhoneNumber is not a phone number"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/PostDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..4b1ce94 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/PostDataModel.cs @@ -0,0 +1,53 @@ +using SoftwareInstallationContracts.Enums; +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class PostDataModel(string id, string postId, string postName, PostType postType, int servicesCount, 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 ValidatonException("Field Id is empty"); + + if (!Id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (PostId.isEmpty()) + throw new ValidatonException("Field PostId is empty"); + + if (!PostId.isGuid()) + throw new ValidatonException("The value in the field PostId is not a unique identifier"); + + if (Salary <= 0) + throw new ValidatonException("Field Salary is empty"); + + if (PostType == PostType.None) + throw new ValidatonException("Field PostType is empty"); + + if (PostName.isEmpty()) + throw new ValidatonException("Field PostName is empty"); + } + +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramDataModel.cs new file mode 100644 index 0000000..7da7eae --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramDataModel.cs @@ -0,0 +1,50 @@ +using SoftwareInstallationContracts.Enums; +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class ProgramDataModel(string id, string programName, ProgramType programType, string companyId, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + + public string ProgramName { get; private set; } = programName; + + public ProgramType ProgramType { get; private set; } = programType; + + public string CompanyId { get; private set; } = companyId; + + public double Price { get; private set; } = price; + + public bool IsDeleted { get; private set; } = isDeleted; + + public void Validate() + { + if (Id.isEmpty()) + throw new ValidatonException("Field Id is empty"); + + if (!Id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (ProgramName.isEmpty()) + throw new ValidatonException("Field ProgramName is empty"); + + if (ProgramType == ProgramType.None) + throw new ValidatonException("Field ProgramType is empty"); + + if (CompanyId.isEmpty()) + throw new ValidatonException("Field CompanyId is empty"); + + if (!CompanyId.isGuid()) + throw new ValidatonException("The value in the field CompanyId is not a unique identifier"); + + if (Price <= 0) + throw new ValidatonException("Field Price is less than or equal to 0"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramHistoryDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramHistoryDataModel.cs new file mode 100644 index 0000000..dfdaf44 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/ProgramHistoryDataModel.cs @@ -0,0 +1,32 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class ProgramHistoryDataModel(string programId, double oldPrice) : IValidation +{ + public string ProgramId { get; private set; } = programId; + + public double OldPrice { get; private set; } = oldPrice; + + public DateTime ChangeUpdate { get; private set; } = DateTime.UtcNow; + + public void Validate() + { + if (ProgramId.isEmpty()) + throw new ValidatonException("Field ProgramId is empty"); + + if (!ProgramId.isGuid()) + throw new ValidatonException("The value in the field ProgramId is not a unique identifier"); + + if (OldPrice <= 0) + throw new ValidatonException("Field OldPrice is less than or equal to 0"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SalaryDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..9151e7d --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,31 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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 ValidatonException("Field WorkerId is empty"); + + if (!WorkerId.isGuid()) + throw new ValidatonException("The value in the field WorkerId is not a unique identifier"); + + if (Salary <= 0) + throw new ValidatonException("Field Salary is less than or equal to 0"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..2da26a1 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleDataModel.cs @@ -0,0 +1,52 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class SaleDataModel(string id, string workerId, string buyerId, double sum, bool isCancel, List programs) : IValidation +{ + public string Id { get; private set; } = id; + + public string WorkerId { get; private set; } = workerId; + + public string? BuyerId { get; private set; } = buyerId; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + + public double Sum { get; private set; } = sum; + + public bool IsCancel { get; private set; } = isCancel; + + public List Programs { get; private set; } = programs; + + public void Validate() + { + if (Id.isEmpty()) + throw new ValidatonException("Field Id is empty"); + + if (!Id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (WorkerId.isEmpty()) + throw new ValidatonException("Field WorkerId is empty"); + + if (!WorkerId.isGuid()) + throw new ValidatonException("The value in the field WorkerId is not a unique identifier"); + + if (!BuyerId?.isGuid() ?? !BuyerId?.isEmpty() ?? false) + throw new ValidatonException("The value in the field BuyerId is not a unique identifier"); + + if (Sum <= 0) + throw new ValidatonException("Field Sum is less than or wqual to 0"); + + if ((Programs?.Count() ?? 0) == 0) + throw new ValidatonException("The sale must include programs"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleProgramDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleProgramDataModel.cs new file mode 100644 index 0000000..5ac2e53 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/SaleProgramDataModel.cs @@ -0,0 +1,37 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class SaleProgramDataModel(string programId, string saleId, int count) : IValidation +{ + public string ProgramId { get; private set; } = programId; + + public string SaleId { get; private set; } = saleId; + + public int Count { get; private set; } = count; + + public void Validate() + { + if (ProgramId.isEmpty()) + throw new ValidatonException("Field ProgramId is empty"); + + if (!ProgramId.isGuid()) + throw new ValidatonException("The value in the field ProgramId is not a unique identifier"); + + if (SaleId.isEmpty()) + throw new ValidatonException("Field SaleId is empty"); + + if (!SaleId.isGuid()) + throw new ValidatonException("The value in the field SaleId is not a unique identifier"); + + if (Count <= 0) + throw new ValidatonException("Field Count is less than or equal to 0"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/WorkerDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..4a5a104 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,47 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.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 ValidatonException("Field Id is empty"); + + if (!Id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (FIO.isEmpty()) + throw new ValidatonException("Field FIO is empty"); + + if (PostId.isEmpty()) + throw new ValidatonException("Field PostId is empty"); + + if (!PostId.isGuid()) + throw new ValidatonException("The value in the field PostId is not a unique identifier"); + + if (BirthDate.Date > DateTime.Now.AddYears(-16).Date) + throw new ValidatonException($"Minors cannot be hired (BirthDate = {BirthDate.ToShortDateString()}"); + + if (EmploymentDate.Date < BirthDate.Date) + throw new ValidatonException("The date of employment cannot be less than the date of birth"); + + if ((EmploymentDate - BirthDate).TotalDays / 365 < 16) + throw new ValidatonException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()}"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/СompanyDataModel.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/СompanyDataModel.cs new file mode 100644 index 0000000..abe0bd7 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/DataModels/СompanyDataModel.cs @@ -0,0 +1,31 @@ +using SoftwareInstallationContracts.Exceptions; +using SoftwareInstallationContracts.Extensions; +using SoftwareInstallationContracts.Infrastucture; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.DataModels; + +public class СompanyDataModel(string id, string companyName, string prevCompanyName, string prevPrevCompanyName) : IValidation +{ + public string Id { get; private set; } = id; + public string СompanyName { get; private set; } = companyName; + public string? PrevCompanyName { get; private set; } = prevCompanyName; + public string? PrevPrevCompanyName { get; private set; } = prevPrevCompanyName; + public void Validate() + { + if (Id.isEmpty()) + throw new ValidatonException("Field Id is empty"); + + if (!Id.isGuid()) + throw new ValidatonException("The value in the field Id is not a unique identifier"); + + if (СompanyName.isEmpty()) + throw new ValidatonException("Field СompanyName is empty"); + } +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/PostType.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/PostType.cs new file mode 100644 index 0000000..d38e1df --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.Enums; + +public enum PostType +{ + None = 0, + Supervisor = 1, + Master = 2, + Stuff = 3 +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/ProgramType.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/ProgramType.cs new file mode 100644 index 0000000..babd83f --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/Enums/ProgramType.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.Enums; + +public enum ProgramType +{ + None = 0, + SystemSoftware = 1, + ApplicationSoftware = 2, + UtilitySoftware = 3, + EnterpriseSoftware = 4, + CloudServices = 5 +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/Exceptions/ValidatonException.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/Exceptions/ValidatonException.cs new file mode 100644 index 0000000..1f335ab --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/Exceptions/ValidatonException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.Exceptions; + +public class ValidatonException(String message) : Exception(message) +{ +} diff --git a/SoftwareInstallationContracts/SoftwareInstallationContracts/Extensions/StringExceptions.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/Extensions/StringExceptions.cs new file mode 100644 index 0000000..2234183 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/Extensions/StringExceptions.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.Extensions; + +public static class StringExceptions +{ + 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/SoftwareInstallationContracts/SoftwareInstallationContracts/Infrastucture/IValidation.cs b/SoftwareInstallationContracts/SoftwareInstallationContracts/Infrastucture/IValidation.cs new file mode 100644 index 0000000..c42f0c6 --- /dev/null +++ b/SoftwareInstallationContracts/SoftwareInstallationContracts/Infrastucture/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.Infrastucture; + +public interface IValidation +{ + void Validate(); +}