From 2a7bf250762d7a5917130bd0e1324fcc62fceb9c Mon Sep 17 00:00:00 2001 From: ShuryginDima Date: Wed, 5 Feb 2025 22:40:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataModels/BuyerDataModel.cs | 34 +++++++++++++++ .../DataModels/DishDataModel.cs | 34 +++++++++++++++ .../DataModels/DishHistoryDataModel.cs | 26 ++++++++++++ .../DataModels/OrderDataModel.cs | 39 +++++++++++++++++ .../DataModels/OrderDishDataModel.cs | 31 ++++++++++++++ .../DataModels/PostDataModel.cs | 41 ++++++++++++++++++ .../DataModels/SalaryDataModel.cs | 27 ++++++++++++ .../DataModels/WorkerDataModel.cs | 42 +++++++++++++++++++ .../AndDietCokeContracts/Enums/DishType.cs | 15 +++++++ .../AndDietCokeContracts/Enums/PostType.cs | 15 +++++++ .../Exceptions/ValidationException.cs | 11 +++++ .../Extensions/StringExtensions.cs | 19 +++++++++ .../Infrastructure/IValidation.cs | 12 ++++++ 13 files changed, 346 insertions(+) create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/BuyerDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/DishDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/DishHistoryDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDishDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/PostDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/SalaryDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/DataModels/WorkerDataModel.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/Enums/DishType.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/Enums/PostType.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/Exceptions/ValidationException.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/Extensions/StringExtensions.cs create mode 100644 AndDietCokeProject/AndDietCokeContracts/Infrastructure/IValidation.cs diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/BuyerDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/BuyerDataModel.cs new file mode 100644 index 0000000..24ab045 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/BuyerDataModel.cs @@ -0,0 +1,34 @@ +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Xml; +using AndDietCokeContracts.Exceptions; + +namespace AndDietCokeContracts.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"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/DishDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/DishDataModel.cs new file mode 100644 index 0000000..378e7ec --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/DishDataModel.cs @@ -0,0 +1,34 @@ +using AndDietCokeContracts.Enums; +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using AndDietCokeContracts.Exceptions; + +namespace AndDietCokeContracts.DataModels; + +public class DishDataModel(string id, string dishName, DishType dishType, double price, bool isDeleted) : IValidation +{ + public string Id { get; private set; } = id; + public string DishName { get; private set; } = dishName; + public DishType DishType { get; private set; } = dishType; + 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 (DishName.IsEmpty()) + throw new ValidationException("Field DishName is empty"); + if (DishType == DishType.None) + throw new ValidationException("Field DishType 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/AndDietCokeProject/AndDietCokeContracts/DataModels/DishHistoryDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/DishHistoryDataModel.cs new file mode 100644 index 0000000..605a6df --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/DishHistoryDataModel.cs @@ -0,0 +1,26 @@ +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AndDietCokeContracts.Exceptions; + +namespace AndDietCokeContracts.DataModels; + +public class DishHistoryDataModel(string dishId, double oldPrice) : IValidation +{ + public string DishId { get; private set; } = dishId; + public double OldPrice { get; private set; } = oldPrice; + public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; + public void Validate() + { + if (DishId.IsEmpty()) + throw new ValidationException("Field DishId is empty"); + if (!DishId.IsGuid()) + throw new ValidationException("The value in the field DishId is not a unique identifier"); + if (OldPrice <= 0) + throw new ValidationException("Field OldPrice is less than or equal to 0"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDataModel.cs new file mode 100644 index 0000000..f9a8a64 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDataModel.cs @@ -0,0 +1,39 @@ +using AndDietCokeContracts.Exceptions; +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace AndDietCokeContracts.DataModels; + +public class SaleDataModel(string id, string workerId, string? buyerId, double sum, bool isCancel, List dishes) : 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 Dishes { get; private set; } = dishes; + 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 ((Dishes?.Count ?? 0) == 0) + throw new ValidationException("The sale must include dishes"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDishDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDishDataModel.cs new file mode 100644 index 0000000..4bf9c48 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/OrderDishDataModel.cs @@ -0,0 +1,31 @@ +using AndDietCokeContracts.Exceptions; +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.DataModels; + +public class OrderDishDataModel(string orderId, string dishId, int count) : +IValidation +{ + public string OrderId { get; private set; } = orderId; + public string DishId { get; private set; } = dishId; + public int Count { get; private set; } = count; + public void Validate() + { + if (OrderId.IsEmpty()) + throw new ValidationException("Field OrderId is empty"); + if (!OrderId.IsGuid()) + throw new ValidationException("The value in the field OrderId is not a unique identifier"); + if (DishId.IsEmpty()) + throw new ValidationException("Field DishId is empty"); + if (!DishId.IsGuid()) + throw new ValidationException("The value in the field DishId is not a unique identifier"); + if (Count <= 0) + throw new ValidationException("Field Count is less than or equal to 0"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/PostDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..d112ae0 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/PostDataModel.cs @@ -0,0 +1,41 @@ +using AndDietCokeContracts.Enums; +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using AndDietCokeContracts.Exceptions; + +namespace AndDietCokeContracts.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/AndDietCokeProject/AndDietCokeContracts/DataModels/SalaryDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..81ef5c1 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,27 @@ +using AndDietCokeContracts.Exceptions; +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.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"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/DataModels/WorkerDataModel.cs b/AndDietCokeProject/AndDietCokeContracts/DataModels/WorkerDataModel.cs new file mode 100644 index 0000000..5c9c004 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/DataModels/WorkerDataModel.cs @@ -0,0 +1,42 @@ +using AndDietCokeContracts.Extensions; +using AndDietCokeContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Xml; +using AndDietCokeContracts.Exceptions; + +namespace AndDietCokeContracts.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) + throw new ValidationException($"Minors cannot be hired (EmploymentDate - { EmploymentDate.ToShortDateString() }, BirthDate - { BirthDate.ToShortDateString()})"); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/Enums/DishType.cs b/AndDietCokeProject/AndDietCokeContracts/Enums/DishType.cs new file mode 100644 index 0000000..608c730 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/Enums/DishType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.Enums; + +public enum DishType +{ + None = 0, + Pizza = 1, + Pasta = 2, + Beverage = 3 +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/Enums/PostType.cs b/AndDietCokeProject/AndDietCokeContracts/Enums/PostType.cs new file mode 100644 index 0000000..2262da5 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.Enums; + +public enum PostType +{ + None = 0, + Cashier = 1, + Cook = 2, + Packager = 3 +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/Exceptions/ValidationException.cs b/AndDietCokeProject/AndDietCokeContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..3d0b2c8 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.Exceptions; + +public class ValidationException(string message) : Exception(message) +{ +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/Extensions/StringExtensions.cs b/AndDietCokeProject/AndDietCokeContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..05b6caa --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/Extensions/StringExtensions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.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 _); + } +} \ No newline at end of file diff --git a/AndDietCokeProject/AndDietCokeContracts/Infrastructure/IValidation.cs b/AndDietCokeProject/AndDietCokeContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..a048240 --- /dev/null +++ b/AndDietCokeProject/AndDietCokeContracts/Infrastructure/IValidation.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AndDietCokeContracts.Infrastructure; + +internal interface IValidation +{ + void Validate(); +} \ No newline at end of file