From 7fb7fec2450d5c84da69da7349ae7238ddf6a731 Mon Sep 17 00:00:00 2001 From: xom9kxom9k Date: Sun, 9 Feb 2025 12:45:02 +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/ClientDataModel.cs | 40 ++++++++++++ .../DataModels/EmployeeDataModel.cs | 61 +++++++++++++++++++ .../DataModels/PostDataModel.cs | 40 ++++++++++++ .../DataModels/SalaryDataModel.cs | 31 ++++++++++ .../DataModels/SaleDataModel.cs | 56 +++++++++++++++++ .../DataModels/SaleTourDataModel.cs | 37 +++++++++++ .../DataModels/TourDataModel.cs | 38 ++++++++++++ .../DataModels/TourHistoryDataModel.cs | 31 ++++++++++ .../Enums/DiscountType.cs | 15 +++++ .../MagicCarpetContracts/Enums/PostType.cs | 15 +++++ .../MagicCarpetContracts/Enums/TourType.cs | 15 +++++ .../Exceptions/ValidationException.cs | 11 ++++ .../Extensions/StringExtensions.cs | 13 ++++ .../Infrastructure/IValidation.cs | 13 ++++ 14 files changed, 416 insertions(+) create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/ClientDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/EmployeeDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/PostDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/SalaryDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleTourDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/TourDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/DataModels/TourHistoryDataModel.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Enums/DiscountType.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Enums/PostType.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Enums/TourType.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Exceptions/ValidationException.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Extensions/StringExtensions.cs create mode 100644 MagicCarpetContracts/MagicCarpetContracts/Infrastructure/IValidation.cs diff --git a/MagicCarpetContracts/MagicCarpetContracts/DataModels/ClientDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/ClientDataModel.cs new file mode 100644 index 0000000..2c7cc21 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/ClientDataModel.cs @@ -0,0 +1,40 @@ +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class ClientDataModel(string id, string fIO, string phoneNumber, double discountSize) : IValidation +{ + public string Id { get; private set; } = id; + + public string FIO { get; private set; } = fIO; + + public string PhoneNumber { get; private set; } = phoneNumber; + + public double DiscountSize { get; private set; } = discountSize; + + 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/MagicCarpetContracts/MagicCarpetContracts/DataModels/EmployeeDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/EmployeeDataModel.cs new file mode 100644 index 0000000..aa23e25 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/EmployeeDataModel.cs @@ -0,0 +1,61 @@ +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class EmployeeDataModel(string id, string fio, string email, 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 Email { get; private set; } = email; + + 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 (Email.IsEmpty()) + throw new ValidationException("Field Email is empty"); + + if (!Regex.IsMatch(Email, @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")) + throw new ValidationException("Field Email is not a valid email address"); + + 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(-18).Date) + throw new ValidationException($"Only adults can 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 < 18) + throw new ValidationException($"Only adults can be hired (EmploymentDate - {EmploymentDate.ToShortDateString()}, BirthDate - {BirthDate.ToShortDateString()})"); + } +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/DataModels/PostDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/PostDataModel.cs new file mode 100644 index 0000000..364d785 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/PostDataModel.cs @@ -0,0 +1,40 @@ + using MagicCarpetContracts.Enums; +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.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/MagicCarpetContracts/MagicCarpetContracts/DataModels/SalaryDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SalaryDataModel.cs new file mode 100644 index 0000000..fdffdad --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SalaryDataModel.cs @@ -0,0 +1,31 @@ +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class SalaryDataModel(string employeeId, DateTime salaryDate, double employeeSalary) : IValidation +{ + public string EmployeeId { get; private set; } = employeeId; + + public DateTime SalaryDate { get; private set; } = salaryDate; + + 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/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleDataModel.cs new file mode 100644 index 0000000..dc24021 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleDataModel.cs @@ -0,0 +1,56 @@ +using MagicCarpetContracts.Enums; +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class SaleDataModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType, + double discount, bool isCancel, List tours) : IValidation +{ + public string Id { get; private set; } = id; + + public string EmployeeId { get; private set; } = employeeId; + + public string? ClientId { get; private set; } = clientId; + + public DateTime SaleDate { get; private set; } = DateTime.UtcNow; + public double Sum { get; private set; } = sum; + + public DiscountType DiscountType { get; private set; } = discountType; + + public double Discount { get; private set; } = discount; + + public bool IsCancel { get; private set; } = isCancel; + + public List Tours { get; private set; } = tours; + + 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 EmployeeId is empty"); + + if (!EmployeeId.IsGuid()) + throw new ValidationException("The value in the field EmployeeId is not a unique identifier"); + + if (!ClientId?.IsGuid() ?? !ClientId?.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 ((Tours?.Count ?? 0) == 0) + throw new ValidationException("The sale must include tours"); + } +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleTourDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleTourDataModel.cs new file mode 100644 index 0000000..8f68c6e --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/SaleTourDataModel.cs @@ -0,0 +1,37 @@ +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class SaleTourDataModel(string saleId, string cocktailId, int count) : IValidation +{ + public string SaleId { get; private set; } = saleId; + + public string TourId { get; private set; } = cocktailId; + + 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 (TourId.IsEmpty()) + throw new ValidationException("Field ProductId is empty"); + + if (!TourId.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/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourDataModel.cs new file mode 100644 index 0000000..6977e7d --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourDataModel.cs @@ -0,0 +1,38 @@ +using MagicCarpetContracts.Enums; +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace MagicCarpetContracts.DataModels; + +public class TourDataModel(string id, string tourName, string tourCountry, double price, TourType tourType) : IValidation +{ + public string Id { get; private set; } = id; + public string TourName { get; private set; } = tourName; + public string TourCountry { get; private set; } = tourCountry; + public double Price { get; private set; } = price; + public TourType Type { get; private set; } = tourType; + + 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 (TourName.IsEmpty()) + throw new ValidationException("Field TourName is empty"); + if (TourCountry.IsEmpty()) + throw new ValidationException("Field TourCountry is empty"); + if (Price <= 0) + throw new ValidationException("Field Price is less than or equal to 0"); + if (Type == TourType.None) + throw new ValidationException("Field Type is empty"); + } +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourHistoryDataModel.cs b/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourHistoryDataModel.cs new file mode 100644 index 0000000..a3169d4 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/DataModels/TourHistoryDataModel.cs @@ -0,0 +1,31 @@ +using MagicCarpetContracts.Exeptions; +using MagicCarpetContracts.Extensions; +using MagicCarpetContracts.Infrastructure; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.DataModels; + +public class TourHistoryDataModel(string cocktailId, double oldPrice) : IValidation +{ + public string TourId { get; private set; } = cocktailId; + + public double OldPrice { get; private set; } = oldPrice; + + public DateTime ChangeDate { get; private set; } = DateTime.UtcNow; + + public void Validate() + { + if (TourId.IsEmpty()) + throw new ValidationException("Field TourId is empty"); + + if (!TourId.IsGuid()) + throw new ValidationException("The value in the field TourId is not a unique identifier"); + + if (OldPrice <= 0) + throw new ValidationException("Field OldPrice is less than or equal to 0"); + } +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Enums/DiscountType.cs b/MagicCarpetContracts/MagicCarpetContracts/Enums/DiscountType.cs new file mode 100644 index 0000000..fe7c76c --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Enums/DiscountType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Enums; +[Flags] +public enum DiscountType +{ + None = 0, + OnSale = 1, + RegularCustomer = 2, + Certificate = 4 +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Enums/PostType.cs b/MagicCarpetContracts/MagicCarpetContracts/Enums/PostType.cs new file mode 100644 index 0000000..1e61386 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Enums/PostType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Enums; + +public enum PostType +{ + None = 0, + Manager = 1, + TravelAgent = 2, + Сhief = 3, +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Enums/TourType.cs b/MagicCarpetContracts/MagicCarpetContracts/Enums/TourType.cs new file mode 100644 index 0000000..768e073 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Enums/TourType.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Enums; + +public enum TourType +{ + None = 0, + Beach = 1, + Ski = 2, + Sightseeing = 3, +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Exceptions/ValidationException.cs b/MagicCarpetContracts/MagicCarpetContracts/Exceptions/ValidationException.cs new file mode 100644 index 0000000..8d8b32a --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Exceptions/ValidationException.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Exeptions; + +public class ValidationException(string message) : Exception(message) +{ +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Extensions/StringExtensions.cs b/MagicCarpetContracts/MagicCarpetContracts/Extensions/StringExtensions.cs new file mode 100644 index 0000000..297cf30 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Extensions/StringExtensions.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Extensions; + +public static class StringExtensions +{ + public static bool IsEmpty(this string str) => string.IsNullOrWhiteSpace(str); + public static bool IsGuid(this string str) => Guid.TryParse(str, out _); +} diff --git a/MagicCarpetContracts/MagicCarpetContracts/Infrastructure/IValidation.cs b/MagicCarpetContracts/MagicCarpetContracts/Infrastructure/IValidation.cs new file mode 100644 index 0000000..0ec8803 --- /dev/null +++ b/MagicCarpetContracts/MagicCarpetContracts/Infrastructure/IValidation.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MagicCarpetContracts.Infrastructure; + +public interface IValidation +{ + void Validate(); +} +