From 99cb8dc1c68e5fed8b7303cc4c09f62930537543 Mon Sep 17 00:00:00 2001 From: cyxaruk Date: Sun, 9 Feb 2025 13:49:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BC=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 --- .../SPiluSZharu/DataModels/PostDataModel.cs | 52 +++++++++++++++++++ .../DataModels/ProductDataModel.cs | 39 ++++++++++++++ .../DataModels/ProductHistoryDataModel.cs | 27 ++++++++++ .../DataModels/RestaurantDataModel.cs | 27 ++++++++++ .../SPiluSZharu/DataModels/SaleDataModel.cs | 51 ++++++++++++++++++ .../DataModels/SaleProductDataModel.cs | 37 +++++++++++++ .../SPiluSZharu/DataModels/WorkerDataModel.cs | 52 +++++++++++++++++++ SPiluSZharu/SPiluSZharu/Enums/PostType.cs | 15 ++++++ SPiluSZharu/SPiluSZharu/Enums/ProductType.cs | 15 ++++++ .../Exceptions/ValidationException.cs | 4 ++ .../Extensions/StringExtensions.cs | 20 +++++++ .../SPiluSZharu/Infrastructure/IValidation.cs | 12 +++++ .../SPiluSZharu/SPiluSZharuContracts.csproj | 9 ++++ SPiluSZharu/SPiluSZharuProject.sln | 25 +++++++++ 14 files changed, 385 insertions(+) create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/PostDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/ProductDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/ProductHistoryDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/RestaurantDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/SaleDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/SaleProductDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/DataModels/WorkerDataModel.cs create mode 100644 SPiluSZharu/SPiluSZharu/Enums/PostType.cs create mode 100644 SPiluSZharu/SPiluSZharu/Enums/ProductType.cs create mode 100644 SPiluSZharu/SPiluSZharu/Exceptions/ValidationException.cs create mode 100644 SPiluSZharu/SPiluSZharu/Extensions/StringExtensions.cs create mode 100644 SPiluSZharu/SPiluSZharu/Infrastructure/IValidation.cs create mode 100644 SPiluSZharu/SPiluSZharu/SPiluSZharuContracts.csproj create mode 100644 SPiluSZharu/SPiluSZharuProject.sln diff --git a/SPiluSZharu/SPiluSZharu/DataModels/PostDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/PostDataModel.cs new file mode 100644 index 0000000..035dbdd --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/PostDataModel.cs @@ -0,0 +1,52 @@ +using SPiluSZharuContracts.Enums; +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.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/SPiluSZharu/SPiluSZharu/DataModels/ProductDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/ProductDataModel.cs new file mode 100644 index 0000000..588ce29 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/ProductDataModel.cs @@ -0,0 +1,39 @@ +using SPiluSZharuContracts.Enums; +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; + + +namespace SPiluSZharuContracts.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"); + + } +} diff --git a/SPiluSZharu/SPiluSZharu/DataModels/ProductHistoryDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/ProductHistoryDataModel.cs new file mode 100644 index 0000000..83f6751 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/ProductHistoryDataModel.cs @@ -0,0 +1,27 @@ +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; + + +namespace SPiluSZharuContracts.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/SPiluSZharu/SPiluSZharu/DataModels/RestaurantDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/RestaurantDataModel.cs new file mode 100644 index 0000000..ebf6366 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/RestaurantDataModel.cs @@ -0,0 +1,27 @@ +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; + + +namespace SPiluSZharuContracts.DataModels; + +public class RestaurantDataModel(string id, string restaurantName, string prevRestaurantName, string prevPrevRestaurantName) : IValidation +{ + public string Id { get; private set; } = id; + + public string RestaurantName { get; private set; } = restaurantName; + + public string PrevRestaurantName { get; private set; } = prevRestaurantName; + + public string PrevPrevRestaurantName { get; private set; } = prevPrevRestaurantName; + + public void Validate() + { + if (Id.IsEmpty()) + throw new ValidationException("Field Id is empty"); + if (!Id.IsGuid()) + throw new ValidationException("Field Id is not a unique identifier"); + if (RestaurantName.IsEmpty()) + throw new ValidationException("Field RestaurantName is empty"); + } +} diff --git a/SPiluSZharu/SPiluSZharu/DataModels/SaleDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..5bb28e3 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/SaleDataModel.cs @@ -0,0 +1,51 @@ +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.DataModels; + +public class SaleDataModel(string id, string workerId, string? restaurantId, double sum, bool isCancel, List products) : IValidation +{ + public string Id { get; private set; } = id; + + public string WorkerId { get; private set; } = workerId; + + public string? RestaurantId { get; private set; } = restaurantId; + + 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 (!RestaurantId?.IsGuid() ?? !RestaurantId?.IsEmpty() ?? false) + throw new ValidationException("The value in the field RestaurantId 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/SPiluSZharu/SPiluSZharu/DataModels/SaleProductDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/SaleProductDataModel.cs new file mode 100644 index 0000000..6a20824 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/SaleProductDataModel.cs @@ -0,0 +1,37 @@ +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.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/SPiluSZharu/SPiluSZharu/DataModels/WorkerDataModel.cs b/SPiluSZharu/SPiluSZharu/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..cf7d468 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/DataModels/WorkerDataModel.cs @@ -0,0 +1,52 @@ +using SPiluSZharuContracts.Exceptions; +using SPiluSZharuContracts.Extensions; +using SPiluSZharuContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.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/SPiluSZharu/SPiluSZharu/Enums/PostType.cs b/SPiluSZharu/SPiluSZharu/Enums/PostType.cs new file mode 100644 index 0000000..08d1484 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.Enums; + +public enum PostType +{ + None = 0, + Operator = 1, + Aggreagator = 2, + DeliveryMan = 3 +} diff --git a/SPiluSZharu/SPiluSZharu/Enums/ProductType.cs b/SPiluSZharu/SPiluSZharu/Enums/ProductType.cs new file mode 100644 index 0000000..99447e0 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/Enums/ProductType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.Enums; + +public enum ProductType +{ + None = 0, + Pizza = 1, + Burgers = 2, + Drinks = 3 +} diff --git a/SPiluSZharu/SPiluSZharu/Exceptions/ValidationException.cs b/SPiluSZharu/SPiluSZharu/Exceptions/ValidationException.cs new file mode 100644 index 0000000..969a033 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/Exceptions/ValidationException.cs @@ -0,0 +1,4 @@ +namespace SPiluSZharuContracts.Exceptions; +public class ValidationException(string message) : Exception(message) +{ +} \ No newline at end of file diff --git a/SPiluSZharu/SPiluSZharu/Extensions/StringExtensions.cs b/SPiluSZharu/SPiluSZharu/Extensions/StringExtensions.cs new file mode 100644 index 0000000..d0c2c53 --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/Extensions/StringExtensions.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.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/SPiluSZharu/SPiluSZharu/Infrastructure/IValidation.cs b/SPiluSZharu/SPiluSZharu/Infrastructure/IValidation.cs new file mode 100644 index 0000000..6ae55aa --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SPiluSZharuContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +} diff --git a/SPiluSZharu/SPiluSZharu/SPiluSZharuContracts.csproj b/SPiluSZharu/SPiluSZharu/SPiluSZharuContracts.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/SPiluSZharu/SPiluSZharu/SPiluSZharuContracts.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/SPiluSZharu/SPiluSZharuProject.sln b/SPiluSZharu/SPiluSZharuProject.sln new file mode 100644 index 0000000..411c640 --- /dev/null +++ b/SPiluSZharu/SPiluSZharuProject.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35013.160 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SPiluSZharuContracts", "SPiluSZharu\SPiluSZharuContracts.csproj", "{80D721F8-2CC5-422B-A06B-42A00984BDD5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {80D721F8-2CC5-422B-A06B-42A00984BDD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {80D721F8-2CC5-422B-A06B-42A00984BDD5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {80D721F8-2CC5-422B-A06B-42A00984BDD5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {80D721F8-2CC5-422B-A06B-42A00984BDD5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9B711281-34A4-4ACE-8CA0-636574EA0BF2} + EndGlobalSection +EndGlobal