From b26ff1e7bb1a735cdde15606a0edb945882873d4 Mon Sep 17 00:00:00 2001 From: Kudyaeva Date: Thu, 6 Feb 2025 22:37:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 42 ++++++++++++++++ .../DataModels/PostDataModel.cs | 48 ++++++++++++++++++ .../DataModels/ProductDataModel.cs | 40 +++++++++++++++ .../DataModels/ProductHistoryDataModel.cs | 29 +++++++++++ .../DataModels/SalaryDataModel.cs | 29 +++++++++++ .../DataModels/SaleDataModel.cs | 46 +++++++++++++++++ .../DataModels/SaleProductDataModel.cs | 34 +++++++++++++ .../DataModels/WorkerDataModel.cs | 49 +++++++++++++++++++ .../TheButcherShopContracts/Enums/PostType.cs | 16 ++++++ .../Enums/ProductType.cs | 16 ++++++ .../Exceptions/ValidationException.cs | 11 +++++ .../Extensions/StringExtensions.cs | 21 ++++++++ .../Infrastructure/IValidation.cs | 12 +++++ 13 files changed, 393 insertions(+) create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/BuyerDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs create mode 100644 TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/BuyerDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..94af1df --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/BuyerDataModel.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; +using TheButcherShopContracts.Exceptions; + + +namespace TheButcherShopContracts.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 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 (PhoneNumber.IsEmpty()) + throw new ValidationException("Field PhoneNumber is empty"); + + if (!Regex.IsMatch(PhoneNumber, @"^((8|\+7)[\- ]?)?(\(?\d{3}\)?[\-]?)?[\d\- ]{7,10}$")) + throw new ValidationException("Field PhoneNumber is not a phone number"); + + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..a01c419 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/PostDataModel.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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"); + } +} + diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..bfd2b8e --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductDataModel.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Enums; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +internal 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"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..72e9aa2 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +public class ProductHistoryDataModel(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/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..8cdb4e0 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..4eab3c4 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleDataModel.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.DataModels; + +public class SaleDataModell(string id, string workerId, string? buyerId, double sum, bool isCancel, List products) : 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 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 (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 (!BuyerId?.IsGuid() ?? !BuyerId?.IsEmpty() ?? false) + throw new ValidationException("The value in the field BuyerId 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/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..fd1c066 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/SaleProductDataModel.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs b/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..a97577a --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using TheButcherShopContracts.Exceptions; +using TheButcherShopContracts.Extensions; +using TheButcherShopContracts.Infrastructure; +using static System.Runtime.InteropServices.JavaScript.JSType; + +namespace TheButcherShopContracts.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())"); + } +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs b/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs new file mode 100644 index 0000000..46cfa1f --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Enums/PostType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Enums; + +public enum PostType +{ + None = 0, + Butcher = 1 , // Мясник (занимается разделкой мяса) + Packer = 2, // Фасовщик (занимается фасовкой товаров) + Salesperson = 3, // Продавец (занимается продажей товаров) + Administrator = 4, // Администратор (административная работа) +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs b/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs new file mode 100644 index 0000000..5791d77 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Enums/ProductType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Enums; + +public enum ProductType +{ + None = 0, + FreshMeat = 1, // Свежее мясо + SemiFinished = 2, // Полуфабрикаты + SausagesAndSmoked = 3, // Колбасные изделия и копчености + OffalAndDelicacies = 4 // Субпродукты и деликатесы +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs b/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..51db8cd --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs b/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..b5547b8 --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Extensions/StringExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.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/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs b/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..e76964f --- /dev/null +++ b/TheButcherShopContracts/TheButcherShopContracts/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TheButcherShopContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +}