From 15a16dc06784bef6070faac4e6e2a6718e825c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AC=20=D0=AA?= Date: Thu, 13 Feb 2025 00:38:49 +0400 Subject: [PATCH] =?UTF-8?q?1=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/EmployeeDataModel.cs | 54 +++++++++++++++++++ .../DataModels/HistoryDataModel.cs | 30 +++++++++++ .../DataModels/Meat_productDataModel.cs | 40 ++++++++++++++ .../HamContract/DataModels/PostDataModel.cs | 46 ++++++++++++++++ .../HamContract/DataModels/SalaryDataModel.cs | 29 ++++++++++ .../HamContract/DataModels/SaleDataModel.cs | 41 ++++++++++++++ .../DataModels/SaleProductDataModel.cs | 35 ++++++++++++ HamContract/HamContract/Enums/PostType.cs | 16 ++++++ HamContract/HamContract/Enums/ProductType.cs | 18 +++++++ .../Exceptions/ValidationException.cs | 9 ++++ .../Extensions/StringExtensions.cs | 19 +++++++ .../HamContract/Infrastructure/IValidation.cs | 12 +++++ 12 files changed, 349 insertions(+) create mode 100644 HamContract/HamContract/DataModels/EmployeeDataModel.cs create mode 100644 HamContract/HamContract/DataModels/HistoryDataModel.cs create mode 100644 HamContract/HamContract/DataModels/Meat_productDataModel.cs create mode 100644 HamContract/HamContract/DataModels/PostDataModel.cs create mode 100644 HamContract/HamContract/DataModels/SalaryDataModel.cs create mode 100644 HamContract/HamContract/DataModels/SaleDataModel.cs create mode 100644 HamContract/HamContract/DataModels/SaleProductDataModel.cs create mode 100644 HamContract/HamContract/Enums/PostType.cs create mode 100644 HamContract/HamContract/Enums/ProductType.cs create mode 100644 HamContract/HamContract/Exceptions/ValidationException.cs create mode 100644 HamContract/HamContract/Extensions/StringExtensions.cs create mode 100644 HamContract/HamContract/Infrastructure/IValidation.cs diff --git a/HamContract/HamContract/DataModels/EmployeeDataModel.cs b/HamContract/HamContract/DataModels/EmployeeDataModel.cs new file mode 100644 index 0000000..30535fb --- /dev/null +++ b/HamContract/HamContract/DataModels/EmployeeDataModel.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels; + +public class EmployeeDataModel(string id, string name, string phone, string postId, DateTime Dateofbirth, DateTime employmentDate, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + public string Name { get; private set; } = name; + public string Phone { get; private set; } = phone; + public string PostId { get; private set; } = postId; + public DateTime Dateofbirth { get; private set; } = Dateofbirth; + 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 (Name.IsEmpty()) + throw new ValidationException("Field Name is empty"); + + if (Phone.IsEmpty()) { throw new ValidationException("Field PhoneNumber is empty"); } + + if (!Regex.IsMatch(Phone, @"^\+7[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{2}[\s-]?\d{2}$")) { throw new ValidationException("Field PhoneNumber is not a phone number"); } + + 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 (Dateofbirth.Date > DateTime.Now.AddYears(-18).Date) + throw new ValidationException($"Minors cannot be hired (BirthDate = {Dateofbirth.ToShortDateString()})"); + + if (EmploymentDate.Date < Dateofbirth.Date) + throw new ValidationException("The date of employment cannot be less than the date of birth"); + + if ((EmploymentDate - Dateofbirth).TotalDays / 365 < 16) // EmploymentDate.Year - BirthDate.Year + throw new ValidationException($"Minors cannot be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - BirthDate.ToShortDateString())"); + } +} + diff --git a/HamContract/HamContract/DataModels/HistoryDataModel.cs b/HamContract/HamContract/DataModels/HistoryDataModel.cs new file mode 100644 index 0000000..0519ea8 --- /dev/null +++ b/HamContract/HamContract/DataModels/HistoryDataModel.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels; + +public class HistoryDataModel(string productId, double oldPrice) : IValidation +{ + public string ProductId { get; private set; } = productId; + public double OldPrice { get; private set; } = oldPrice; + public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; + + public void Validate() + { + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + if (OldPrice <= 0) + throw new ValidationException("Field OldPrice is less than or equal to 0"); + } +} + diff --git a/HamContract/HamContract/DataModels/Meat_productDataModel.cs b/HamContract/HamContract/DataModels/Meat_productDataModel.cs new file mode 100644 index 0000000..a616637 --- /dev/null +++ b/HamContract/HamContract/DataModels/Meat_productDataModel.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Enums; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels +{ + public class ProductDataModel(string id, string productName, ProductType productType, double price, bool isDeleted) : IValidation + + { + public string Id { get; private set; } = id; + public string ProductName { get; private set; } = productName; + public ProductType ProductType { get; private set; } = productType; + 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 (ProductName.IsEmpty()) + throw new ValidationException("Field ProductName is empty"); + + if (ProductType == ProductType.None) + throw new ValidationException("Field ProductType is empty"); + + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + } + } +} \ No newline at end of file diff --git a/HamContract/HamContract/DataModels/PostDataModel.cs b/HamContract/HamContract/DataModels/PostDataModel.cs new file mode 100644 index 0000000..b961b46 --- /dev/null +++ b/HamContract/HamContract/DataModels/PostDataModel.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Enums; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.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/HamContract/HamContract/DataModels/SalaryDataModel.cs b/HamContract/HamContract/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..e1ee540 --- /dev/null +++ b/HamContract/HamContract/DataModels/SalaryDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels; + +public class SalaryDataModel(string employeeId, DateTime Date, double employeeSalary) : IValidation +{ + public string EmployeeId { get; private set; } = employeeId; + public DateTime Date { get; private set; } = Date; + 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/HamContract/HamContract/DataModels/SaleDataModel.cs b/HamContract/HamContract/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..e3e4191 --- /dev/null +++ b/HamContract/HamContract/DataModels/SaleDataModel.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels; + +public class SaleDataModel(string id, string employeeId, double sum, bool isCancel, List products) : IValidation +{ + public string Id { get; private set; } = id; + public string EmployeeId { get; private set; } = employeeId; + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + public double Sum { get; private set; } = sum; + public bool IsCancel { get; private set; } = isCancel; + public List Products { get; private set; } = products; + + 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 WorkerId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field WorkerId is not a unique identifier"); + + if (Sum <= 0) + throw new ValidationException("Field Sum is less than or equal to 0"); + + if ((Products?.Count ?? 0) == 0) + throw new ValidationException("The sale must include products"); + } +} diff --git a/HamContract/HamContract/DataModels/SaleProductDataModel.cs b/HamContract/HamContract/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..806698d --- /dev/null +++ b/HamContract/HamContract/DataModels/SaleProductDataModel.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HamContract.Extensions; +using HamContract.Infrastructure; + +namespace HamContract.DataModels; + +public class SaleProductDataModel(string saleId, string productId, int count) : IValidation +{ + public string SaleId { get; private set; } = saleId; + public string ProductId { get; private set; } = productId; + public int Count { get; private set; } = count; + public void Validate() + { + if (SaleId.IsEmpty()) + throw new ValidationException("Field SaleId is empty"); + + if (!SaleId.IsGuid()) + throw new ValidationException("The value in the field SaleId is not a unique identifier"); + + if (ProductId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!ProductId.IsGuid()) + throw new ValidationException("The value in the field ProductId is not a unique identifier"); + + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } + +} diff --git a/HamContract/HamContract/Enums/PostType.cs b/HamContract/HamContract/Enums/PostType.cs new file mode 100644 index 0000000..742c111 --- /dev/null +++ b/HamContract/HamContract/Enums/PostType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HamContract.Enums +{ + public enum PostType + { None=0, + Butcher=1, + Seller=2, + Cleaner=3 + + } +} diff --git a/HamContract/HamContract/Enums/ProductType.cs b/HamContract/HamContract/Enums/ProductType.cs new file mode 100644 index 0000000..28ebc0d --- /dev/null +++ b/HamContract/HamContract/Enums/ProductType.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HamContract.Enums +{ + public enum ProductType + { + None=0, + Animal_Meat = 1, + Various_meat_products =2, + Semi_finished_products=3 + + + } +} diff --git a/HamContract/HamContract/Exceptions/ValidationException.cs b/HamContract/HamContract/Exceptions/ValidationException.cs new file mode 100644 index 0000000..553574f --- /dev/null +++ b/HamContract/HamContract/Exceptions/ValidationException.cs @@ -0,0 +1,9 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +namespace HamContract.Exceptions; +public class ValidationExceptionvoid(string message) : Exception(message) +{ +} diff --git a/HamContract/HamContract/Extensions/StringExtensions.cs b/HamContract/HamContract/Extensions/StringExtensions.cs new file mode 100644 index 0000000..3efe16b --- /dev/null +++ b/HamContract/HamContract/Extensions/StringExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HamContract.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/HamContract/HamContract/Infrastructure/IValidation.cs b/HamContract/HamContract/Infrastructure/IValidation.cs new file mode 100644 index 0000000..cd01d3e --- /dev/null +++ b/HamContract/HamContract/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HamContract.Infrastructure; + +public interface IValidation +{ + void Validate(); +}